Lab 4 - Advanced Arithmetic
Lab 4 - Advanced Arithmetic
Arithmetic
SHL Instruction
SHR Instruction
ROL Instruction
ROR Instruction
SHL Instruction
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
; DL = 20
SHR Instruction
; DL = 40
; DL = 10
CF
; 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
The highest bit is copied into both the Carry flag and
into the lowest bit
CF
mov al,11110000b
rol al,1
; AL = 11100001b
mov dl,3Fh
rol dl,4
; DL = F3h
ROR Instruction
The lowest bit is copied into both the Carry flag and into
the highest bit
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.
Your turn . . .
Indicate the hexadecimal value of AL after each rotation:
mov al,6Bh
ror al,1
rol al,3
a. B5h
b. ADh
Binary Multiplication
Binary Multiplication
We already know that SHL performs unsigned
multiplication efficiently when the multiplier is a power
of 2.
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
IMUL Instruction
DIV Instruction
IDIV Instruction
MUL Instruction
Implied operands:
MUL Examples
The Carry flag
indicates whether or
not the upper half of
the product contains
significant digits.
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
IMUL Instruction
; AX = 00C0h, OF=1
IMUL Examples
27
mov eax,4823424
mov ebx,-423
imul ebx
; EDX:EAX = FFFFFFFF86635D80h, OF=0
Irvine, Kip R.
Assembly
Language
for IntelBased
Computers,
2003.
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
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
;
;
;
;
edx,0
eax,8003h
ecx,100h
ecx
;
;
;
;
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
Signed Arithmetic
Example: eax = (-var1(1*ofvar2)
Expressions
2) + var3
mov
neg
imul
jo
add
jo
eax,var1
eax
var2
TooBig
eax,var3
TooBig
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
sign-extend dividend
EDX = remainder
EBX = right side
begin left side
EDX:EAX = left side
final division
quotient
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