9A. Displaying the current system time [VTU 4th sem MP lab]

vtu mp 9a

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
Unlike previous programs where we wrote .MODEL SMALL to indicate that there is one data segment DS and one code segment CS, Here we use other method of declaring it.
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
DATA segment is the beginning of data segment.
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
CODE SEGMENT is the beginning of code segment. And START is the label which we will be declaring before writing the code and ending the label with ‘END START’ after code is finished. (In all the lab programs using this ASSUME CS:CODE,DS:DATA method)

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
Move DOS service number 2CH into AH register. Call Interrupt INT 21H.Systems time will be obtained and,
  1. Hours will be saved in CH register. 
  2. Minutes will be saved in CL register. 
  3. Seconds will be saved in DH register.
We will so obtain the time and print the value of each register by converting the HH/MM/SS by converting to ASCII equivalent.

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
Hour is contained in CH register. So move it into AL register. Call AAM to separate two digits into AH and AL registers as told above.

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
Move ‘:’ into DL register and display using 02H and INT 21H.


Now let us display Minutes,
  • MOV AL,CL
  • AAM
  • MOV BX,AX
  • CALL DISPLAY
Minutes is contained in CL register. So move it into AL register.

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
Display a separator again using the same way.

Now let us display seconds,
  • MOV AL,DH
  • AAM
  • MOV BX,AX
  • CALL DISPLAY
Minutes is contained in DH register. So move it into AL register.

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
Terminate the program using 4CH into AH and on 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
CODE END declares the end of code segment
And END START ends the START label.


Output:
CURRENT TIME IS: 10:38:26

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!