home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / DOSGETCH.ASM < prev    next >
Assembly Source File  |  1997-07-05  |  2KB  |  72 lines

  1. ;  +++Date last modified: 05-Jul-1997
  2.  
  3.         page    55, 132
  4.  
  5. ;  FUNCTIONS: dosgetch/doswaitch
  6. ;
  7. ;  Donated to the public domain 96-11-12 by Tom Torfs (2:292/516)
  8. ;
  9. ;  dosgetch() attempts to read a character from stdin. Returns the
  10. ;  character read or EOF (-1) if no character is available. Extended keys
  11. ;  are returned in two parts: first a zero and then the actual code.
  12. ;
  13. ;  doswaitch() works alike but waits if no character is available.
  14. ;
  15. ;  Requires MASM 5.1 or later or equivalent
  16. ;
  17. ;  Assemble with:       MASM /Mx /z ...
  18. ;                       TASM /jMASM /mx /z ...
  19. ;
  20.  
  21. %       .MODEL  memodel,C               ;Add model support via
  22.                                         ;command line macros, e.g.
  23.                                         ;MASM /Dmemodel=LARGE
  24.  
  25.         .CODE
  26. ;
  27. ; int dosgetch(void) - comments see above
  28. ;
  29.  
  30.         PUBLIC  dosgetch
  31.  
  32. dosgetch        PROC
  33.  
  34.         push dx
  35.         mov     ah, 6                   ; direct console I/O
  36.         mov     dl, 255                 ; read a character
  37.         int     21h                     ; let DOS do the job
  38.         pop dx
  39.  
  40.         jz      nochar                  ; character ready ?
  41.  
  42.         xor     ah,ah                   ; clear high byte ax
  43.         ret                             ; return
  44.  
  45. nochar:
  46.  
  47.         mov     ax, -1                  ; return EOF
  48.         ret                             ; return
  49.  
  50. dosgetch        ENDP    
  51.  
  52. ;
  53. ; int doswaitch(void) - comments see above
  54. ;
  55.  
  56.         PUBLIC  doswaitch
  57.  
  58. doswaitch       PROC
  59.  
  60. waitloop:
  61.  
  62.         call    dosgetch               ; try to read character
  63.         
  64.         cmp     ax, -1                 ; available ?
  65.         je waitloop                    ; no -> repeat
  66.  
  67.         ret                            ; return
  68.  
  69. doswaitch       ENDP    
  70.  
  71.         end
  72.