Affected Flags |
|||
C | V | N | Z |
X | X | X |
lsl | : Logical shift left |
lsr | : Logical shift right |
asr | : Arithmetic Shift Right |
rol | : Circular rotation left |
ror | : Circulat rotation right |
These instructions are doing different kind of bit shifting. Instructions terminated by "L" are shifting to left while instructions terminated by "R" are shifting to right. There are 3 kind of bit shiting. Lsl and Lsr are doing simple shifting for unsigned numbers. The Rol and Ror are circular bit shift with the carry. Asr is doing the same thing than Lsr but for signed number : the sign is preserved. There is no Asl instruction as Lsl is working also for signed number. An overflow condition with signed number and Lsl can be detected with the Bso conditionnal jump instruction.
Bits being thrown out by shift instructions are put in the C Flag from the status register.
In many case, shift instructions is a fast way to make multiplication and division by a power of two numbers.
Here are some graphics to explain behavior of these instructions :
rx = shifted register.
ry = Register containing the number of shifts.
Form | Effect |
lsl.b ry,rx |
Logical Shift Left |
lsl.w ry,rx |
|
lsl.l ry,rx | |
lsr.b ry,rx | Logical Shift Right |
lsr.w ry,rx | |
lsr.l ry,rx | |
asr.b ry,rx |
Arithmetic Shift Right |
asr.w ry,rx | |
asr.l ry,rx | |
rol.b ry,rx | Circular Shift Left |
rol.w ry,rx | |
rol.l ry,rx | |
ror.b ry,rx | Circular Shift Right |
ror.w ry,rx | |
ror.l ry,rx |
S = Number of the register containing shift count
D = Shifted register number
Instruction | Op Code |
Cycles | Encoding |
lsl.b ry,rx | 14 | 6 | 00010100 SSSSDDDD |
lsl.w ry,rx | 54 | 6 | 01010100 SSSSDDDD |
lsl.l ry,rx | 94 | 6 | 10010100 SSSSDDDD |
lsr.b ry,rx | 15 | 6 | 00010101 SSSSDDDD |
lsr.w ry,rx | 55 | 6 | 01010101 SSSSDDDD |
lsr.l ry,rx | 95 | 6 | 10010101 SSSSDDDD |
asr.b ry,rx | 13 | 6 | 00010011 SSSSDDDD |
asr.w ry,rx | 53 | 6 | 01010011 SSSSDDDD |
asr.l ry,rx | 93 | 6 | 10010011 SSSSDDDD |
rol.b ry,rx | 16 | 6 | 00010110 SSSSDDDD |
rol.w ry,rx | 56 | 6 | 01010110 SSSSDDDD |
rol.l ry,rx | 96 | 6 | 10010110 SSSSDDDD |
ror.b ry,rx | 17 | 6 | 00010111 SSSSDDDD |
ror.w ry,rx | 57 | 6 | 01010111 SSSSDDDD |
ror.l ry,rx | 97 | 6 | 10010111 SSSSDDDD |
Note: This example is for understanding and can be optimized.
// Example : 8x8 bit graphic 90° rotation
; r0 = pointer to source array of 8 bytes
; r1 = pointer to destination array of 8 bytes
rotate: pushregs r2-r3/r7-r14
move.b (r0),r7
inclr
move.b (r0),r8
inclr
move.b (r0),r9
inclr
move.b (r0),r10
inclr
move.b (r0),r11
inclr
move.b (r0),r12
inclr
move.b (r0),r13
inclr
move.b (r0),r14
movex.n #1,r0
movex.n #8,r3
loop: lsl.b r0, r7
rol.b r0, r2
lsl.b r0, r8
rol.b r0, r2
lsl.b r0, r9
rol.b r0, r2
lsl.b r0, r10
rol.b r0, r2
lsl.b r0, r11
rol.b r0, r2
lsl.b r0, r12
rol.b r0, r2
lsl.b r0, r13
rol.b r0, r2
lsl.b r0, r14
rol.b r0, r2
move.b r2,(r1)
inclr
dec.l #1,r3
bne loop
popregs r2-r3/r7-r14
rts