Edit this page

rlwinm (Rotate Left Word Immediate then aNd with Mask)

rlwinm is a powerful PPC instruction. But it can also be confusing.

args: dest-register, source-register, rotate-left, mask-left, mask-right

This instruction rotates source to the left, then it ANDs the result with a mask that is all ones from bit mask-left to bit mask-right. All arguments are in the range 0..31. mask-left and mask-right are bit numbers, where the left-most (most significant on big-endian systems) bit is zero, and the rightmost bit is 31. The range is inclusive. Mask-left is the first one-bit following a zero-bit, and mask-right is the last one-bit after a zero. Masks can be of the form 000111...111000 if mask-left is less than mask-right, or the less obvious 111000...000111 if mask-left is greater than mask-right.

Example: If you wanted to take the 8 highest bits from r3 and put them in r4 (but shifted down 8 bits) you would do:

r4 r3 24 8 15 _rlwinm

To see how, pretend that r3 contains 0x12345678. r4 will get clobbered, so it can contain anything, initially. The first thing that happens is that the r3 value gets left-shifted 24 bits, not-sign-extended, to 64 bits. r3, internally, now looks like this:

0x0012345678000000

Since this is a rotate operation, not a shift, and a 32-bit result, nonetheless, we must preserve the bits that fell off the left side of the 32-bit r3. Those fallen bits get resurrected at the bottom of the working word. The working 32-bit word is now:

0x78123456

Next, the mask is applied. The most-significant bit is bit-zero, so masking bits 8 through 15 equates to this mask:

0x00ff0000

Applying the mask, the value is:

0x00120000

Which is exactly the top 8 bits from r3, shifted right 8 bits. This is what gets jammed into r4.

See Also:

herkforth assembler

Edit this page · home ·