HomeARM CourseDay 9
DAY 9 · THE INSTRUCTION SET

The Barrel Shifter — ARM's Secret Weapon

By EcrioniX · Updated Jun 6, 2026

On Day 8 we kept saying "Operand2 can be a shifted register." Today we unlock that. The barrel shifter lets ARM shift or rotate an operand for free, inside the same instruction — a trick that gives RISC ARM surprising muscle.

1. Shifting comes free with the operation

On most CPUs, shifting a value and then using it takes two instructions. ARM puts a barrel shifter right before the ALU, so it can shift Operand2 on the way in — no extra instruction, no extra cycle:

ADD r0, r1, r2, LSL #2 ; r0 = r1 + (r2 << 2) in ONE instruction

That single line computes r0 = r1 + r2×4. The shift happened for free as part of the add. This is the feature that makes ARM assembly feel unusually expressive.

💡 Analogy

It's like a coffee machine that grinds the beans as it brews — you don't grind first in a separate step. The shifter "grinds" the operand on its way into the ALU.

2. The five shift types

TypeNameWhat it does
LSLLogical Shift Leftshift left, fill with 0 → multiply by 2ⁿ
LSRLogical Shift Rightshift right, fill with 0 → unsigned ÷ 2ⁿ
ASRArithmetic Shift Rightshift right, copy the sign bit → signed ÷ 2ⁿ
RORRotate Rightbits that fall off the right wrap to the left
RRXRotate Right eXtendedrotate right by 1 through the carry flag

LSL vs LSR vs ASR — the right-shift difference

The key subtlety: right-shifting a signed number must preserve its sign. LSR fills with zeros (wrong for negatives); ASR copies the sign bit (correct):

value 1111 1000 (= -8 signed) LSR #1 0111 1100 (becomes +124 — sign lost!) ASR #1 1111 1100 (= -4 — correct signed ÷2)

So: LSR for unsigned, ASR for signed division by powers of two.

3. Multiply by a constant — without a multiplier

Since x << n = x × 2ⁿ, you can build many multiplications from shifts and adds — often in a single instruction:

; r0 = r1 * 5 → r1 + r1*4 → r1 + (r1 << 2) ADD r0, r1, r1, LSL #2 ; r0 = r1 * 9 → r1 + r1*8 → r1 + (r1 << 3) ADD r0, r1, r1, LSL #3 ; r0 = r1 * 7 → r1*8 - r1 → (r1 << 3) - r1 RSB r0, r1, r1, LSL #3

Compilers love this: multiplying by small constants becomes one or two fast shift-add instructions instead of a slower multiply. (For variable multiplies there's a real MUL — Day 13.)

4. A pure shift is just MOV

If you only want to shift (no add), use MOV with a shifted operand — there's no separate "SHL" instruction in classic ARM:

MOV r0, r1, LSL #3 ; r0 = r1 * 8 MOV r0, r1, LSR #1 ; r0 = r1 / 2 (unsigned) MOV r0, r1, ASR #1 ; r0 = r1 / 2 (signed)

The shift amount can also be a register (LSL r3) for a variable shift. And with the S suffix, the bit shifted out lands in the carry flag — handy for serial bit work.

✅ The mental model

ARM bolts a barrel shifter onto Operand2, so a shift/rotate rides along with almost any data-processing instruction for free. Shift-left = multiply, shift-right = divide (LSR unsigned, ASR signed), and shift+add builds constant multiplies in one instruction.

🎯 Day 9 takeaways

Quick check

  1. Write one instruction for r0 = r1 × 16.
  2. To divide a signed value by 4, do you use LSR or ASR?
  3. How would you compute r0 = r1 × 3 in one instruction?

FAQ

What is the barrel shifter?

Hardware that shifts/rotates an instruction's second operand before the ALU uses it — for free, in the same cycle and instruction.

LSL vs LSR vs ASR?

LSL = shift left (×2ⁿ). LSR = shift right filling zeros (unsigned ÷). ASR = shift right copying the sign bit (signed ÷).

How do you multiply by a constant?

Shift-and-add: ×5 is ADD r0, r1, r1, LSL #2; ×7 is RSB r0, r1, r1, LSL #3.

Previous
← Day 8: Data-processing instructions

← Back to the full course roadmap