Digital Clock In assembly Language
.model small
.stack 100h
.data
strng0 db 'Press X to terminate the program.',10,'' ; Message to prompt the user for input.
input db 0 ; Variable to store the user input (single digit, 0-9).
hours db 0 ; Variable to store the current hours.
minutes db 0 ; Variable to store the current minutes.
seconds db 0 ; Variable to store the current seconds.
variable db 0 ; Temporary variable used to convert numbers to ASCII for display.
onelop db 0 ; Flag variable used to track if 'one loop' has occurred.
.code
jmp start
display_double_digits proc
mov dx, 0
mov ax, 0
mov al, variable
mov bl, 10
div bl
mov bh, al
mov bl, ah
mov ah, 02h
mov dl, bh
add dl, 48
int 21h
mov ah, 02h
mov dl, bl
add dl, 48
int 21h
ret
display_double_digits endp
milli_seconds_counter proc
mov cx, 40000 ; Counter value for the delay loop.
for:
loop for
ret
milli_seconds_counter endp
start:
main proc
mov ax, @data
mov ds, ax
lea dx, strng0
mov ah, 09h
int 21h
lea dx, strng
mov ah, 09h
int 21h
mov ah, 01h
int 21h ; This will take only single-digit input from the user between 0 and 9.
sub al, 48
mov seconds, al ; Convert the ASCII value to actual numeric value.
mov input, al
mov al, input
lopp:
mov cl, onelop
cmp cl, 1
je check_input ; Check if 'one loop' has occurred.
mov bl, hours
cmp bl, 11
je one_loop ; Check if 'hours' is 11 (to reset the clock to 0).
jmp noloop ; If no special conditions, continue normally.
one_loop:
mov onelop, 1 ; Set the 'one loop' flag.
jmp noloop
check_input:
mov ch, hours
cmp ch, 0
jne noloop ; If 'hours' is not 0, continue normally.
mov al, input
cmp al, seconds
jne noloop ; If 'input' is not equal to 'seconds', continue normally.
jmp outt ; Terminate the program as 'X' key has been pressed.
noloop:
mov ah, 02h
mov dl, 10 ; Display a new line.
int 21h
; Display hours.
mov al, hours
cmp al, 9
jg nocheck
mov ah, 02h
mov dl, 48
int 21h
; Display the hours as a single digit.
mov ah, 02h
mov dl, hours
add dl, '0'
int 21h
jmp check
nocheck:
mov variable, al
call display_double_digits
check:
mov ah, 02h
mov dl, ':' ; Display a colon to separate hours and minutes.
int 21h
; Display minutes.
mov al, minutes
cmp al, 9
jg nocheck1
mov ah, 02h
mov dl, 48
int 21h
; Display the minutes as a single digit.
mov ah, 02h
mov dl, minutes
add dl, '0'
int 21h
jmp check1
nocheck1:
mov variable, al
call display_double_digits
check1:
mov ah, 02h
mov dl, ':' ; Display a colon to separate minutes and seconds.
int 21h
; Display seconds.
mov al, seconds
cmp al, 9
jg nocheck2
mov ah, 02h
mov dl, 48
int 21h
mov ah, 02h
mov dl, seconds
add dl, '0'
int 21h
; Display the seconds as a single digit.
jmp check2
nocheck2:
mov variable, al
call display_double_digits
check2:
add seconds, 1 ; Increment seconds.
mov al, seconds
cmp al, 59
jle noreset ; If 'seconds' is less than or equal to 59, no need to reset.
mov seconds, 0
add minutes, 1 ; Increment minutes.
mov al, minutes
cmp al, 59
jle noreset ; If 'minutes' is less than or equal to 59, no need to reset.
; Reset the minutes to 0 after reaching 60.
mov minutes, 0
add hours, 1 ; Increment hours.
cmp hours, 11
jle noreset ; If 'hours' is less than or equal to 11, no need to reset (12-hour format).
; Reset the hours to 0 after reaching 12 to follow the 12-hour format.
mov hours, 0
mov minutes, 0
mov seconds, 0
noreset:
call milli_seconds_counter ; Introduce a delay to wait for one second.
; Check for keyboard input status.
mov ah, 01h
int 16h
; Check the Zero Flag (ZF) to see if a key has been pressed.
; If no key was pressed, jump to NoKeyPress label.
jz NoKeyPress
; If a key has been pressed, read the ASCII value of the key.
mov ah, 00h
int 16h
; The ASCII value of the pressed key will be in the AL register.
mov dl, al
mov ah, 02h
int 21h
cmp al, 'X'
je outt ; If 'X' key has been pressed, terminate the program.
cmp al, 'x'
je outt ; If 'x' key has been pressed (lowercase), also terminate.
NoKeyPress:
; Continue the infinite loop to update the clock.
jmp lopp
outt:
; Display a new line before terminating the program.
mov ah, 02h
mov dl, 10
int 21h
main endp
; Ending the program.
mov ah, 4ch
int 21h
end