Lecture 10 (Arithmetic Instructions)
Lecture 10 (Arithmetic Instructions)
Instruction set
CI 052
02/21/15
K.Rajalakshmi,JIIT,Noida
Instruction set
CI 052
02/21/15
K.Rajalakshmi,JIIT,Noida
Instruction set
CI 052
Arithmetic Instruction
Addition
ADD
ADC
INC
AAA
DAA
02/21/15
SUB
SBB
DEC
NEG
AAS
DAS
Instruction set
CI 052
Multiplication
MUL
IMUL
AAM
02/21/15
DIV
IDIV
AAD
CBW
CWD
K.Rajalakshmi,JIIT,Noida
Instruction set
CI 052
Addition Instruction
Mnemonic
Meaning
Format
Operation
Flag
affected
ADD
Addition
ADD D,S
OF,SF,ZF,
AF,PF,CF
ADC
ADC D,S
OF,SF,ZF,
AF,PF,CF
INC
Increment by 1
INC D
(D) + 1 (D)
OF,SF,ZF,
AF,PF
AAA
AAA
OF,SF,ZF,
AF,PF,CF,
undefined
DAA
Decimal adjust
for addition
DAA
OF,SF,ZF,
AF,PF,CF,
undefined
02/21/15
K.Rajalakshmi,JIIT,Noida
Instruction set
CI 052
Destination
Source
Destination
Register
Register
Reg 16
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Reg 8
Memory
Accumulator Immediate
02/21/15
K.Rajalakshmi,JIIT,Noida
Instruction set
CI 052
EXAMPLE (1):
Assume that the AX and BX registers contain 110016 and
0ABC16, respectively. What is the result of executing the
instruction ADD AX, BX?
Solution:
(BX)+(AX) = 0ABC16 + 110016
=1BBC16
The sum ends up in destination register AX. That is
(AX) = 1BBC16
02/21/15
K.Rajalakshmi,JIIT,Noida
02/21/15
Instruction set
K.Rajalakshmi,JIIT,Noida
CI 052
02/21/15
Instruction set
K.Rajalakshmi,JIIT,Noida
CI 052
Instruction set
CI 052
EXAMPLE(2):
The original contents of AX, BL, word-size memory location
SUM, and carry flag (CF) are 123416, AB16, 00CD16, and 016,
respectively. Describe the results of executing the following
sequence of instruction?
ADD AX, [SUM]
ADC BL, 05H
INC [SUM]
Solution:
(AX) (AX) + (SUM)
= 00CD16 + 116
K.Rajalakshmi,JIIT,Noida
= 00CE16
10
Instruction set
CI 052
EXAMPLE (3):
What is the result of executing the following instruction sequence?
ADD AL, BL
AAA
Assuming that AL contains 3216 (ASCII code for 2) and BL contains 3416
(ASCII code 4), and that AH has been cleared.
Solution:
(AL) (AL)+(BL)= 3216 + 3416=6616
The result after the AAA instruction is
(AL) = 0616
(AH) = 0016
with both AF and CF remain cleared
02/21/15
K.Rajalakshmi,JIIT,Noida
11
Instruction set
CI 052
EXAMPLE (4):
Perform a 32-bit binary add operation on the contents of the
processors register.
Solution:
(DX,CX) (DX,CX)+(BX,AX)
(DX,CX) = FEDCBA9816
(BX,AX) = 0123456716
MOV DX, 0FEDCH
MOV CX, 0BA98H
MOV BX, 01234H
MOV AX, 04567H
ADD CX, AX
ADC DX, BX
02/21/15
12
02/21/15
Instruction set
K.Rajalakshmi,JIIT,Noida
CI 052
13
02/21/15
Instruction set
K.Rajalakshmi,JIIT,Noida
CI 052
14
Instruction set
CI 052
EXAMPLE
Assuming that the contents of register BX and CX are
123416 and 012316, respectively, and the carry flag is 0, what
is the result of executing the instruction SBB BX, CX?
Solution:
(BX) - (CX) - (CF) (BX)
We get
(BX)
K.Rajalakshmi,JIIT,Noida
15
Instruction set
CI 052
EXAMPLE
= 000016-(BX)
=000016+2complement of 003A16
= 000016+FFC616
= FFC616
02/21/15
K.Rajalakshmi,JIIT,Noida
16
Instruction set
CI 052
EXAMPLE
Perform a 32-bit binary subtraction for variable X and Y.
Solution:
MOV SI, 200H ; Initialize pointer for X
MOV DI, 100H ; Initialize pointer for Y
MOV AX, [SI] ; Subtract LS words
SUB AX, [DI]
MOV [SI],AX ; Save the LS word of result
MOV AX, [SI]+2 ; Subtract MS words
SBB AX, [DI]+2
MOV [SI]+2, AX ; Save the MS word of result
02/21/15
K.Rajalakshmi,JIIT,Noida
17
02/21/15
Instruction set
K.Rajalakshmi,JIIT,Noida
CI 052
18
02/21/15
Instruction set
K.Rajalakshmi,JIIT,Noida
CI 052
19
Instruction set
CI 052
EXAMPLE
The 2s-complement signed data contents of AL are 1 and that of CL
are 2. What result is produced in AX by executing the following
instruction?
MUL CL and IMUL CL
Solution:
(AL) = -1 (as 2s complement) = 111111112 = FF16
(CL) = -2 (as 2s complement) = 111111102 = FE16
Executing the MUL instruction gives
(AX)
= 111111112x111111102
=11111101000000102
=FD0216
K.Rajalakshmi,JIIT,Noida
20
Instruction set
CI 052
EXAMPLE
What is the result of executing the following instructions?
MOV AL, 0A1H
CBW
CWD
Solution:
(AL) = A116 = 101000012
Executing the CBW instruction extends the MSB of AL
(AH) = 111111112 = FF16 or (AX) = 11111111101000012
Executing the CWD instruction, we get
(DX) = 11111111111111112 = FFFF16
That is, (AX) = FFA116 (DX) = FFFF16
02/21/15
K.Rajalakshmi,JIIT,Noida
21
Instruction set
CI 052
: DAA
Subtraction
: DAS
02/21/15
3099 h
3099 h
------
------
42CD h
4333 h
-------
------K.Rajalakshmi,JIIT,Noida
22
Instruction set
CI 052
MOV BX,3099H
MOV BX,3099H
MOV AL,BL
MOV AL,BL
ADD AL,DL
ADD AL,DL
DAA
DAA
MOV CL,AL
MOV CL,AL
MOV AL,BH
MOV AL,BH
ADC AL,DH
ADC AL,DH
DAA
DAA
MOV CH,AL
02/21/15
MOV CH,AL
K.Rajalakshmi,JIIT,Noida
23
Instruction set
CI 052
ASCII Arithmetic
The ASCII arithmetic instructions functions with ASCII-coded numbers
The numbers range in value from 30-39 h for the numbers 0-9.
02/21/15
K.Rajalakshmi,JIIT,Noida
24
Instruction set
AAA
CI 052
AAD
AAM
ADD instruction
31h + 39h = 6Ah
MUL instruction
05H * 05H = 0019H
AAA instruction
1 + 9 = 10 =01 00h
DIV instruction
72 / 9 => AL=8 ; AH =0
AAM Instruction
5 * 5 = 02 05H
AAD instruction
0702H is loaded into AX
Adjust by AAD instruction
AX will have 0048
0048 is binary equivalent of 72 decimal
Example
MOV AX, 31H
ADD AL,39H
AAA
ADD AX,3030H
Example
MOV BL, 9
MOV AX,0702H
AAD
DIV BL
Example
MOV AL,5
MOV CL,5
MUL CL
AAM
02/21/15
K.Rajalakshmi,JIIT,Noida
25