Function AH = 2C of the INT 21 Dos interrupt will return the current time.
Seconds are returned in DH.
Here is a simple expample of using this interrupt,
it will print the ascii character 'A' every time a second has passed.
This program is in a infinite loop, modify to suite your needs.
MOV BH,DH ; DH has current second
GETSEC: ; Loops until the current second is not equal to the last, in BH
CMP BH,DH ; Here is the comparison to exit the loop and print 'A'
Nicholas Dean Pugh, Coder ........
thanks a lot for the help..
can u tell me how i can wait for a keyboard input from the user while running the timer?
i am making a game where there are 2 players. the first player enters a piece of music.. and the second player has to reproduce the music within a particular time.. so how do i run the timer and wait for the 2nd player's input at the same time?
This code uses the direct I/O functions provided by INT 21h
along with function AH=2Ch of INT 21h.
The code can be used to time how long it took for the user to enter input,
Here the code follows, it will allow the user 5 seconds to input a string,
modify to suite your needs.
text db 0,0,0,0,0,36
mov si,offset text ; Move offset of string in data region into SI register
mov ah,02ch ; Get current second
mov bh,dh ; Store current second
mov ah,02ch ; Call function 02C of INT 021 to get new time
sub dh,05h ; Subtract 05 from new second. Giving 5 Secs for input.
; For if old second in BH=05 and new second in DH-5=1,
; four more
; seconds must pass.
je endprog ; Exit when DH is finally equal to BH.
; Example BH=05 and DH - 05 = 05, then DH = 0F and
; 05 - 0F = Five Seconds
mov ah,06h ; Function 06h of INT 021 will directly read from the Stdin/Stdout
mov dl,0ffh ; Move 0ff into the DL register to read from the keyboard
jz readloop ; If the zero flag of the FLAGS register is set, no key was pressed.
; And this conditional jump will change the offset of IP to the readloop
; label if the zero flag is set, to read again.
mov [si],al ; If execution made it to here it will move the byte in AL into the offset
; of the string in the data region
inc si ; this will increment to the next byte of the string, to store the next
mov bl,[si] ; Check to see if the end of the string has been reached.
je endprog ; If so exit.
mov bx,offset text ; This rest will print out the read in string.
mov dx,offset text
Hi there, if I were to implement a timer of 10sec for the user to enter a key as interrupt to the game. How do i structure the code? Should I use the INT 16H or can I use the code that you implemented in this thread using the INT 21H with the change from the offset data to keystroke?
public class GrossmontBank
//class variables (global - accessible throughout this class)
//scanner object to be used throughout
private static Scanner input = new Scanner(System.in);