I am somewhat new to assembly language programming so pardon me if this question seems irrelevant. I was trying to understand a 32-bit addition program and following is one of the procedures used to display the result of addition (saved in EAX) back to console:
;Procedure to display EAX as a 8 digit hex number
DISPH PROC NEAR
PUSH EBX ; Save EBX
MOV CL,4 ; To rotate the register by 4 bits
MOV SI,8 ; Count for displaying 8 digits
DISPH1:
ROL EAX,CL ; Rotate EAX left by 4 bits
PUSH EAX ; Save EAX
AND AL,0FH
ADD AL,30H
CMP AL,'9' ; if AL <= '9', AL contains the ASCII code
JBE DISPH2
ADD AL,7 ; if AL > '9' , add 07H to AL to convert into ASCII
DISPH2:
MOV AH,2H ; O/P subprogram
MOV DL,AL ; Call MS-DOS O/P subprogram
INT 21H ; Display the data in DL register on screen
POP EAX ; retrieve EAX from Stack
DEC SI
JNZ DISPH1
POP EBX ; Restore EBX
RET
DISPH ENDP
END ; end of file
Please help me understand why the ROL instruction is used under DISP1 label, and what does it achieve. Thanks in advance. :)
The Output is the number that represents the new binary string. ST Program - ROL: The ROL function is used to rotate the positions of the bits in the Input string to the left: Output := ROL (IN, N);
ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to Carry Flag [CF]. ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to Carry Flag [CF]. RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to MSB.
SHL : Shift Left. SAL : Shift Arithmetic Left. ROL : Rotate Left.
The ROL instruction shifts each bit to the left, with the highest bit copied in the Carry flag and into the lowest bit. The ROR instruction shifts each bit to the right, with the lowest bit copied in the Carry flag and into the highest bit.
The code prints a 32 bit value in hexadecimal. The ROL instruction is convenient here because it lets you process the nibbles in the highest to lowest order.
Consider the value 0xCAFED00D. To print the first four letters it out you have to extract the values 'C' A' 'F' and 'E' in this order.
EAX = 0xCAFED00D
ROL EAX, 4 -> EAX = AFED00DC (lowest nibble is C)
ROL EAX, 4 -> EAX = FED00DCA (lowest nibble is A)
ROL EAX, 4 -> EAX = ED00DCAF (lowest nibble is F)
ROL EAX, 4 -> EAX = D00DCAFE (lowest nibble is E)
and so on..
As you can see this sequence moves the value of interest into the lowest nibble. Extracting this value is done by ANDing the resulting value with 0x0f.
Afterwards the value gets converted into a hexadecimal character and outputted via DOS BIOS.
It is used to iterate over the digits of the number during the display operation. At each iteration it sets into the lower byte of EAX the bits that correspond to the next digit to display. Unlike SHL/SHR operations, ROL will preserve the original value in the EAX after completion of the entire display routine. ROL is naturally convenient for displaying from the highest digit (similar effect could be achieved by SHR by 32-amount of processed bits, but ROL is more straightforward).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With