\ue000\ue001\u062f\ue002\ue0 \ue00c\ue00d\ue00e\ue001 \ue00c\ue00d\ue00e\ue001 -\ue000\ue006\ue003\ue0
Kingdom of Saudi Arabia Royal Commission at Yanbu University College \u2013 Yanbu Department of ACS & AIT
\ue000\ue006\ue014\ue0
Yanbu Al-Sinaiyah
2ND Semester 2007-08
CS-203 HANDOUT 1 (HO - 5) ARITHMETIC INSTRUCTIONS IN 8086
5. Introduction This unit deals with the arithmetic instructions used with the 8086 processor. 5.1
Instructions to Add and Subtract
8086 uses same instructions to add and subtract signed and unsigned numbers. These are as follows: ADD used for adding INC used for incrementing SUB used for subtracting DEC used for decrementing All these instructions operate on values in single registers, memory or constants. They can also be combined to handle larger values that require two registers for storage. The syntax for Add and subtract are as follows: ADD
Register, Immediate Number Register, Register Register, Memory Location Memory Location, Register
Operation Addition Subtraction
Opcode
Code Example
Meaning
ADD INC SUB DEC
ADD AX,5 INC BX SUB CL,AH DEC VAR1
AX \u2190 AX + 5
* [BX]+1 * CL \u2013 AH * [VAR1] \u2013 *1
[BX]\u2190 CL \u2190
OF
[VAR1] \u2190
Flags Affected
SF
ZF
AF
PF
CF
* * * *
* * * *
* * * *
* * * *
* * -
Example: ADD AL, 34; 34 is added with data of register AL and result is stored in AL ADD AH, AL; Data of AL is added with data of AH and result is stored in AH ADD AX, Number1; Data stored in memory location \u2018Number1\u2019 added with AX ADD Number1, BX; Data of register BX is added with Memory Location
Prepared by: Khurram Tanvir
1
CS203 HO#5
Note: Destination cannot be an immediate number. In an instruction, source and destination cannot be memory. ADD 34, AL;
-> WRONG
ADD NUM1, NUM2 -> WRONG (memory to memory addition not allowed in 8086) Instructions of Multiplication and Division
5.2
To multiply and divide, 8086 has different instructions for signed and unsigned numbers. Multiplication and division instructions have also special requirements depending on the size of the operands and the processor the code runs on. MUL used to multiply the unsigned numbers \u2022IMUL used to multiply the signed numbers \u2022DIV used to divide the unsigned numbers \u2022IDIV used to divide the unsigned numbers \u2022
The syntax for multiply and divide are as follows: MUL Register \u2022IMUL Register \u2022DIV Register \u2022IDIV Register
example example example example
\u2022
MUL IMUL DIV IDIV
CX CX CX CX
Note that before performing the multiplication, we need to store the first number in the accumulator register. Also to perform the division, we need to store the number to be divided (dividend) in data register. Following table shows the example codes, their meaning, and flags affected after the execution of the instruction. * means flag changed \u2022- means flag not affected by instruction \u2022? means flag is undefined after executing of instruction \u2022
The next examples show 8-bit signed and unsigned addition and subtraction.
8-bit signed and unsigned addition
.DATA
DB
MEM8 .CODE ; ADDITION MOV INC ADD ADD MOV ADD
Prepared by: Khurram Tanvir
3
AL, 2 AL AL, 4 AL, MEM8 AH, AL
; ; START WITH REGISTER ; AL + 1 -> AL ; AL + 4 -> AL ; AL + 3 -> AL ; COPY TO AH
AL, AH
; AL + AH -> AL
2
CS203 HO#5
8-bit signed and unsigned subtraction
; SUBTRACTION MOV DEC SUB
SUB
MOV SUB
; ; AL <- 9 ; AL \u2013 1 ->AL ; AL \u2013 2 ->AL ; ; AL, MEM8 ; AL \u2013 3 ->AL ; ; AH, 4 ; AH <- 4 AL, AH ; AL \u2013 AH ->AL ; ; AL, 9 AL AL, 2
Example 1 TITLE "PROGRAM 1 EXPERIMENT 4" ; This program reads two numbers from the keyboard and ; gives their sum. This program uses internal registers ; to store the variables.
.MODEL SMALL .STACK 200 .DATA DB 0DH,0AH,'$' CRLF PROMPT1 DB 'Enter the first positive integer: ','$' PROMPT2 DB 'Enter the second positive integer: ','$' PROMPT3 DB 'The sum of the two numbers is: ','$' .CODE .STARTUP LEA DX,PROMPT1 ;DISPLAY PROMPT1 MOV AH,09H INT 21H MOV AH,01H INT 21H SUB AL,30H MOV CL,AL
;READ FIRST NUMBER
LEA DX,CRLF MOV AH,09H INT 21H LEA DX,PROMPT2 MOV AH,09H INT 21H
;MOVE CURSOR TO NEXT LINE
MOV AH,01H INT 21H SUB AL,30H ADD AL,CL MOV CL,AL ADD CL,30H
;Convert character to number ;SAVE THE NUMBER IN CL
;DISPLAY PROMPT2
;READ SECOND NUMBER ;Convert character to number ;PERFORM ADDITION AND SAVE RESULT IN CL
;CONVERT DIGIT TO CHARACTER
Prepared by: Khurram Tanvir
3
CS203 HO#5
LEA DX,CRLF MOV AH,09H INT 21H LEA DX,PROMPT3 MOV AH,09H INT 21H
.EXIT END
MOV DL,CL MOV AH,02H INT 21H
;MOVE CURSOR TO NEXT LINE
;DISPLAY PROMPT3
;DISPLAY SUM
Example 2 TITLE "PROGRAM 2 EXPERIMENT 4" ; This program reads two numbers from the keyboard and ; displays their sum. This program uses the memory to ; store the variables. .MODEL SMALL .STACK 200 .DATA CRLF DB 0DH,0AH,'$' PROMPT1 DB 'Enter the first positive integer: ','$' PROMPT2 DB 'Enter the second positive integer: ','$' PROMPT3 DB 'The sum of the two numbers is: ','$' NUM1 DB ? NUM2 DB ? DB ? RES .CODE .STARTUP ;DISPLAY PROMPT1 LEA DX,PROMPT1 MOV AH,09H INT 21H MOV AH,01H ;READ FIRST NUMBER INT 21H SUB AL,30H ;Convert character to number MOV NUM1,AL ;SAVE NUM1 LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE MOV AH,09H INT 21H LEA DX,PROMPT2 ;DISPLAY PROMPT2 MOV AH,09H INT 21H MOV AH,01H ;READ SECOND NUMBER INT 21H SUB AL,30H ;Convert character to number MOV NUM2,AL ;SAVE NUM2 ADD AL,NUM1 ;PERFORM ADDITION MOV RES,AL ;SAVE RESULT IN RES ;MOVE CURSOR TO NEXT LINE LEA DX,CRLF MOV AH,09H INT 21H LEA DX,PROMPT3 ;DISPLAY PROMPT3 MOV AH,09H INT 21H ;DISPLAY SUM MOV DL,RES ;RETREIVE RES FROM MEMORY ADD DL,30H ;CONVERT DIGIT TO CHARACTER
Prepared by: Khurram Tanvir
4
CS203 HO#5
.EXIT END
MOV AH,02H INT 21H
5.3 Using Multiplication Instructions: The MUL instruction multiplies unsigned numbers. IMUL multiplies signed numbers. For both instructions, one factor must be in the accumulator register (AL for 8-bit numbers,
Operation Multiplication
Opcode
Code Example
Meaning
DEC MUL
DEC VAR1 MUL CL MUL CX
[VAR1] ← [VAR1] – 1 AX ← AL * CL (DX,AX) ← AX* CX
OF
* *
Flags Affected
SF
* ?
ZF
* ?
AF
* ?
PF
* ?
CF
*
AX for 16-bit numbers). The other factor can be in any single register or memory operand. The result overwrites the contents of the accumulator register. Multiplying two 8-bit numbers produces a 16-bit result returned in AX. Multiplying two 16-bit operands yields a 32-bit result in DX:AX. The following examples illustrate multiplication of unsigned 8 integers.
multiplication of unsigned 8-bit integers
; 8-bit unsigned multiply mov al, 3 mov bl, 2 mul bl
; Load AL ; Load BL ; BL * AL -> AX ;ProductinAX; overflow and carry set
multiplication of unsigned 16-bit integers
; 8-bit unsigned multiply mov ax, 3 mov bx, 2 mul bx
; ; ; ; ;
Load AL Load BL BX * AX -> (DX:AX) DX has high 16 bits AX has lower 16 bits
5.4 Using Division Instructions: The DIV instruction divides unsigned numbers, and IDIV divides signed numbers. Both
Prepared by: Khurram Tanvir
5
CS203 HO#5
Operation
Opcode
Code Example
Meaning
Division
DIV
DIV CX
AX ← Q(([DX,AX])/CX) DX ←R(([DX,AX])/CX)
Flags Affected
OF
SF
ZF
AF
PF
CF
?
?
?
?
?
?
return a quotient and a remainder. Table summarizes the division operations. The dividend is the number to be divided, and the divisor is the number to divide by. The quotient is the result. The divisor can be in any register or memory location except the registers where the quotient and remainder are returned.
AX ← Quotient(([DX,AX])/CX) DX ←Remainder(([DX,AX])/CX) Size of Operand Dividend Register Size of Divisor Quotient Remainder 16 bits
AX
8 bits
AL
AH
32 bits
DX:AX
16 bits
AX
DX
Table: Division Operations Unsigned division does not require careful attention to flags. The following examples illustrate signed division, which can be more complex.
unsigned division
; Divide 16-bit unsigned by 8-bit mov ax, 700 mov bl, 36 div bl
Prepared by: Khurram Tanvir
; ; ; ; ;
6
Load dividend Load divisor DIV Divide BL Quotient in AL Remainder in AH
------
700 36 19 16
CS203 HO#5
Example Test Questions Fill in the blanks
1. DEC VAR1 means ___________________________________ . 2. ___________statement decrements the register by 1. Specify True or False
1. ADD instruction is used for both signed and unsigned numbers (True/False) 2. IMUL instruction is used for both signed and unsigned numbers (True/False) 3. As a result of multiplication the higher 16 bits go to AX (True/False) Choose the best answer.
As a result of DIV instruction, the remainder goes to a. CX b. AX c. DX d. BX
register register register register
AS a result of MUL instruction, the higher 16 bits are saved in a. CX b. AX c. DX d. BX
register register register register
Question: How division of unsigned numbers is carried out in 8086.
Prepared by: Khurram Tanvir
7
CS203 HO#5