Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ROL instruction used?

Tags:

x86

assembly

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. :)

like image 932
Aditya Bhatnagar Avatar asked Nov 08 '13 10:11

Aditya Bhatnagar


People also ask

What is rol used for?

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);

What is ROL instruction in 8086?

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.

What is the difference between SHL and Rol?

SHL : Shift Left. SAL : Shift Arithmetic Left. ROL : Rotate Left.

How does ROL and ROR work?

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.


2 Answers

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.

like image 53
Nils Pipenbrinck Avatar answered Sep 20 '22 07:09

Nils Pipenbrinck


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).

like image 30
SomeWittyUsername Avatar answered Sep 17 '22 07:09

SomeWittyUsername