1.4. Rotation

The rotate instruction shifts all the bits in a register to the right by a given amount. If we take this pattern:

313029282726252423222120191817161514131211109876543210
00000000000000000110111100000000
00006F00

And shift it right 1 bit, we get:

313029282726252423222120191817161514131211109876543210
00000000000000000011011110000000
00003780

Notice that shifting one bit completely changes the hex version of the pattern. If we want to shift by one hex digit, we should shift the original pattern 4 bits.

313029282726252423222120191817161514131211109876543210
00000000000000000000011011110000
000006F0

Tip

Each 4-bits of rotation shifts the hex digits by 1

Bits that shift past position 0 wrap around to bit 31. Here is the pattern shifted 6 more bits:

313029282726252423222120191817161514131211109876543210
11000000000000000000000000011011
C000001B

RORrd, rn, rm / #

ROtate Right. Rotate rn to the right the number of bits in rm or #. Result in rd.

MOV   r1, #0x12      @ r1 = 0000 0000 ... 0000 0001 0010

@Rotate r1 right by 1 bit
ROR   r2, r1, #1     @ r2 = 0010 0000 ... 0000 0000 1001

@Rotate r1 right by 4 bits - one hex char
ROR   r3, r1, #4     @ r3 = 0010 0000 ... 0000 0000 0001
Try sample

There is no rotate left instruction, because we do not need one. A 31-bit rotate is the same as rotating left 1 bit, a 30-bit rotate the same as rotating left 2 bits, …. If we want to rotate left 12 bits, we could instead rotate 20 to the right:

MOV   r1, #0x12      @ r1 = 0x00000012

@Rotate r1 left 12 bits by ROR 20 bits (32 - 20 = 12); answer in r6
ROR   r6, r1, #20    @ r6 = 0x00012000
Try sample

Tip

To rotate left X bits, we rotate 32 - X to the right

You have attempted of activities on this page
Next Section - 1.5. Shifts