home *** CD-ROM | disk | FTP | other *** search
- ;-------------------------------------------------------------------------------
- ;
- ; keywait.asm
- ; ===========
- ;
- ; Waits a few seconds for a keystroke. If ESC is pressed then sets
- ; errorlevel to 27. If a function key F1-F10 is pressed then sets
- ; errorlevel to a value in the range 1-10 corresponding to the function
- ; key. If time expires or any other key is pressed then errorlevel
- ; is set to zero. Can be useful in .BAT files.
- ;
- ; A public domain program by Jon Saxton. Please distribute source
- ; code and not just the object code.
- ;
- ;-------------------------------------------------------------------------------
-
- COLON equ ':'
- CR equ 0Dh
- ETX equ 03h
- ESCk equ 1Bh
- SPACE equ ' '
- LP equ '('
- RP equ ')'
- F1 equ 59
- F10 equ 68
-
- .model small
-
- .code
-
- ticks db 10 ;Default number of seconds
- crlf db CR,LF,'$'
-
- start:
- mov bx,80h ;Point at command tail
- mov al,[bx] ;Get character count
- or al,al ;Check if anything there
- jz default
- mov cx,0 ;Initialise product
- convert:
- inc bx
- xor ah,ah
- mov al,[bx]
- cmp al,CR
- jz setTicks
- cmp al,'0'
- jb convert
- cmp al,'9'
- ja convert
- sub al,'0'
- add cx,cx ;*2
- mov dx,cx
- add cx,cx ;*4
- add cx,cx ;*8
- add cx,dx ;*10
- add cx,ax
- jmp convert
- setTicks:
- mov ax,seg start
- mov ds,ax
- mov ax,cx
- or al,al
- jz default
- cmp ax,100
- ja default
- mov ticks,al
- default:
- mov ah,2Ch ;"Get system time" function
- int 21h
- mov bh,dh ;Save initial "seconds" value
- time1:
- mov ah,2Ch
- int 21h ;Get time
- cmp dh,bh ;Has the second changed?
- jnz showTime ;Yes: display the time
- key1: ;No: keyboard loop
- xor bl,bl ;Set counter
- mov ah,0Bh ;Keyboard status function
- key2:
- int 21h ;Has key been pressed?
- or al,al
- jnz exit ;Quit if so
- dec bl ;Decrement the status loop counter
- jnz key2 ;Loop if counter not exhausted
- jmp time1 ;Otherwise go and look at the clock
- exit:
- mov ah,07h ;Fetch and discard the key press
- int 21h
- or al,al ;Extended key code?
- jnz simple ;No, check for ESC etc
- mov ah,07h ;Fetch second byte of extended code
- int 21h
- cmp al,F1 ;Check for F1 - F10
- jb sig0
- cmp al,F10
- ja sig0
- sub al,F1-1
- jmp sig
- simple:
- cmp al,ESCk ;ESC causes errorlevel
- jz sig ; to be set to 27
- sig0:
- xor al,al
- sig:
- mov ah,4Ch
- push ax
- mov ah,9
- mov dx,crlf
- int 21h
- pop ax
- int 21h
- showTime:
- push dx ;Save seconds
- push cx ;Save hours and minutes
- mov al,CR ;Return to beginning of line
- call putchar ;Display
- pop ax ; hour
- push ax
- mov al,ah
- call decimal
- mov al,COLON ; colon
- call putchar
- pop ax ; minute
- call decimal
- mov al,COLON ; colon
- call putchar
- pop ax ; second
- mov al,ah
- call decimal
- mov al,SPACE ; space
- call putchar
- mov al,LP ; left parenthesis
- call putchar
- mov al,ticks
- dec al
- mov ticks,al
- pushf
- call decimal ; seconds remaining
- mov al,RP
- call putchar ; right parenthesis
- xor al,al
- popf
- jz sig
- jmp default
-
- decimal: ;Display AL as decimal number
- mov bl,10 ;Divisor
- cbw ;Dividend
- div bl
- push ax ;Save remainder
- call h4 ;Display quotient from AL
- pop ax ;Recover remainder
- mov al,ah
- call h4 ;Display remainder
- ret
- h4:
- add al,30h
- putchar:
- mov dl,al
- mov ah,2
- int 21h
- ret
-
- .stack 60
-
- end start
-