Q) Write a program to simulate a Decimal Up-counter to display 00-99
This program will simply count from 00 to 99 and display each count on the screen like a timer. A delay is given during displaying of next numbers using defined delay procedures.
Algorithm:
Step 1: store the ASCII value of ‘0’ i.e., 30 in AL register.
Step 2: Display the first digit of counter on the screen.
Step 3: store the ASCII value of ‘0’ i.e., 30 in BL register.
Step 4: Display the second digit of counter on the screen.
Step 5: Increment the value of second digit and compare it will ‘9’.
Step 6: Introduce delay counter after each count.
Step 7: Once the second value exceeds 9, move the cursor position to the first digit and increment it by 1 and check whether even the first value exceeds 9 or not. If true, then you reached 99. Go to step 8 and stop. Otherwise, go to step 2 and continue.
Step 8: Stop.
CODE:
Explanation:
- .MODEL SMALL
- .CODE
- START:
We do not need data segment in this since we are not storing anything.
Code segment is therefore started and START is a label to indicate the start of the code.
Logic before the Code:
We need to display 00 to 99.
So what we will do is load two registers, say AL and BL with value ‘0’ initially, that is 30H(ASCII value is to be stored in order to display ‘0’).
Consider AL is for first digit. And BL is for second digit.
We will display both at once. That is 00 is displayed on the screen.
Next we will increase second digit (BL) and check whether it exceeds value ‘9’ (ASCII 39H). If it does not, then bring cursor to one position back(to override the existing second digit on the screen) and again display second register. That is numbers from 00-09 is displayed.
When second digit exceeds ‘9’, we will set it to ‘0’ and increase first digit(AL) by 1 and check whether first digit exceeds ‘9’ or not. If not, then bring cursor two positions back (to override the existing first and second digits on the screen) and display both registers.
That is values from 00-99 will be displayed.
When both registers exceed 9, we need to stop.
Now watch out the code,
- MOV AL,30H
- LOOP2:
- MOV DL,AL
- MOV AH,02H
- INT 21H
LOOP2 is the label to handle first digit.
Now Display first digit at once by moving AL into DL and calling 02H service with INT 21H.
First digit will be displayed.
- PUSH AX
- MOV BL,030H
- LOOP1:
- MOV AH,02H
- MOV DL,BL
- INT 21H
Now load BL with ‘0’. And display it by copying it into DL and using 02H and INT 21H.
- INC BL
- CALL DELAY
- MOV AH,03H
- INT 10H
Increase the second digit by 1. And call DELAY to introduce a time delay between displaying 2 counts (because processors are very fast :p).
Next, read the current cursor position using DOS service 03H into AH on INT 10H.(recall what i said in the previous programs, INT 10H is related to all cursor/screen related operations).
Now, you will get the current row value in DH and current column into DL.
Now bring the cursor to first column (DL=01H) in the current row.
- MOV AH,02H
- MOV DL,01H
- INT 10H
- CMP BL,039H
- JLE LOOP1
If it does not exceed, simply print next number by jumping to LOOP1 where you increase BL and display second digit again.
If it exceeds 9, we need to set it to zero and increment first digit(AL) and print next numbers.
- MOV AH,02H
- MOV DL,00H
- INT 10H
- POP AX
- INC AL
- CMP AL,039H
- JLE LOOP2
Now get back the first digit which was backed up into the stack into AL, using POP instruction.
Increase first digit(AL) and check whether it exceed 9. If it does not, simply jump to LOOP2 and continue displaying.
If the first digit exceeds 9, it means you reached 99. You need to stop at this time.
- MOV AH,4CH
- INT 21H
- DELAY PROC
- PUSH CX
- PUSH BX
- MOV CX,0FFFH
- N3:
- MOV BX,0FFFH
- N4:
- DEC BX
- JNZ N4
- LOOP N3
- POP BX
- POP CX
- RET
- DELAY ENDP
This is just to produce a time delay during printing numbers.
Load the CX and BX registers with a high value and use it for looping.
I will better explain this with a ‘for loop’ of the C language.
for(CX=0FFFH;CX>=0;CX--){for(BX=0FFFH;BX>=0;BX--){//this nested loop will produce a time delay in the processor since it is a heavy big loop.}}
- END
Output:
Numbers from 00 to 99 will be displayed on the screen.
Share your views about this article!