Let's
see something simple but interesting stuff in assembly language. To
print a value on screen, various high level programming languages use
there library function and statement. For example, C uses 'printf',
C++ uses 'cout', Java uses 'println' and Python uses 'print'. It is
single line statements that does our task. But, microprocessor
implements several sequential steps to print a number on screen.
Here,
I have used the widely used MASM to demonstrate this work. Basically,
to print a number microprocessor don't have any function for this. It
is only possible to print a single character with function number 02
an interrupt number 21h. The character that you have to print, must
be present in DL register.
For
example,
MOV
DL,'a'
MOV
AH,02H
INT
21H
This
code will print 'a' on the screen (without quote!). Here DL register
of processor is storing ASCII value of character 'a'. So, following
code will also do the same task, as ASCII value of character 'a' is
41 in hexadecimal.
MOV
DL,41H
MOV
AH,02H
INT
21H
Here,
02 is function number and it is necessary to store it in AH register
before invoking an interrupt. Now, to print number on screen it is
not directly possible using assembly language. We need to do it using
ASCII values of hexadecimal digit. Remember ASCII values of 0 to 9
are 30 to 39 respectively. So, if you want to print 5 on screen, we
need to store 35 in DL register before invoking the interrupt. Now,
following code will print 5 on screen.
MOV
DL,35H
MOV
AH,02H
INT
21H
It
means that, we need code conversion by adding 30 in hex to the
respective single digit number. If you want to print 2 digit number
the same procedure can be followed for both digit by rotating. Then
one after the another following algorithm will do the task for us.
- Get a two digit number in temporary register say BH.
- Rotate the bits of the number by 4 position so hex digit would be swapped.
- Copy the rotated number in DL register.
- Mask high order 4 bits to zero.
- Add 30h in DL.
- Use function 02h and int 21h to print the number.
- Repeat the steps 2 to 6 for second digit also.
Code:
- MOV BH,96H ;number to print
- MOV CH,02H ;number of digits
- MOV CL,04H ;rotation count
- UP:ROL BH,CL ;swap digits
- MOV DL,BH
- AND DL,0FH ;mask MSB digit
- ADD DL,30H ;add 30 in DL
- MOV AH,02H ;function number
- INT 21H
- DEC CH ;do twice
- JNZ UP
Here,
we simulate the code stepwise.
- BH=96H
- CH=02H
- CL=04H
- BH=69H
- DL=69H
- DL=09H
- DL=39H
- AH=02H
- It will print DL=39 i.e 9 on screen.
- CH=01H
- Condition true as ZF=0. Jump to 4th Statement.
- BH=96H
- DL=96H
- DL=06H
- DL=36H
- AH=02H
- It will print DL=36 i.e 6 on screen.
- CH=00H
- Condition false so terminate.
Now,
here we have printed the hex digits only between 0-9. But, if digits
are between A-F then we need to check this condition also. The ASCII
value of numbers and characters have difference of 07H. So, if the
hex digit of number is greater than 9 then to have to add 07H also in
DL register. The following code will do the task.
MOV
BH,5CH ;number to print
MOV
CH,02H
MOV
CL,04H
UP:
ROL
BH,CL
MOV
DL,BH
AND
DL,0FH
CMP
DL,09H
JBE
NEXT
ADD
DL,07H
NEXT:
ADD
DL,30H
MOV
AH,02H
INT
21H
DEC
CH
JNZ
UP
You
may use debugger to check the execution of the code. It is to be
noted that this code will work in x86 architecture using MASM or
TASM.
Bubble Sort Algorithm
ReplyDeleteElements: Assembly Language Programming
Register Window with Appropriate Window
Basic Error types
Deadlock Prevention Methods
Church Turing Thesis
Artificial Intelligence
Failure Handling in IPC
Status Register Bits
ReplyDeleteHashing Functions with Example
Crossbar Switch Interconnection Structures
Delay, Loss, Throughput
GUI Architecture of Android
The King Casino Resort - Hertzaman
ReplyDeleteFind the perfect place to stay, herzamanindir.com/ play, and goyangfc unwind at 출장샵 Harrah's 1xbet korean Resort febcasino.com Southern California. Get your points now!