home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / KEYWAIT.ZIP / KEYWAIT.DOS < prev    next >
Text File  |  1989-09-12  |  3KB  |  167 lines

  1. ;-------------------------------------------------------------------------------
  2. ;
  3. ;                keywait.asm
  4. ;                ===========
  5. ;
  6. ;    Waits a few seconds for a keystroke.  If ESC is pressed then sets
  7. ;    errorlevel to 27.  If a function key F1-F10 is pressed then sets
  8. ;    errorlevel to a value in the range 1-10 corresponding to the function
  9. ;    key.  If time expires or any other key is pressed then errorlevel
  10. ;    is set to zero.  Can be useful in .BAT files.
  11. ;
  12. ;    A public domain program by Jon Saxton.  Please distribute source
  13. ;    code and not just the object code.
  14. ;
  15. ;-------------------------------------------------------------------------------
  16.  
  17. COLON    equ    ':'
  18. CR    equ    0Dh
  19. ETX    equ    03h
  20. ESCk    equ    1Bh
  21. SPACE    equ    ' '
  22. LP    equ    '('
  23. RP    equ    ')'
  24. F1    equ    59
  25. F10    equ    68
  26.  
  27.     .model    small
  28.  
  29.     .code
  30.  
  31. ticks    db    10            ;Default number of seconds
  32. crlf    db    CR,LF,'$'
  33.  
  34. start:
  35.     mov    bx,80h            ;Point at command tail
  36.     mov    al,[bx]            ;Get character count
  37.     or    al,al            ;Check if anything there
  38.     jz    default
  39.     mov    cx,0            ;Initialise product
  40. convert:
  41.     inc    bx
  42.     xor    ah,ah
  43.     mov    al,[bx]
  44.     cmp    al,CR
  45.     jz    setTicks
  46.     cmp    al,'0'
  47.     jb    convert
  48.     cmp    al,'9'
  49.     ja    convert
  50.     sub    al,'0'
  51.     add    cx,cx            ;*2
  52.     mov    dx,cx
  53.     add    cx,cx            ;*4
  54.     add    cx,cx            ;*8
  55.     add    cx,dx            ;*10
  56.     add    cx,ax
  57.     jmp    convert
  58. setTicks:
  59.     mov    ax,seg start
  60.     mov    ds,ax
  61.     mov    ax,cx
  62.     or    al,al
  63.     jz    default
  64.     cmp    ax,100
  65.     ja    default
  66.     mov    ticks,al
  67. default:
  68.     mov    ah,2Ch            ;"Get system time" function
  69.     int    21h
  70.     mov    bh,dh            ;Save initial "seconds" value
  71. time1:
  72.     mov    ah,2Ch
  73.     int    21h            ;Get time
  74.     cmp    dh,bh            ;Has the second changed?
  75.     jnz    showTime        ;Yes: display the time
  76. key1:                    ;No:  keyboard loop
  77.     xor    bl,bl            ;Set counter
  78.     mov    ah,0Bh            ;Keyboard status function
  79. key2:
  80.     int    21h            ;Has key been pressed?
  81.     or    al,al
  82.     jnz    exit            ;Quit if so
  83.     dec    bl            ;Decrement the status loop counter
  84.     jnz    key2            ;Loop if counter not exhausted
  85.     jmp    time1            ;Otherwise go and look at the clock
  86. exit:
  87.     mov    ah,07h            ;Fetch and discard the key press
  88.     int    21h
  89.     or    al,al            ;Extended key code?
  90.     jnz    simple            ;No, check for ESC etc
  91.     mov    ah,07h            ;Fetch second byte of extended code
  92.     int    21h
  93.     cmp    al,F1            ;Check for F1 - F10
  94.     jb    sig0
  95.     cmp    al,F10
  96.     ja    sig0
  97.     sub    al,F1-1
  98.     jmp    sig
  99. simple:
  100.     cmp    al,ESCk            ;ESC causes errorlevel
  101.     jz    sig            ; to be set to 27
  102. sig0:
  103.     xor    al,al
  104. sig:
  105.     mov    ah,4Ch
  106.     push    ax
  107.     mov    ah,9
  108.     mov    dx,crlf
  109.     int    21h
  110.     pop    ax
  111.     int    21h
  112. showTime:
  113.     push    dx            ;Save seconds
  114.     push    cx            ;Save hours and minutes
  115.     mov    al,CR            ;Return to beginning of line
  116.     call    putchar            ;Display
  117.     pop    ax            ;  hour
  118.     push    ax
  119.     mov    al,ah
  120.     call    decimal
  121.     mov    al,COLON        ;  colon
  122.     call    putchar
  123.     pop    ax            ;  minute
  124.     call    decimal
  125.     mov    al,COLON        ;  colon
  126.     call    putchar
  127.     pop    ax            ;  second
  128.     mov    al,ah
  129.     call    decimal
  130.     mov    al,SPACE        ;  space
  131.     call    putchar
  132.     mov    al,LP            ;  left parenthesis
  133.     call    putchar
  134.     mov    al,ticks
  135.     dec    al
  136.     mov    ticks,al
  137.     pushf
  138.     call    decimal            ;  seconds remaining
  139.     mov    al,RP
  140.     call    putchar            ;  right parenthesis
  141.     xor    al,al
  142.     popf
  143.     jz    sig
  144.     jmp    default
  145.  
  146. decimal:                ;Display AL as decimal number
  147.     mov    bl,10            ;Divisor
  148.     cbw                ;Dividend
  149.     div    bl
  150.     push    ax            ;Save remainder
  151.     call    h4            ;Display quotient from AL
  152.     pop    ax            ;Recover remainder
  153.     mov    al,ah
  154.     call    h4            ;Display remainder
  155.     ret
  156. h4:
  157.     add    al,30h
  158. putchar:
  159.     mov    dl,al
  160.     mov    ah,2
  161.     int    21h
  162.     ret
  163.  
  164.     .stack    60
  165.  
  166.     end    start
  167.