Ensamblador 8086
Ensamblador 8086
EXPERIMENT NO. 6 - 7
6 ARITHMETIC OF SIGNED INTEGERS, DOUBLE
PRECISION,
BCD
AND
FLOATING
POINT
NUMBERS IN 8086
6.1 Objectives
The objectives of this experiment are
To learn how to handle carry and borrow propagation using ADC and SBB
instructions.
To get familiarized with arithmetic of signed integers using NEG, IMUL and IDIV
instructions.
Meaning
ADC
SBB
NEG
Negate
IMUL
Integer multiplication
IDIV
Integer division
CWD
CBW
AAA
AAS
10
AAM
11
AAD
Part B
Part C
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
Syntax
Flag effected
Sample Code
Byte
C (carry)
ADD BL, 5H
Word
C (carry)
Syntax
Flag effected
Sample Code
Byte
C (borrow)
SUB AL, BL
Word
C (borrow)
SUB DATA, 5H
Syntax
Multiplier
Multi
plica
nd
Product
Byte
MUL BYTE_MULP
BYTE_MULP
AL
AX
Word
MUL WORD_MULP
WORD_MULP
AX
DX:AX
Sample
Code
Flags effected
MUL BL
MUL DAT_BYT
CF/
OF
*
CF/OF=1 means that the product is too big to fit in the lower half of the destination (AL for byte
00multiplication and AX for word multiplication)
*
Syntax
Divisor
Dividend
Quotient
Remainder
Sample Code
Byte
MUL BYTE_DIVR
BYTE_DIVR
AX
AL
AH
DIV DATA_8BIT
Word
MUL WORD_DIVR
BYTE_DIVR
DX:AX
AX
DX
DIV BX
Divide overflow: It is possible that the quotient will be too big to fit in the specified
destination (AL or AX). This can happen if the divisor is much smaller than the dividend.
When this happened the program terminates and the system displays the message Divide
Overflow.
*
The effect of DIV on the flags is that all status flags are undefined.
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
The following example illustrates the effects of MUL and DIV instruction on flags.
;EXAMPLE 1
;RUN THE CODE IN SINGLE STEP MODE
CODE
SEGMENT
ASSUME CS:CODE
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
;CHECK THE CARRY FLAG, OVERFLOW FLAG, ZERO FLAG, NOTE EFFECTS OF DIV ON
;FLAGS ARE UNDEFINED
HLT
ORG 50H
A DB 0FH
CODE
ENDS
END
Operation
destinationsource+destination+carry(CF)
destinationdestination-source-borrow(CF)
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
For example,
NEG destination
The following example illustrates conversion on numbers to their twos complements. For
each number, calculate the twos complement in a scratch-paper and verify them with
the twos complement calculated by 8086.
;EXAMPLE 2
CODE
SEGMENT
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
ASSUME CS:CODE
MOV CX, 5
MOV DI, 0
NEG_: MOV AX, [A+DI]
NEG AX
MOV [B+DI], AX
ADD DI, 2
LOOP NEG_
HLT
ORG 050H
A DW 1H , 0FFFFH, 0F056H, 1056H, 4059H
ORG 060H
B DW ?, ?, ?, ?, ?
CODE
ENDS
END
IMUL multiplier
And
IDIV divisor
The following points for signed multiplication and division are to be noted:
1. For both the instructions all the operands the considered signed integers.
2. The product of signed multiplication is also a signed integer.
3. For signed division the remainder has the same sign as the dividend.
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
Both the instructions attributes are same for MUL and DIV instructions as depicted in
table 1(c) and 1(d) except that the effect of IMUL on status flag is a bit different.
=0, if upper half of the product is the sign extension of the lower half
(this means the bits of the upper half are the same as the sign bit og
the lower half)
CF/OF
=1, otherwise
The following examples illustrate the differences between MUL and IMUL.
Table 3: Examples for illustrating the differences of MUL and IMUL.
No.
AX
BX
1h
FFFFh
Instru
ction
Decimal
product
FFFFh
FFFFh
0FFFh
0100h
FFFFh
AX
CF/OF
0000 FFFF
0000
FFFF
-1
FFFF FFFF
FFFF
FFFF
42948362
25
FFFE 0001
FFFE
0001
0000 0001
0000
0001
MUL AX
16769025
00FF E001
00FF
E001
IMUL AX
16769025
00FF E001
00FF
E001
MUL BX
16776960
00FF FF00
00FF
FF00
-256
FFFF FF00
FFFF
FF00
MUL BX
IMUL BX
DX
65535
MUL BX
IMUL BX
Hex product
IMUL BX
The following examples illustrate the differences between DIV and IDIV.
Table 4: Examples for illustrating the differences of MUL and IMUL.
No.
DX
AX
BX
0000h
0005h
0002h
0000h
0005h
FFFEh
Instructi
on
FFFFh
FFFBh
0002h
Decimal
Remainder
AX
DX
DIV BX
0002
0001
IDIV BX
0002
0001
DIV BX
0000
0005
-2
FFFE
0001
FFFE
FFFF
IDIV BX
Decimal
quotient
Divide Overflow
DIV BX
IDIV BX
-2
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
-1
The following example illustrates the difference of IMUL and MUL instructions. Try to
explain why the results in of MUL and IMUL instructions are different.
;EXAMPLE 3
CODE
SEGMENT
ASSUME CS:CODE
MOV AX,0FFFFH
PUSH AX
MOV PROD_MUL, DX
MOV PROD_MUL+2, AX
MOV PROD_IMUL, DX
MOV PROD_IMUL+2, AX
IMUL BX
HLT
ORG 050H
PROD_MUL DW ?, ?
ORG 060H
PROD_IMUL DW ?, ?
CODE
ENDS
END
;EXAMPLE 4
CODE
SEGMENT
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
ASSUME CS:CODE
MOV AX,0F056h
PUSH AX
ORG 050H
SQ_MUL DW ?, ?
ORG 060H
SQ_IMUL DW ?, ?
CODE
ENDS
END
;AX = FB1Eh
CWD
will make DX sign extension of AX, so that DX:AX = FFFF FB1E h.
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
In the following example it is intended to calculate the -4010d7d. Try to explain why
the result is not correct when sign extension to DX is not performed.
;EXAMPLE 5
CODE
SEGMENT
ASSUME CS:CODE
XOR DX, DX
DIV BX
MOV Q_DIV, AX
MOV R_DIV, DX
ORG 050H
Q_DIV
DW ?
R_DIV
DW ?
ORG 060H
Q_IDIV1 DW ?
R_IDIV1 DW ?
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
10
ORG 070H
Q_IDIV2 DW ?
R_IDIV2 DW ?
CODE ENDS
END
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
11
PART C: Arithmetic with Double Precision Numbers, BCD Numbers and Floating
Point Numbers
Higher 16 bit
higher 16 bits
Carry from
lower 16 bits
Lower 16
bit
FFFF
FA11
+0A12
+1001
0A11
0A12
+1
1
0A12
0A12
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
12
3. AXA+2, BXB+2
4. C+2AX+BX+C(Carry)
;EXAMPLE 6
; DOUBLE PRECISION ADDITION AND SUBTRACTION
; C+4:C+2:C = A+2:A + B+2:B
; D+2:D = A+2:A - B+2:B
CODE SEGMENT
ASSUME CS:CODE,DS:CODE
MOV AX, A
MOV BX, B
ADD AX, BX
MOV C, AX
MOV AX, A+2
MOV BX, B+2
ADC AX, BX
MOV C+2, AX
ADC C+4, 0
MOV AX, A
MOV BX, B
SUB AX, BX
MOV D, AX
MOV AX, A+2
MOV BX, B+2
SBB AX, BX
MOV D+2, AX
HLT
ORG 0050H
A
DW
0F056H, 4509H
ORG 0060H
B
DW
1056H, 1509H
ORG 0070H
C
DW
?,?, ?
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
13
ORG 0080H
D
DW
CODE
?,?
ENDS
END
Q1 xx
Q2+Carry: P2+Q1:C
(=C+4)
(=C+2)
AXA
DX:AXAXBX
CAX
TEMPDX
AXA+2
DX:AX AXBX
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
14
7. AXAX+TEMP
8. C+2AX
9. DXDX+C (Carry)
10. C+4DX
;EXAMPLE 7
; DOUBLE PRECISION MULTIPLICATION
; C+4:C+2:C=BX x A+2:A
CODE
SEGMENT
ASSUME
CS:CODE,DS:CODE
ORG 0100H
MOV BX, 0E56FH
XOR DX, DX
MOV AX, A
;STEP 1
MUL BX
;STEP 2
MOV C, AX
;STEP 3
MOV TEMP, DX
;STEP 4
;STEP 5
MUL BX
;STEP 6
;STEP 7
MOV C+2, AX
;STEP 8
ADC DX, 0
;STEP 9
MOV C+4, DX
;STEP 10
HLT
ORG 0150H
A
DW
0F056H, 4509H
ORG 0160H
DW
?, ?,?
TEMP
DW
CODE
ENDS
END
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
15
DX:AXA+4:A+2
Quotient AX, Remainder DXDX:AXBX
Q+2AX
AXA
Quotient AX, Remainder DXDX:AXBX
QAX, RDX
; EXAMPLE 8
;THIS EXAMPLE PERFORMS A 48 BIT NUMBER BY 16 BIT NUMBER DIVISION
;Q+2:Q=A+4:A+2:A/BX, R = REMAINDER
CODE SEGMENT
ASSUME CS:CODE,DS:CODE
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
16
2* Can you extend this algorithm to perform a 48 bit by 32 bit division by using DIV
instruction? If not, why?
AH
0000 0000
AL
BL
0000 0110
+
AL
+
AH
0000 0001
0000 0111
0000 1101
AL
0000 1010
0000 0011
This adjustment is performed in 8086 if we add the instruction AAA (ASCII Adjust for
addition).
For example, the following assembly code performs decimal addition on the unpacked
BCD numbers in AL and BL.
MOV AH, 0
ADD AL, BL
AAA
Example 9 performs the addition of two two-digit numbers stored in A+1:A and B+1:B.
;Example 9
;ADDITION OF TWO TWO-DIGIT NUMBERS
;C+2:C+1:C = A+1:A + B+1:B
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
17
CODE
SEGMENT
ASSUME
CS:CODE,DS:CODE
ORG 0100H
XOR AH, AH
MOV AL, A
ADD AL, B
AAA
MOV C, AL
MOV AL, AH
ADD AL, A+1
ORG 0150H
A
DB
7, 9
;A+1:A=09 07
DB
5, 6
;B+1:B=06 05
ORG 0160H
C
DB
?, ?, ?
CODE ENDS
END
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
18
AH
0000 0010
AL
BL
0000 0010
1
AH
AL
0000 0001
0000 0110
0000 0111
1111 1111
-
AL
0000 0110
0000 1001
This adjustment is performed in 8086 if we add the instruction AAS (ASCII Adjust for
subtraction) after subtraction. The usage of AAS is shown below.
MOV AH, 0
SUB AL, BL
AAS
Problem Set 6.16
1* Write down the code to subtract the two-digit number in bytes B+1:B from the
one contained in A+1:A.
MUL BL
AAM
The following example illustrates the effect AAM on AX and performs a single digit BCD
multiplication. Run the code in single step mode.
;EXAMPLE 10
CODE
SEGMENT
ASSUME
CS:CODE,DS:CODE
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
19
MOV AL, 9D
MOV BL, 8D
MUL BL
AAM
HLT
CODE ENDS
END
;EXAMPLE 11
;BCD DIVISION
;95 BY 8
; Q=QUOTIENT, R=REMAINDER
CODE
SEGMENT
ASSUME
CS:CODE,DS:CODE
MOV AL, 05
MOV AH, 09
MOV BL, 8
AAD
DIV BL
MOV R, AH
AAM
MOV Q, AL
MOV Q+1, AH
HLT
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
20
Q DB ?, ?
R DB ?
CODE ENDS
END
;EXAMPLE 12
;MULTIPLICATION OF TWO NUMBERS IN FLOATING POINT REPRESENTATION
;A = 8000 0000 0000h
= 8x16^(C)
= Fx16^(-B)
;C=AxB
CODE
SEGMENT
ASSUME
CS:CODE,DS:CODE
ORG 0100H
MOV AX, A
MUL B
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
21
MOV C, AX
DW
8H, 0CH
DW
DW
CODE ENDS
END
6.23 References
Ytha Yu, Charles Marut, Assembly Language Programming and Organization of the
IBM PC, Mitchell McGraw-Hill, 1992.
Chapter 4: Introduction to IBM PC and Assembly Language (Part A)
Chapter 9: Multiplication and Division Instructions (Part B)
Chapter 18: Advanced Arithmetic (Part C)
Asif Islam Khan & Dr. Kazi Mujibur Rahman, June 19, 2007
22