Advanced
Arithmetic
Shift and Rotate Instructions
Shift and Rotate Applications
Multiplication and Division Instructions
Extended Addition and Subtraction
Shift and Rotate Instructions
Logical vs Arithmetic Shifts
SHL Instruction
SHR Instruction
SAL and SAR Instructions
ROL Instruction
ROR Instruction
Logical vs Arithmetic Shifts
A logical shift fills the newly created bit position with
zero:
0
CF
An arithmetic shift fills the newly created bit position
with a copy of the numbers sign bit:
CF
SHL Instruction
The SHL (shift left) instruction performs a logical left
shift on the destination operand, filling the lowest bit
with 0.
0
CF
Operand types for SHL:
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL
(Same for all shift and
rotate instructions)
Fast Multiplication
Shifting left 1 bit multiplies a number by 2
mov dl,5
shl dl,1
Before: 0 0 0 0 0 1 0 1 = 5
After: 0 0 0 0 1 0 1 0 = 10
Shifting left n bits multiplies the operand by 2n
For example, 5 * 22 = 20
mov dl,5
shl dl,2
; DL = 20
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.
0
CF
Shifting right n bits divides the operand by 2n
mov dl,80
shr dl,1
shr dl,2
; DL = 40
; DL = 10
SAL and SAR Instructions
SAL (shift arithmetic left) is identical to SHL.
SAR (shift arithmetic right) performs a right arithmetic
shift on the destination operand.
CF
An arithmetic shift preserves the number's
sign.
mov dl,-80
sar dl,1
sar dl,2
; DL = -40
; DL = -10
Your turn . . .
Indicate the hexadecimal value of AL after each shift:
mov
shr
shl
mov
sar
sar
al,6Bh
al,1
al,3
al,8Ch
al,1
al,3
a.
b.
c.
d.
Your turn . . .
Indicate the hexadecimal value of AL after each shift:
mov
shr
shl
mov
sar
sar
al,6Bh
al,1
al,3
al,8Ch
al,1
al,3
a. 35h
b. A8h
c. C6h
d. F8h
ROL Instruction
ROL (rotate) shifts each bit to the left
The highest bit is copied into both the Carry flag and
into the lowest bit
No bits are lost
CF
mov al,11110000b
rol al,1
; AL = 11100001b
mov dl,3Fh
rol dl,4
; DL = F3h
ROR Instruction
ROR (rotate right) shifts each bit to the right
The lowest bit is copied into both the Carry flag and into
the highest bit
No bits are lost
CF
mov al,11110000b
ror al,1
; AL = 01111000b
mov dl,3Fh
ror dl,4
; DL = F3h
Your turn . . .
a.
b.
13
mov al,6Bh
ror al,1
rol al,3
Irvine, Kip R.
Assembly
Language
for IntelBased
Computers,
2003.
Indicate the hexadecimal value of AL after each rotation:
Your turn . . .
Indicate the hexadecimal value of AL after each rotation:
mov al,6Bh
ror al,1
rol al,3
a. B5h
b. ADh
Shift and Rotate Applications
Shifting Multiple Doublewords
Binary Multiplication
Displaying Binary Bits
Isolating a Bit String
Binary Multiplication
We already know that SHL performs unsigned
multiplication efficiently when the multiplier is a power
of 2.
You can factor any binary number into powers of 2.
For example, to multiply EAX * 36, factor 36 into 32 + 4
and use the distributive property of multiplication to carry
out the operation:
EAX * 36
= EAX * (32 + 4)
= (EAX * 32)+(EAX * 4)
mov
mov
shl
shl
add
eax,123
ebx,eax
eax,5
ebx,2
eax,ebx
; mult by 25
; mult by 22
16
Your turn . . .
Multiply AX by 26, using shifting and addition instructions.
Hint: 26 = 16 + 8 + 2.
Your turn . . .
Multiply AX by 26, using shifting and addition instructions.
Hint: 26 = 16 + 8 + 2.
mov ax,2
mov dx,ax
shl dx,4
push dx
mov dx,ax
shl dx,3
shl ax,1
add ax,dx
pop dx
add ax,dx
; test value
; AX * 16
; save for later
;
;
;
;
;
AX * 8
AX * 2
AX * 10
recall AX * 16
AX * 26
Multiplication and Division
Instructions
MUL Instruction
IMUL Instruction
DIV Instruction
Signed Integer Division
CBW, CWD, CDQ Instructions
IDIV Instruction
Implementing Arithmetic Expressions
MUL Instruction
The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or
32-bit operand by either AL, AX, or EAX.
The instruction formats are:
MUL r/m8
MUL r/m16
MUL r/m32
Implied operands:
MUL Examples
The Carry flag
indicates whether or
not the upper half of
the product contains
significant digits.
12345h * 1000h, using 32-bit operands:
mov eax,12345h
mov ebx,1000h
mul ebx
; EDX:EAX = 0000000012345000h, CF=0
Your turn . . .
What will be the hexadecimal values of DX, AX, and the Carry
flag after the following instructions execute?
mov ax,1234h
mov bx,100h
mul bx
Your turn . . .
What will be the hexadecimal values of DX, AX, and the Carry
flag after the following instructions execute?
mov ax,1234h
mov bx,100h
mul bx
DX = 0012h, AX = 3400h, CF = 1
Your turn . . .
What will be the hexadecimal values of EDX, EAX, and the Carry
flag after the following instructions execute?
mov eax,00128765h
mov ecx,10000h
mul ecx
Your turn . . .
What will be the hexadecimal values of EDX, EAX, and the Carry
flag after the following instructions execute?
mov eax,00128765h
mov ecx,10000h
mul ecx
EDX = 00000012h, EAX = 87650000h, CF = 1
IMUL Instruction
IMUL (signed integer multiply ) multiplies an 8-, 16-, or
32-bit signed operand by either AL, AX, or EAX
Preserves the sign of the product by sign-extending it
into the upper half of the destination register
Example: multiply 48 * 4, using 8-bit operands:
mov al,48
mov bl,4
imul bl
; AX = 00C0h, OF=1
OF=1 because AH is not a sign extension of AL.
IMUL Examples
OF=0 because EDX is a sign extension of EAX.
27
mov eax,4823424
mov ebx,-423
imul ebx
; EDX:EAX = FFFFFFFF86635D80h, OF=0
Irvine, Kip R.
Assembly
Language
for IntelBased
Computers,
2003.
Multiply 4,823,424 * 423:
Your turn . . .
28
mov ax,8760h
mov bx,100h
imul bx
Irvine, Kip R.
Assembly
Language
for IntelBased
Computers,
2003.
What will be the hexadecimal values of DX, AX, and the Carry
flag after the following instructions execute?
Your turn . . .
What will be the hexadecimal values of DX, AX, and the Carry
flag after the following instructions execute?
mov ax,8760h
mov bx,100h
imul bx
DX = FF87h, AX = 6000h, OF = 1
DIV Instruction
The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and
32-bit division on unsigned integers
A single operand is supplied (register or memory operand), which
is assumed to be the divisor
Instruction formats:
DIV r/m8
DIV r/m16
DIV r/m32
Default Operands:
DIV Examples
Divide 8003h by 100h, using 16-bit operands:
mov
mov
mov
div
dx,0
ax,8003h
cx,100h
cx
;
;
;
;
clear dividend, high
dividend, low
divisor
AX = 0080h, DX = 3
Same division, using 32-bit operands:
mov
mov
mov
div
edx,0
eax,8003h
ecx,100h
ecx
;
;
;
;
clear dividend, high
dividend, low
divisor
EAX = 00000080h, DX = 3
Your turn . . .
What will be the hexadecimal values of DX and
AX after the following instructions execute? Or, if
divide overflow occurs, you can indicate that as
your answer:
mov
mov
mov
div
dx,0087h
ax,6000h
bx,100h
bx
Your turn . . .
What will be the hexadecimal values of DX and
AX after the following instructions execute? Or, if
divide overflow occurs, you can indicate that as
your answer:
mov
mov
mov
div
dx,0087h
ax,6000h
bx,100h
bx
DX = 0000h, AX = 8760h/
Your turn . . .
What will be the hexadecimal values of DX and
AX after the following instructions execute? Or, if
divide overflow occurs, you can indicate that as
your answer:
mov
mov
mov
div
dx,0087h
ax,6002h
bx,10h
bx
Your turn . . .
What will be the hexadecimal values of DX and
AX after the following instructions execute? Or, if
divide overflow occurs, you can indicate that as
your answer:
mov
mov
mov
div
dx,0087h
ax,6002h
bx,10h
bx
Divide Overflow
Unsigned Arithmetic
Expressions
Some good reasons to learn how to implement integer
expressions:
Learn how do compilers do it
Test your understanding of MUL, IMUL, DIV, IDIV
Check for overflow (Carry and Overflow flags)
Example: var4 = (var1 + var2) * var3
; Assume unsigned operands
mov eax,var1
add eax,var2
; EAX = var1 + var2
mul var3
; EAX = EAX * var3
jc
TooBig
; check for carry
mov var4,eax
; save product
Signed Arithmetic
Example: eax = (-var1(1*ofvar2)
Expressions
2) + var3
mov
neg
imul
jo
add
jo
eax,var1
eax
var2
TooBig
eax,var3
TooBig
; check for overflow
; check for overflow
Example: var4 = (var1 * 5) / (var2 3)
mov
mov
imul
mov
sub
idiv
mov
eax,var1
ebx,5
ebx
ebx,var2
ebx,3
ebx
var4,eax
; left side
; EDX:EAX = product
; right side
; EAX = quotient
Signed Arithmetic
Expressions
2) / (-var2 % var3);
Example: var4 = (var1(2* of
-5)
mov
neg
cdq
idiv
mov
mov
imul
idiv
mov
eax,var2
eax
var3
ebx,edx
eax,-5
var1
ebx
var4,eax
; begin right side
;
;
;
;
;
;
;
sign-extend dividend
EDX = remainder
EBX = right side
begin left side
EDX:EAX = left side
final division
quotient
Sometimes it's easiest to calculate the right-hand term of an
expression first.
Your turn . . .
Implement the following expression using signed
32-bit integers:
eax = (ebx * 20) / ecx
Your turn . . .
Implement the following expression using signed
32-bit integers:
eax = (ebx * 20) / ecx
mov eax,20
imul ebx
idiv ecx
Your turn . . .
Implement the following expression using signed
32-bit integers. Do not modify any variables
other than var3:
var3 = (var1 * -var2) / (var3 ebx)
Your turn . . .
Implement the following expression using signed
32-bit integers. Do not modify any variables
other than var3:
var3 = (var1 * -var2) / (var3 ebx)
mov
mov
neg
imul
mov
sub
idiv
mov
eax,var1
edx,var2
edx
edx
ecx,var3
ecx,ebx
ecx
var3,eax
; left side: EDX:EAX
; EAX = quotient