Q) Read the current time from the system and display it in the standard format of the screen.
This program will simply display the current system time on the screen (in HH:MM:SS format).
Algorithm:
Step 1: Read the system time using ‘2CH’ service number.
Step 2: To display the hour, minute, seconds, convert from hexadecimal to BCD using ‘AAM’ instruction.
Step 3: Display using ‘02H’ service number.
Step 4: Stop.
CODE:
Explanation:
- ASSUME CS:CODE,DS:DATA
Previous one was something like
.MODEL SMALL
.DATA
...
...
.CODE
....
...
END
This method will be
ASSUME CS:CODE,DS:DATA
DATA SEGMENT
..
..
DATA ENDS
CODE SEGMENT
..
..
CODE ENDS
Here we tell the compiler that, CS is assumed to be the code segment and DS is taken as Data segment.
- DATA SEGMENT
- MSG DB 10,13,"CURRENT TIME IS: $"
- DATA ENDS
This is the data segment with the message “Current time is:” terminated with ‘$’ character. 10,13 have their usual meanings.
DATA ENDS tells that it is the end of the data segment
- CODE SEGMENT
- START:
- MOV AX,DATA
- MOV DS,AX
- MOV AH,09H
- LEA DX,MSG
- INT 21H
One more change in this method is Data segment is initialized in different method.
In the .MODEL SMALL method , it is like this:
MOV AX,@DATA
MOV DS,AX
In ASSUME CS:CODE,DS:DATA method, it is like
MOV AX,@DATA
MOV DS,AX
[Do not use ‘@’]
Then print the message “Current time is:” using 09H service with INT 21H.
Now we take system’s current time using DOS service ‘2CH’.
How to obtain system’s current time in 8086 microprocessor programming?
- MOV AH,2CH
- INT 21H
- Hours will be saved in CH register.
- Minutes will be saved in CL register.
- Seconds will be saved in DH register.
Lets also learn about AAM here.
AAM (ASCII Adjustment after multiplication).
Instead of exactly telling what AAM is, let me tell you how it is used in the program.
Lets say we moved the ‘Hour’ which is in CH to AL.
Now AL is containing a two digit number in the 8bits of AL.
After AAM, The first 4 bits of AL will be stored in AH and second 4 bits will be saved in AL. Which means we now got our first digit of Hour in AH and second digit in AL
Now its time to display the time (lulz ;____;)
Let us display Hours,
- MOV AL,CH
- AAM
- MOV BX,AX
- CALL DISPLAY
After AAM, AX is moved into BX and now its the time to display Hours. Call DISPLAY procedure to print the hours.
Now let us display a separator (:),
- MOV DL,':'
- MOV AH,02H
- INT 21H
Now let us display Minutes,
- MOV AL,CL
- AAM
- MOV BX,AX
- CALL DISPLAY
After AAM, AX is moved into BX and now its the time to display Minutes. Call DISPLAY procedure to print the minutes.
Again a separator is to be displayed,
- MOV DL,':'
- MOV AH,02H
- INT 21H
Now let us display seconds,
- MOV AL,DH
- AAM
- MOV BX,AX
- CALL DISPLAY
After AAM, AX is moved into BX and now its the time to display seconds. Call DISPLAY procedure to print the seconds.
- MOV AH,4CH
- INT 21H
This is the display logic. We use the similar logic that was used in lab program 4A.
- DISPLAY PROC NEAR
- MOV DL,BH
- ADD DL,30H
- MOV AH,02H
- INT 21H
- MOV DL,BL
- ADD DL,30H
- INT 21H
- RET
- DISPLAY ENDP
DISPLAY PROC NEAR stated that its the beginning of DISPLAY procedure’s definition.
Here, NEAR means the function definition is present nearer to the function call.
The processed HH/MM/SS after AAM was moved to BX in the above codes. So the first digit is in the BH and second is in the BL (As per AAM).
We display each by converting into ASCII.
First move BH(first digit) to DL register and add ASCII of ‘0’, that is 30H to DL and print it using service 02H, with INT 21H.
Then move second digit into DL and add 30H to get the ASCII of that digit.
That’s it. It has been displayed now.
- CODE ENDS
- END START
And END START ends the START label.
Output:
CURRENT TIME IS: 10:38:26
Share your views about this article!