Shift and Rotate
Instructions
CH#7-(A_KHALIQ)
SHL (Shift Left) Instruction
Shifts each bit to the left, filling the lowest bit with 0. The
highest bit is moved to the carry flag. The bit that was in
carry flag is lost.
Permitted operands:
SHL dest,1
SHL dest,CL
SHL dest, immed8 ;80286 and higher
Desination operand can be 8, 16 or 32 bit register or memory
operand. CH#7-(A_KHALIQ)
SHL (Shift Left) Example
mov dl,1 ;dl =1
shl dl,1 ;dl=2
shl dl,1 ;dl=4
shl dl,1 ;dl=8
CH#7-(A_KHALIQ)
SHR (Shift Right) Instruction
Shift each bit to the right, replacing the highest bit with a 0. The
lowest bit is copied into the carry flag and the bit that was in
carry flag is lost.
mov dl,32 ;dl =32
shr dl,1 ;dl=16
shr dl,1 ;dl=8
shr dl,1 ;dl=4
CH#7-(A_KHALIQ)
SAL (Shift Airthmatic Left)
– Similar to SHL
CH#7-(A_KHALIQ)
SAR Shift Arithmetic Right
(for signed numbers)
Shift each instruction to the left and make a copy of the sign bit.
mov al,0f0h ;al = 11110000 (-16)
sar al,1 ;al = 11111000 (-8)
mov dx,8000h dx = -32768
sar dx,5 dx = -1024 (divide by 32)
CH#7-(A_KHALIQ)
Rotate Instructions
ROL (rotate Left)
Moves each bit to the left. The highest bit is copied both
into carry flag and into the lowest bit.
mov al,40h ;al = 01000000b
rol al,1 ;al = 10000000b CF = 0
rol al,1 ;al = 00000001b CF =1
rol al,1 ;al = 00000010b CF = 0
rol al,1 ;al = 00000100b CF = 0
rol al,1 ;al = 00001000b CF = 0
CH#7-(A_KHALIQ)
ROR (Rotate Right)
Moves each bit to the right. The lowest bit is copied into the
carry flag and into the highest bit at the same time.
mov al,01h ;al = 00000001b
ror al,1 ;al = 10000000b CF = 1
ror al,1 ;al = 01000000b CF = 0
CH#7-(A_KHALIQ)
RCL (Rotate Carry Left)
Shifts each bit to the left and copies the highest bit into the
Carry Flag, Carry Flag previous value is copied into lowest
bit of result.
CLC ;CF = 0
mov bl,88h ;BL = 10001000b
rcl bl,1 ;BL = 00010000b CF = 1
rcl bl,1 ;BL = 00100001b CF = 0
CH#7-(A_KHALIQ)
RCR (Rotate Carry Right)
Shift each bit to the right and copies the lowet bit into carry flag.
The carry Flag copies into the highest bit of the result.
stc ; CF =1
mov ah,10h ;AH = 00010000b, CF = 1
rcr ah,1 ;AH = 10001000b, CF = 0
CH#7-(A_KHALIQ)