Chapter 7 - Integer Arithmetic
Chapter 7 - Integer Arithmetic
7th Edition
Kip R. Irvine
(c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide show for your personal use, or for
use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Chapter Overview
1
Shift and Rotate Instructions
Logical Shift
0
CF
2
Arithmetic Shift
CF
SHL Instruction
• The SHL (shift left) instruction performs a
logical left shift on the destination operand,
filling the lowest bit with 0.
3
Bitwise Multiplication
SHR Instruction
0
CF
Bitwise Division
Shifting right n bits divides the operand by 2n
mov dl,80
shr dl,1 ; DL = 40
shr dl,2 ; DL = 10
8
4
SAL and SAR Instructions
CF
Signed Division
• Use SAR to divide a signed operand by a power of 2
10
5
Your turn . . .
mov al,6Bh
shr al,1 a.35h
shl al,3 b.A8h
mov al,8Ch 10001100
sar al,1 c.C6h
sar al,3 d.F8h
11
ROL Instruction
CF
6
Multiple Rotations
• CF contains the last bit that rotated out of the MSB
mov al,00100000b
rol al,3 ; CF = 1, AL = 00000001b
ROR Instruction
CF
mov al,11110000b
ror al,1 ; AL = 01111000b
mov dl,3Fh
ror dl,4 ; DL = F3h
14
7
Your turn . . .
mov al,6Bh
ror al,1 a.B5h
rol al,3 b.ADh
15
RCL Instruction
clc ; CF = 0
mov bl,88h ; CF,BL = 0 10001000b
rcl bl,1 ; CF,BL = 1 00010000b
rcl bl,1 ; CF,BL = 0 00100001b
16
8
Recovering a Bit from the Carry Flag
17
RCR Instruction
stc ; CF = 1
mov ah,10h ; CF,AH = 1 00010000b
rcr ah,1 ; CF,AH = 0 10001000b
18
9
Your turn . . .
stc
mov al,6Bh
rcr al,1 a.B5h
rcl al,3 b.AEh
19
Left ROL
Without
Carry
Right ROR
Rotate
Left RCL
With Carry
Right RCR
20
10
Signed Overflow
21
SHLD Instruction
22
11
SHLD Example
Shift count of 1:
mov al,11100000b
mov bl,10011101b
shld al,bl,1
al
23
.data wval AX
wval WORD 9BA6h Before: 9BA6 AC36
.code
After: BA6A AC36
mov ax,0AC36h
shld wval,ax,4
24
12
SHRD Instruction
25
SHRD Example
Shift count of 1:
mov al,11000001b
mov bl,00011101b
shrd al,bl,1
26
13
Another SHRD Example
mov ax,234Bh DX AX
mov dx,7654h Before: 7654 234B
27
Your turn . . .
mov ax,7C36h
mov dx,9FA6h
shld dx,ax,4 ; DX = FA67h
shrd dx,ax,8 ; DX = 36FAh
28
14
What's Next
01001100 1
30
15
Shifting Multiple Bytes (Step 2)
01001100 1 10011001
CF [esi+1] CF
11001100 1
CF [esi] CF
11001100 1
16
Shifting Multiple Doublewords
33
Illustration
array[esi+4]
array[esi] array[esi+8]
C mid dword
rcr array[esi
+ 4],1 C low dword
rcr
array[esi],1
34
17
Binary Multiplication
• mutiply 123 * 36
36 = (00100100)2 = 25 + 22
123 * 36 = 123 * 25 + 123 * 22
= 123 * SHL 5 + 123 SHL 2
35
Your turn . . .
mov dx,ax
shl dx,4 ; AX * 16
push edx ; save for later
mov dx,ax
shl dx,3 ; AX * 8
shl ax,1 ; AX * 2
add ax,dx ; AX * 10
pop edx ; recall AX * 16
add ax,dx ; AX * 26
36
18
Displaying Binary Bits
Algorithm: Shift MSB into the Carry flag; If CF = 1, append
a "1" character to a string; otherwise, append a "0"
character. Repeat in a loop, 32 times.
.data
buffer BYTE 32 DUP(0),0
.code
mov ecx,32
mov esi,OFFSET buffer
L1: shl eax,1
mov BYTE PTR [esi],'0'
jnc L2
mov BYTE PTR [esi],'1'
L2: inc esi
loop L1
37
0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 0
19
What's Next
39
• MUL Instruction
• IMUL Instruction
• Measuring Program Execution Time
• DIV Instruction
• Signed Integer Division
• Implementing Arithmetic Expressions
40
20
MUL Instruction
8 bits
Overflow is impossible
OF/CF = 0 if the upper half of the product is 0.
OF/CF = 1 if the upper half of the product is not 0.
41
MUL Examples
mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX = 0000000012345000h, CF=0
21
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 1234 AX
mul bx 0100 BX
DX 0012 3400 AX
DX = 0012h, AX = 3400h, CF = 1
43
Your turn . . .
mov eax,00128765h
mov ecx,10000h
mul ecx
44
22
Using MUL in 64-Bit Mode
64 bits
RAX r/m RDX RAX
mov rax,0FFFF0000FFFF0000h
mov rbx,2
mul rbx ; RDX:RAX =
0000000000000001FFFE0001FFFE0000
45
IMUL Instruction
46
23
Single-Operand Formats
8 bits
Overflow is impossible
OF/CF = 0 if the upper half of the product is a
sign extension of the lower half.
47
IMUL reg16,reg/mem16
IMUL reg16,imm8 16-bit format
IMUL reg16,imm16
IMUL reg32,reg/mem32
IMUL reg32,imm8 32-bit format
IMUL reg32,imm32
24
Three-Operand Formats (32-Bit Mode)
• Store the product in the first operand (must be a reg.)
IMUL reg16,reg/mem16,imm8
16-bit format
IMUL reg16,reg/mem16,imm16
IMUL reg32,reg/mem32,imm8
32-bit format
IMUL reg32,reg/mem32,imm32
mov rax,-4
mov rbx,4
imul rbx ; RDX = 0FFFFFFFFFFFFFFFFh, RAX = -16
25
IMUL Examples
Your turn . . .
26
Measuring Program Execution Time
• GetMseconds procedure in the Irvine32 library gets
the number of system milliseconds that have elapsed
since midnight.
• Call GetMseconds twice to measure the execution
time of some procedure
call GetMseconds ; get start time
mov startTime,eax
. elapsed
call ProcedureToTest time
.
call GetMseconds ; get stop time
sub eax,startTime ; calculate the elapsed time
mov procTime,eax ; save the elapsed time
53
54
27
DIV Instruction
DIV Examples
56
28
Your turn . . .
mov dx,0087h
mov ax,6000h
mov bx,100h
div bx
DX = 0000h, AX = 8760h
57
Your turn . . .
58
29
Signed Integer Division (IDIV
( )
10001111
Sign
Extension
(manual)
11111111 10001111
59
.data
dwordVal SDWORD -101 ; FFFFFF9Bh
.code
mov eax,dwordVal
cdq ; EDX:EAX = FFFFFFFFFFFFFF9Bh
60
30
IDIV Instruction
mov al,-48
cbw ; extend AL into AH
mov bl,5
idiv bl ; AL = -9, AH = -3
61
IDIV Examples
62
31
Your turn . . .
63
Divide Overflow
mov ax,1000h
mov bl,10h
div bl ; AL can not hold 100h
64
32
Suggestion
mov eax,1000h
cdq
mov ebx,10h
div ebx ; EAX = 00000100h
65
mov ax,dividend
mov bl,divisor
cmp bl,0
je NoDividedZero
div bl
.
.
NoDividedZero: ; display error msg
66
33
Implementing 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
Implementation Example
68
34
Signed Arithmetic Expressions
Example: var4 = (var1 * -5) / (-var2 % var3);
mov eax,var2 ; begin right side
neg eax
cdq ; sign-extend dividend
idiv var3 ; EDX = remainder
mov ebx,edx ; EBX = right side
mov eax,-5 ; begin left side
imul var1 ; EDX:EAX = left side
idiv ebx ; final division
mov var4,eax ; quotient
Your turn . . .
mov eax,20
imul ebx
idiv ecx
70
35
Your turn . . .
push edx
push eax ; EAX needed later
mov eax,ecx
imul edx ; left side: EDX:EAX
pop ebx ; saved value of EAX
idiv ebx ; EAX = quotient
pop edx ; restore EDX, ECX
71
Your turn . . .
mov eax,var1
mov edx,var2
neg edx
imul edx ; left side: EDX:EAX
mov ecx,var3
sub ecx,ebx
idiv ecx ; EAX = quotient
mov var3,eax
72
36
What's Next
73
C C C
74
37
ADC Instruction
mem,imm
ADC Example
DL CF DL
adc dl,0 00000000 + 00000000 + 1 00000001
76
38
Extended Addition Example
Length = ECX ESI
…
EDI
…
+
EBX
…
clc ; clear the Carry flag
L1: mov,al,[esi] ; get the first integer
adc,al,[edi] ; add the second integer
pushfd ; save the Carry flag
mov [ebx],al ; store partial sum
add esi,1 ; advance all 3 pointers
add edi,1
add ebx,1
popfd ; restore the Carry flag
loop L1 ; repeat the loop 77
SBB Instruction
78
39
Extended Subtraction Example
• Task: Subtract 2 from EDX:EAX
• Starting value of EDX:EAX: 0000000700000001h
• Subtract the lower 32 bits first, setting the Carry flag.
• Subtract the upper 32 bits, and include the Carry flag.
What's Next
40
ASCII Decimal
Unpacked Decimal
03 04 00 02
one byte for each digit (storing
the binary value of the digit)
82
41
Relationship Between ASCII Decimal and
Unpacked Decimal
‘7’
(37h)
= AX
AAA
AL 6Ch 0102h
83
84
42
AAA Example
43
AAM Instruction
AAD Instruction
44
What's Next
89
90
45
DAA Instruction
Result AL =
cked DAA
al = packed add pa in AL packed decimal
a l
decimal decim of the result
91
DAA Examples
46
DAS Instruction
DAS AL =
cked
al = packed s ub pa result packed decimal
al
decimal decim of the result
93
Summary
47