Rotate Instruction 

Types of Rotate Instructions

Rotate instructions are unparalleled instructions compared to any high level languages. A rotate operation shifts the bits within a cell without discarding. For example, a rotate right shifts bits to the right. Instead of throwing away the rightmost bit (LSB), it is placed in the leftmost position of the rotated cell.

There are two types of rotate instructions:

 Rotate Without Carry instructions
ROL (ROtate Left)
ROR (ROtate Right)
 ROtate through carry instructions
RCL (Rotate through Carry Left)
RCR (Rotate through Carry Right)

General syntax format

There are two different formats:

ROL/ROR/RCL/RCR r/m, count
ROL/ROR/RCL/RCR r/m, CL

First format directly specifies the count value

Count is an immediate expression between 0 and 31.
If a greater value is specified, Pentium takes only the least significant 5 bits as the count value (MODULUS 32).

Second format specifies count indirectly through CL

Only CL register can be used.
CL contents are not changed.
Useful if count value is known only at the run time as opposed at assembly time.

ROL (Rotate Left) Instruction

a rotate left shifts bits to the left. Instead of throwing away the leftmost bit, it is placed in the righmost position of the rotated cell. In addition, at the same time the leftmost bit is copied into the carry flag.

Fig. m07570.1 The ROL AL, 2 instruction

ROR (Rotate Right) Instruction

a rotate right shifts bits to the right. Instead of throwing away the rightmost bit, it is placed in the leftmost position of the rotated cell. In addition, at the same time the rightmost bit is copied into the carry flag.

Fig. m07570.2 The ROR AL, 2 instruction

RCL (Rotate through Carry Left) Instruction

the rotates through carry are just like the other rotates except that a rotate through carry to the left copies the original contents of the Carry Flag into the rightmost bit of the operand.

Fig. m07570.3 The RCL AL, 2 instruction

RCR (Rotate through Carry right) Instruction

A rotate through carry to the right copies the original contents of the Carry Flag into the rightmost bit of the operand.

Fig. m07570.4 The RCR AL, 2 instruction

Rotates through carry instructions are frequently used in applications that call for moving a field of bits from one operand to another. Accomplishing this feat is simply a matter of repeatedly shifting a bit from the first operand into the Carry flag and rotating that bit out of the carry Flag and into the second operand.

Example: Shifting 64-bit number in EDX:EAX
		mov    CX, N   ;N bit shift
	@1:	shl    EAX, 1  ;moves leftmost bit of EAX to CF
		rcl    EDX, 1  ;CF goes to rightmost bit of EDX
		loop   @1
	; The above can be done using SHLD instruction
		shld   EDX, EAX, N
		shl    EAX, N
 

Example: Code fragment to reverse the contents of AL
 The main idea is:
 

We shift left the bits of AL, once at a time, and put them in BL such that we end up with:

 AL = 0  0  0  0  0  0  0  0
 BL = a0 a1 a2 a3 a4 a5 a6 a7
 We then copy BL to AL
	 push	BX
	 push	CX
	 mov	CX, 8
 @1: shl	AL, 1
	 rcr	BL, 1
	 loop	@1
	 pop	CX
	 pop	BX
 

Exercise: 1
 Initially, the contents of AL is as folows:
a7 a6 a5 a4 a3 a2 a1 a0
 Write a code fragment to set the contents of AL as
a0 a1 a2 a3 a7 a6 a5 a4