Lecture # 14 - Shift
Lecture # 14 - Shift
A logical shift fills the newly created bit position with zero.
An arithmetic shift fills the newly created bit position with a copy of the number’s sign
bit.
The SHL (shift left) instruction performs a logical left shift on the destination operand,
filling the lowest bit with 0.
The first operand in SHL is the destination and the second is the shift count:
SHL destination,count
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL
Formats shown here also apply to the SHR, SAL, SAR, ROR, ROL, RCR, and RCL
instructions.
mov dl,5
shl dl,1
Shifting left n bits multiplies the operand by2��
For example, 5 * 22 = 20
mov dl,5
shl dl,2 ;DL = 20, CF = 0
SHR Instruction
The SHR (shift right) instruction performs a logical right shift on the destination operand.
The highest bit position is filled with a zero.
Application: Division
Shifting right n bits divides the operand by 2��
SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand.
Applications:
1. Signed Division
An arithmetic shift preserves the number's sign.
mov dl,-80 ; DL = 10110000b
sar dl,1 ; DL = 11011000b = -40, CF = 0
sar dl,2 ; DL = 11110110b = -10, CF = 0
2. Sign-Extend
Suppose AX contains a signed integer and you want to extend its sign into EAX. First
shift EAX 16 bits to the left, then shift it arithmetically 16 bits to the right: mov
ax,-128 ; EAX = ????FF80h
shl eax,16 ; EAX = FF800000h
sar eax,16 ; EAX = FFFFFF80h
ROL Instruction
The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into
the Carry flag and the lowest bit position.
Example:
mov al,11110000b
rol al,1 ;AL = 11100001b, CF = 1
ROR Instruction
The ROR (rotate right) instruction shifts each bit to the right and copies the lowest bit
into the Carry flag and the highest bit position.
No bits are lost.
Example:
mov al,11110000b
ror al,1 ; AL = 01111000b, CF = 0
RCL Instruction
The RCL (rotate carry left) instruction shifts each bit to the left, copies the Carry flag to
the LSB, and copies the MSB into the Carry flag.
Example:
RCR Instruction
The RCR (rotate carry right) instruction shifts each bit to the right, copies the Carry flag
into the MSB, and copies the LSB into the Carry flag.
Example:
SHLD Instruction
The SHLD (shift left double) instruction shifts a destination operand a given number of
bits to the left.
The bit positions opened up by the shift are filled by the most significant bits of the
source operand.
Only the destination is modified, not the source.
Syntax:
Operand types:
SHLD reg16,reg16,CL/imm8
SHLD mem16,reg16,CL/imm8
SHLD reg32,reg32,CL/imm8
SHLD mem32,reg32,CL/imm8
Example:
.data
wval WORD 9BA6h
.code
mov ax,0AC36h
shld wval,ax,4
SHRD Instruction
The SHRD (shift right double) instruction shifts a destination operand a given number of
bits to the right.
The bit positions opened up by the shift are filled by the least significant bits of the
source operand.
Example:
mov ax,234Bh
mov dx,7654h
shrd ax,dx,4
MUL Instruction
The MUL (unsigned multiply) instruction comes in three versions:
The multiplier and multiplicand must always be the same size, and the product is twice
their size.
The three formats accept register and memory operands, but not immediate operands:
MUL reg/mem8
MUL reg/mem16
MUL reg/mem32
MUL sets the Carry and Overflow flags if the upper half of the product is not equal to
zero.
mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX = 0000000012345000h, CF=OF=0
DIV Instruction
The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit unsigned integer
division.
The single register or memory operand is the divisor.
The formats are
DIV reg/mem8
DIV reg/mem16
DIV reg/mem32
Example1: Divide AX = 8003h by CX = 100h, using 16-bit operands
mov dx,0 ;clear dividend, high
mov ax,8003h ;dividend, low
mov cx,100h ;divisor
div cx ; AX = 0080h, DX = 0003h (Remainder)