0% found this document useful (0 votes)
146 views

Chapter 7 - Integer Arithmetic

The document discusses various shift and rotate instructions in x86 assembly language including logical and arithmetic shifts, multiplication and division using shifts, and instructions like SHL, SAR, ROL, ROR, RCL, RCR, and SHLD. It provides examples of how each instruction works and the hexadecimal results.

Uploaded by

John David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views

Chapter 7 - Integer Arithmetic

The document discusses various shift and rotate instructions in x86 assembly language including logical and arithmetic shifts, multiplication and division using shifts, and instructions like SHL, SAR, ROL, ROR, RCL, RCR, and SHLD. It provides examples of how each instruction works and the hexadecimal results.

Uploaded by

John David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Assembly Language for x86 Processors

7th Edition
Kip R. Irvine

Chapter 7: Integer Arithmetic

Slides prepared by the author


Revision date: 5/3/2015

(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

• Shift and Rotate Instructions


• Shift and Rotate Applications
• Multiplication and Division Instructions
• Extended Addition and Subtraction
• ASCII and Unpacked Decimal Arithmetic
• Packed Decimal Arithmetic

1
Shift and Rotate Instructions

• Logical and Arithmetic Shifts


• SHL Instruction
• SHR Instruction
• SAL and SAR Instructions
• ROL Instruction
• ROR Instruction
• RCL and RCR Instructions
• Signed Overflow
• SHLD/SHRD Instructions
3

Logical Shift

• A logical shift fills the newly created bit


position with zero:

0
CF

2
Arithmetic Shift

• An arithmetic shift fills the newly created bit position


with a copy of the number’s 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.

• Operand types for SHL:


SHL reg,imm8 (Same for all shift and
SHL mem,imm8 rotate instructions)
SHL reg,CL
SHL mem,CL Number of bits
to be shifted 6

3
Bitwise Multiplication

Shifting left 1 bit multiplies a number by 2

mov dl,5 Before: 00000101 =5


shl dl,1 After: 00001010 = 10

Shifting left n bits multiplies the operand by 2n


For example, 10 * 22 = 40
mov dl,10 ; 00001010
shl dl,2 ; 00101000

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

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

• 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 ; DL = -40
sar dl,2 ; DL = -10
9

Signed Division
• Use SAR to divide a signed operand by a power of 2

mov dl,-128 ; DL = 10000000b


sar dl,3 ; DL = 11110000b -16

Signed-Extended AX into EAX


• Want to extend AX’s sign into EAX

mov ax,-128 ; EAX = ????FF80h


shl eax,16 ; EAX = FF800000h
sar eax,16 ; EAX = FFFFFF80h

10

5
Your turn . . .

Indicate the hexadecimal value of AL after each


shift:
01101011

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

• 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,40h ; AL = 01000000b


rol al,1 ; AL = 10000000b
rol al,1 ; AL = 00000001b, CF = 1
rol al,1 ; AL = 00000010b, CF = 0
12

6
Multiple Rotations
• CF contains the last bit that rotated out of the MSB

mov al,00100000b
rol al,3 ; CF = 1, AL = 00000001b

Exchanging Groups of Bits


• Rotating an integer by four bits
mov al,26h
rol al,4 ; AL = 62h
mov ax,6A4Bh
rol ax,4 ; AX = A4B6h
rol ax,4 ; AX = 4B6Ah
13

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
14

7
Your turn . . .

Indicate the hexadecimal value of AL after each


rotation:

mov al,6Bh
ror al,1 a.B5h
rol al,3 b.ADh

15

RCL Instruction

• RCL (rotate carry left) shifts each bit to the left


• Copies the Carry flag to the least significant bit
• Copies the most significant bit to the Carry flag
CF

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

• Suppose that a bit was previously shifted right


to CF for examination
• RCL can recover the original value
.data
testval BYTE 01101010b
.code
shr testval,1 ; shifts LSB into CF
jc exit ; exits if CF is set
rcl testval,1 ; else restore the number
CF
0
CF

17

RCR Instruction

• RCR (rotate carry right) shifts each bit to the right


• Copies the Carry flag to the most significant bit
• Copies the least significant bit to the Carry flag
CF

stc ; CF = 1
mov ah,10h ; CF,AH = 1 00010000b
rcr ah,1 ; CF,AH = 0 10001000b

18

9
Your turn . . .

Indicate the hexadecimal value of AL after each


rotation:

stc
mov al,6Bh
rcr al,1 a.B5h
rcl al,3 b.AEh

19

Shift/Rotate Family Tree


Left SHL
Logic
Right SHR
Shift
Left SAL
Arithmetic
Right SAR

Left ROL
Without
Carry
Right ROR
Rotate
Left RCL
With Carry
Right RCR
20

10
Signed Overflow

• OF is set if the result is out of range (the


number’s sign is reversed)

mov al,+127 ; AL = 01111111b


rol al,1 ; OF = 1, AL = 11111110b

mov al-128 ; AL = 10000000b


shr al,1 ; OF = 1, AL = 01000000b

21

SHLD 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
• The source operand is not affected
• Syntax:
SHLD destination, source, count
• Operand types:

SHLD reg16/32, reg16/32, imm8/CL


SHLD mem16/32, reg16/32, imm8/CL

22

11
SHLD Example

Shift count of 1:
mov al,11100000b
mov bl,10011101b
shld al,bl,1
al

23

Another SHLD Example

Shift wval 4 bits to the left and replace its


lowest 4 bits with the high 4 bits of AX:

.data wval AX
wval WORD 9BA6h Before: 9BA6 AC36
.code
After: BA6A AC36
mov ax,0AC36h
shld wval,ax,4

24

12
SHRD 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
• The source operand is not affected
• Syntax:
SHRD destination, source, count
• Operand types:

SHRD reg16/32, reg16/32, imm8/CL


SHRD mem16/32, reg16/32, imm8/CL

25

SHRD Example

Shift count of 1:
mov al,11000001b
mov bl,00011101b
shrd al,bl,1

26

13
Another SHRD Example

Shift AX 4 bits to the right and replace its


highest 4 bits with the low 4 bits of DX:

mov ax,234Bh DX AX
mov dx,7654h Before: 7654 234B

shrd ax,dx,4 After: 7654 4234

27

Your turn . . .

Indicate the hexadecimal values of each


destination operand:

mov ax,7C36h
mov dx,9FA6h
shld dx,ax,4 ; DX = FA67h
shrd dx,ax,8 ; DX = 36FAh

28

14
What's Next

• Shift and Rotate Instructions


• Shift and Rotate Applications
• Shifting Multiple Doublewords
• Binary Multiplication
• Displaying Binary Bits
• Extracting File Date Fields
• Multiplication and Division Instructions
• Extended Addition and Subtraction
• ASCII and Unpacked Decimal Arithmetic
• Packed Decimal Arithmetic
29

Shifting Multiple Doublewords

• An extended-precision integer can be stored


as an array of bytes, words, or doublewords.
• The integer will be stored in little-endian order
• To shift an array of bytes 1 bits to the right
[esi+2] [esi+1] [esi]
10011001 10011001 10011001

Step 1: Shift the highest byte at [esi+2] to the right:


[esi+2] CF

01001100 1
30

15
Shifting Multiple Bytes (Step 2)

Rotate the value at [esi+1] to the right:


[esi+2] CF [esi+1] CF

01001100 1 10011001

CF [esi+1] CF

11001100 1

Filling the highest bit with the value of CF


and shifting the lowest bit to CF 31

Shifting Multiple Bytes (Step 3)

Rotate the value at [esi] to the right:


[esi+2] [esi+1] CF [esi] CF

01001100 11001100 1 10011001

CF [esi] CF

11001100 1

Filling the highest bit with the value of CF


and shifting the lowest bit to CF 32

16
Shifting Multiple Doublewords

• Shift an array of 3 doublewords 1 bit to the


right
.data
ArraySize = 3
array DWORD ArraySize DUP(99999999h) ; 1001 1001...
.code
mov esi,0
shr array[esi + 8],1 ; high dword
rcr array[esi + 4],1 ; middle dword, include Carry
rcr array[esi],1 ; low dword, include Carry

33

Illustration
array[esi+4]
array[esi] array[esi+8]

low dword mid dword high dword In memory

high dword mid dword low dword Interpretation


shr array[esi …
+ 8],1
high dword

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

EAX * 36 mov eax,123


= EAX * (32 + 4) mov ebx,eax
= (EAX * 32)+(EAX * 4)
shl eax,5 ; mult by 25
shl ebx,2 ; mult by 22
add eax,ebx

35

Your turn . . .

Multiply AX by 26, using shifting and addition


instructions. Hint: 26 = 16 + 8 + 2.
mov ax,2 ; test value

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

Extracting File Date Fields

• The MS-DOS file date field packs the year,


month, and day into 16 bits:
DH DL

0 0 1 0 0 1 1 0 0 1 1 0 1 0 1 0

Field: Year Month Day


Bit numbers: 9-15 5-8 0-4

To extract the month field:


mov ax,dx ; make a copy of DX
shr ax,5 ; shift right 5 bits
and al,00001111b ; clear bits 4-7
mov month,al ; save in month variable
38

19
What's Next

• Shift and Rotate Instructions


• Shift and Rotate Applications
• Multiplication and Division Instructions
• Extended Addition and Subtraction
• ASCII and Unpacked Decimal Arithmetic
• Packed Decimal Arithmetic

39

Multiplication and Division Instructions

• MUL Instruction
• IMUL Instruction
• Measuring Program Execution Time
• DIV Instruction
• Signed Integer Division
• Implementing Arithmetic Expressions

40

20
MUL Instruction

8 bits

MUL reg/mem8 AL r/m AH AL


16 bits
MUL reg/mem16 AX r/m DX AX
32 bits
MUL reg/mem32 EAX r/m EDX EAX

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

2000h * 100h, using 16-bit operands:


.data The Carry flag
val1 WORD 2000h indicates whether or
val2 WORD 100h not the upper half of
.code the product contains
significant digits.
mov ax,val1 ; AX = 2000h
mul val2 ; DX:AX = 00200000h, CF=1

12345h * 1000h, using 32-bit operands:

mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX = 0000000012345000h, CF=0

EAX r/m EDX EAX 42

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 . . .

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

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

• 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 (與MUL不同之處)
• Support three formats: one operand,
two operands, and three operands

46

23
Single-Operand Formats

8 bits

IMUL reg/mem8 AL r/m AH AL


16 bits
IMUL reg/mem16 AX r/m DX AX
32 bits
IMUL reg/mem32 EAX r/m EDX EAX

Overflow is impossible
OF/CF = 0 if the upper half of the product is a
sign extension of the lower half.
47

Two-Operand Formats (32-Bit Mode)


• Store the product in the first operand (must be a reg.)

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

• Truncate the product to the length of the


destination
• OF and CF are set if significant digits are lost
48

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

• Truncate the product to the length of the


destination
• OF and CF are set if significant digits are lost
49

Using IMUL in 64-Bit Mode

• Can use 64-bit operands


• In the two-operand format, a reg/mem64 is
multiplied to RAX. The 128-bit sign-extended
product is stored in RDX:RAX.
• In the following, RBX is multiplied by RAX.

mov rax,-4
mov rbx,4
imul rbx ; RDX = 0FFFFFFFFFFFFFFFFh, RAX = -16

•The three-operand format is also available


50

25
IMUL Examples

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.
完整結果(+192)要看AH:AL

Example: multiply +4,823,424 * −423:


mov eax,+4823424
mov ebx,-423
imul ebx ; EDX:EAX = FFFFFFFF86635D80h, OF=0

OF=0 because EDX is a sign extension of EAX.


EAX本身就可以表達正確結果(86635D80h)。
51

Your turn . . .

What will be the hexadecimal values of DX, AX, and the


Overflow flag after the following instructions execute?
mov ax,8760h Sign bit = 1
mov bx,100h
imul bx 完整結果要看DX:AX
DX = FF87h, AX = 6000h, OF = 1
比較 mov ax,8760h
mov bx,100h
mul bx
DX = 0087h, AX = 6000h, OF/CF = 1
52

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

Comparing MUL and IMUL to Bit Shifting

• Use GetMseconds to compare their


execution time
• (multiplies EAX by 36) for 0ffffffffh times
• On legacy 4-GH Pentium 4
• SHL takes 6.078 seconds
• MUL takes 20,718 seconds
• MUL is 241% slower
• On a more recent processor
• Both perform exactly the same

54

27
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: Default Operands:
DIV reg/mem8
DIV reg/mem16
DIV reg/mem32
DIV reg/mem64 RDX:RAX r/m64 RAX RDX
55

DIV Examples

Divide 8003h by 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 = 3

Same division, using 32-bit operands:

mov edx,0 ; clear dividend, high


mov eax,8003h ; dividend, low
mov ecx,100h ; divisor
div ecx ; EAX = 00000080h, EDX = 3

56

28
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 dx,0087h
mov ax,6000h
mov bx,100h
div bx

DX = 0000h, AX = 8760h
57

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 dx,0087h
mov ax,6002h
mov bx,10h
div bx 商太大,
Divide Overflow AX存不下

58

29
Signed Integer Division (IDIV
( )

• Signed integers must be sign-extended before


division takes place
• fill high byte/word/doubleword with a copy of the low
byte/word/doubleword's sign bit
• For example, the high byte contains a copy of the
sign bit from the low byte:

10001111

Sign
Extension
(manual)
11111111 10001111

59

CBW, CWD, CDQ Instructions

• The CBW, CWD, and CDQ instructions provide


important sign-extension operations:
• CBW (convert byte to word) extends AL into AH
• CWD (convert word to doubleword) extends AX into DX
• CDQ (convert doubleword to quadword) extends EAX into EDX
• Example:

.data
dwordVal SDWORD -101 ; FFFFFF9Bh
.code
mov eax,dwordVal
cdq ; EDX:EAX = FFFFFFFFFFFFFF9Bh
60

30
IDIV Instruction

• IDIV (signed divide) performs signed integer division


• Same syntax and operands as DIV instruction

Example: 8-bit division of -48 by 5

mov al,-48
cbw ; extend AL into AH
mov bl,5
idiv bl ; AL = -9, AH = -3

61

IDIV Examples

Example: 16-bit division of -48 by 5


mov ax,-48
cwd ; extend AX into DX
mov bx,5
idiv bx ; AX = -9, DX = -3

Example: 32-bit division of -48 by 5


mov eax,-48
cdq ; extend EAX into EDX
mov ebx,5
idiv ebx ; EAX = -9, EDX = -3

62

31
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 ax,0FDFFh ; -513


cwd
mov bx,100h
idiv bx

DX = FFFFh (−1), AX = FFFEh (−2)

63

Divide Overflow

• Occurs when a division produces a quotient


that will not fit into the destination operand
• Causes a processor exception and halts the
current program

mov ax,1000h
mov bl,10h
div bl ; AL can not hold 100h

64

32
Suggestion

• Use a 32-bit divisor and 64-bit dividend


to reduce the probability of divide
overflow

mov eax,1000h
cdq
mov ebx,10h
div ebx ; EAX = 00000100h

65

To Prevent Division By Zero

• Test the divisor before dividing

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

; 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
67

Implementation Example

Example: var4 = (var1 * 5) / (var2 – 3)

mov eax,var1 ; left side


mov ebx,5
imul ebx ; EDX:EAX = product
mov ebx,var2 ; right side
sub ebx,3
idiv ebx ; EAX = quotient
mov var4,eax

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

Sometimes it's easiest to calculate the right-hand term


of an expression first.
69

Your turn . . .

Implement the following expression using


signed 32-bit integers:
eax = (ebx * 20) / ecx

mov eax,20
imul ebx
idiv ecx

70

35
Your turn . . .

Implement the following expression using signed 32-bit


integers. Save and restore ECX and EDX:
eax = (ecx * edx) / eax

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 . . .

Implement the following expression using signed 32-bit


integers. Do not modify any variables other than var3:
var3 = (var1 * -var2) / (var3 – ebx)

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

• Shift and Rotate Instructions


• Shift and Rotate Applications
• Multiplication and Division Instructions
• Extended Addition and Subtraction
• ADC Instruction
• Extended Addition Example
• SBB Instruction
• ASCII and UnPacked Decimal Arithmetic
• Packed Decimal Arithmetic

73

Extended Addition and Subtraction

• Adding and subtracting two numbers that


have an almost unlimited size.
• The arithmetic must be performed in steps
• The Carry value from each step is passed on
to the next step.

ADC ADC ADD

C C C
74

37
ADC Instruction

• ADC (add with carry) instruction adds both a


source operand and the contents of the Carry
flag to a destination operand.
• Operands must be the same size
• Same syntax as ADD, SUB, etc.
ADC
reg,reg
ADC
mem,reg
ADC
reg,mem
ADC 75

mem,imm

ADC Example

• Add two 8-bit integers (FFh + FFh), producing


a 16-bit sum in DL:AL:
mov dl,0
mov al,0FFh
add al,0FFh ; AL = FEh
adc dl,0 ; DL/AL = 01FEh
AL AL
add al,0FFh 11111111 + 11111111 11111110

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

• The SBB (subtract with borrow)


instruction subtracts both a source
operand and the value of the Carry flag
from a destination operand.
• Operand syntax:
• Same as for the ADC 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.

mov edx,7 ; set upper half


mov eax,1 ; set lower half
sub eax,2 ; subtract lower half
sbb edx,0 ; subtract upper half

EDX:EAX = 00000006 FFFFFFFF


Carry = 1
(borrow) 79

What's Next

• Shift and Rotate Instructions


• Shift and Rotate Applications
• Multiplication and Division Instructions
• Extended Addition and Subtraction
• ASCII and Unpacked Decimal Arithmetic
• AAA Instruction
• AAS Instruction
• AAM Instruction
• AAD Instruction
• Packed Decimal Arithmetic
80

40
ASCII Decimal

• A number using ASCII Decimal


representation stores a single ASCII digit in
each byte
• The high 4 bits of each byte is always 0011b
• For example, 3,402 is stored as the
following sequence of hexadecimal bytes:
‘3’ ‘4’ ‘0’ ‘2’ = 02 OR 30h

33h 34h 30h 32h


= 03 OR 30h

one byte for each digit (storing


the ASCII code of the digit)
81

Unpacked Decimal

• Unpacked decimal integers use one byte to


store each decimal digit
• The high 4 bits of each byte is always zeros
• For example, 3,402 is stored as the following
sequence of hexadecimal bytes:

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

ASCII Decimal – 30h 或 Unpacked Decimal


and 0Fh
‘5’
05h
(35h) + 30h 或
ADD or 30h
ASCII Decimal

‘7’
(37h)
= AX
AAA
AL 6Ch 0102h
83

AAA (ASCII adjust after addition)

result AAA AH:AL =


SCII
AL = ASCII add A in AL unpacked decimal
al
decimal decim of the result

• AAA adjusts the binary result of an ADD or ADC


instruction on two ASCII decimals.
• It makes the result in AL an unpacked decimal
that is consistent with ASCII decimal
representation.
• The Carry value, if any, ends up in AH

84

42
AAA Example

• Example: Add '8' and '2'


AX – 0060h = 0ah ⇒
ah = 1 and al = 0 after
AAA
mov ah,0
mov al,'8' ; AX = 0038h
add al,'2' ; AX = 006Ah
aaa ; AX = 0100h (adjust result)
or ax,3030h ; AX = 3130h = '10'

The ASCII for ‘0’ is 30h


85

AAS (ASCII adjust after subtraction)

• AAS adjusts the binary result of an SUB or SBB


instruction.
• It makes the result in AL consistent with ASCII
decimal representation.
• It places the Carry value, if any, in AH
• Example: Subtract '9' from '8'
mov ah,0
mov al,'8' ; AX = 0038h borrow
sub al,'9' ; AX = 00FFh
aas ; AX = FF09h, CF=1
or al,30h ; AL = '9'
86

43
AAM Instruction

• The AAM (ASCII adjust after multiplication)


instruction adjusts the binary result of a MUL
instruction. The multiplication must have been
performed on unpacked decimals.
mov bl,05h ; first operand
mov al,06h ; second operand
unpacked
decimal in mul bl ; AX = 001Eh
AL aam ; AX = 0300h
multiply
unpacked = 30 in unpacked
decimal
decimal
product AAM AH:AL =
in AX unpacked decimal 87

AAD Instruction

• The AAD (ASCII adjust before division)


instruction adjusts the unpacked decimal
dividend in AX before a division operation
.data
quotient BYTE ? = 37
remainder BYTE ?
.code
mov ax,0307h ; dividend
aad ; AX = 0025h
mov bl,5 ; divisor
div bl ; AX = 0207h
mov quotient,al
mov remainder,ah 88

44
What's Next

• Shift and Rotate Instructions


• Shift and Rotate Applications
• Multiplication and Division Instructions
• Extended Addition and Subtraction
• ASCII and Unpacked Decimal Arithmetic
• Packed Decimal Arithmetic
• DAA Instruction
• DAS Instruction

89

Packed Decimal Arithmetic

• Packed decimal integers store two decimal


digits per byte
• For example, 12,345,678 can be stored as the
following sequence of hexadecimal bytes:

12h 34h 56h 78h two digits per byte

Good for financial values – extended precision


possible, without rounding errors.

90

45
DAA Instruction

• The DAA (decimal adjust after addition)


instruction converts the binary sum produced
by ADD or ADC in AL to packed decimal format.
• The value to be adjusted must be in AL
• If the lower digit is adjusted, the Auxiliary
Carry flag is set.
• If the upper digit is adjusted, the Carry flag is
set.

Result AL =
cked DAA
al = packed add pa in AL packed decimal
a l
decimal decim of the result

91

DAA Examples

• Example: calculate packed decimal 35 + 48


mov al,35h
add al,48h ; AL = 7Dh
daa ; AL = 83h, CF = 0
• Example: calculate packed decimal 35 + 65
mov al,35h
add al,65h ; AL = 9Ah
daa ; AL = 00h, CF = 1
• Example: calculate packed decimal 69 + 29 1001
+ 1001
mov al,69h 10002
add al,29h ; AL = 92h
daa ; AL = 98h, CF = 0
92

46
DAS Instruction

• The DAS (decimal adjust after subtraction)


instruction converts the binary result of a SUB
or SBB operation to packed decimal format.
• The value must be in AL
• Example: subtract BCD 48 from 85
mov al,85h
sub al,48h ; AL = 3Dh
das ; AL = 37h CF = 0

DAS AL =
cked
al = packed s ub pa result packed decimal
al
decimal decim of the result
93

Summary

• Shift and rotate instructions are some of the


best tools of assembly language
• finer control than in high-level languages
• SHL, SHR, SAR, ROL, ROR, RCL, RCR
• MUL and DIV – integer operations
• close relatives of SHL and SHR
• CBW, CDQ, CWD: preparation for division
• Extended precision arithmetic: ADC, SBB
• ASCII decimal operations (AAA, AAS, AAM,
AAD)
• Packed decimal operations (DAA, DAS)
94

47

You might also like