Assembly Language Programming
Logical Shift Operations
ARM has two logical shift operations, namely LSL (Logical Shift Left) and LSR (Logical Shift Right).
LSL is a logical shift left by 0 to 31 places. The vacated bits at the least significant end of the word are filled with zeros.
Logical Shift Left
LSR is a logical shift right by 0 to 32 places. The vacated bits at the most significant end of the word are filled with zeros.
Logical Shift Right
Consider the following ARM instruction with r1 = 3 and r2 = 5
ADD | r0, r1, r2 | ; r0 := r1 + r2 which is r0 := 3 + 5 = 8 |
Now try the same instruction with a LSL operand, say LSL #3 (logical shift left 3 places which is equivalent to multiplying by 8 (2^3)):
ADD | r0, r1, r2, LSL #3 | ; r0 := r1 + (8 x r2) which is r0:= 3 +(8x5) =43 |
The value of a LSL or a LSR does not have to be a literal. It can instead be sourced from a register (but must still be in the ranges 0 to 31 or 0 to 32 respectively).
Consider a MOV instruction with r1 = 168 and r2 = 3:
MOV | r0, r1, LSR r2 | ; shift the binary value of 168 3 places to the right |
; 168 = 0000 0000 0000 0000 0000 0000 1010 1000 | ||
; shifted 3 places, | ||
; becomes 0000 0000 0000 0000 0000 0000 0001 0101 | ||
; r0 := 21 |
For positive numbers, LSR 3 is the same as dividing by 2 ^ 3 (8).