Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of the rotate instructions (ROL, RCL on x86)?

I always wondered what's the purpose of the rotate instructions some CPUs have (ROL, RCL on x86, for example). What kind of software makes use of these instructions?

I first thought they may be used for encryption/computing hash codes, but these libraries are written usually in C, which doesn't have operators that map to these instructions. (Editor's note: see Best practices for circular shift (rotate) operations in C++ for how to write C or C++ that will compile to a rotate instruction. Also, optimized crypto libraries often do have asm for specific platforms.)

Has anybody found an use for them? Why where they added to the instructions set?

like image 335
Gratian Lup Avatar asked Feb 12 '11 06:02

Gratian Lup


People also ask

What is RCL used for?

The rotate left (ROL) and rotate through carry left (RCL) instructions shift all the bits toward more-significant bit positions, except for the most-significant bit, which is rotated to the least-significant bit location.

What is rol in assembly language?

Rotate Left (rol)

What is RCL microprocessor?

Description. The left rotate instruction shifts all bits in the register or memory operand specified. The carry flag (CF) is included in the rotation. The most significant bit is rotated to the carry flag, the carry flag is rotated to the least significant bit position, all other bits are shifted to the left.

What are rotate instructions in 8086?

Instructions to perform rotate operationsROL − 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].


2 Answers

Rotates are required for bit shifts across multiple words. When you SHL the lower word, the high-order bit spills out into the carry. To complete the operation, you need to shift the higher word(s) while bringing in the carry to the low-order bit. RCL is the instruction that accomplishes this.

                       High word             Low word         CF Initial          0110 1001 1011 1001   1100 0010 0000 1101    ? SHL low word     0110 1001 1011 1001   1000 0100 0001 1010    1 RCL high word    1101 0011 0111 0011   1000 0100 0001 1010    0 

ROL and ROR are useful for examining a value bit-by-bit in a way that is (ultimately) non-destructive. They can also be used to shunt a bitmask around without bringing in garbage bits.

like image 169
Nietzche-jou Avatar answered Oct 16 '22 04:10

Nietzche-jou


The rotate shift opcodes ROL, RCL, ROR, RCR) are used almost exclusively for hashing and CRC computations. They are pretty arcane and very rarely used.

The shift opcodes (SHL, SHR) are used for fast multiplication by powers of 2, or to move a low byte into a high byte of a large register.

The difference between ROL and SHL is ROL takes the high bit and rolls it around into the low bit position. SHL throws the high bit away and fills the low bit position with zero.

like image 30
dthorpe Avatar answered Oct 16 '22 03:10

dthorpe