Why is ROL instruction used?

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

Tags:

Assembly

X86