Sei sulla pagina 1di 7

Kingdom of Saudi Arabia ‫ا

 ا   ا د‬
Royal Commission at Yanbu    ‫ا   ا‬
University College – Yanbu -  ‫ا   ا‬
Department of ACS & AIT
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

Flags Affected
Operation Opcode Code Example Meaning OF SF ZF AF PF CF

Addition ADD ADD AX,5 AX ← AX + 5 * * * * * *


INC INC BX [BX]←[BX]+1 * * * * * -
Subtraction SUB SUB CL,AH CL ← CL – AH * * * * * *
DEC DEC VAR1 [VAR1] ← [VAR1] – 1 * * * * * -

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 ‘Number1’ 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)

5.2 Instructions of Multiplication and Division

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


• IMUL used to multiply the signed numbers
• DIV used to divide the unsigned numbers
• IDIV used to divide the unsigned numbers

The syntax for multiply and divide are as follows:

• MUL Register example MUL CX


• IMUL Register example IMUL CX
• DIV Register example DIV CX
• IDIV Register example IDIV 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


• - means flag not affected by instruction
• ? means flag is undefined after executing of instruction

The next examples show 8-bit signed and unsigned addition and subtraction.

8-bit signed and unsigned addition


.DATA

MEM8 DB 3
.CODE

; ADDITION ;
MOV AL, 2 ; START WITH REGISTER
INC AL ; AL + 1 -> AL
ADD AL, 4 ; AL + 4 -> AL
ADD AL, MEM8 ; AL + 3 -> AL
MOV AH, AL ; COPY TO AH

ADD AL, AH ; AL + AH -> AL

Prepared by: Khurram Tanvir 2 CS203 HO#5


8-bit signed and unsigned subtraction
; SUBTRACTION
;
MOV AL, 9 ; AL <- 9
DEC AL ; AL – 1 ->AL
SUB AL, 2 ; AL – 2 ->AL
;
;
SUB AL, MEM8 ; AL – 3 ->AL
;
;
MOV AH, 4 ; AH <- 4
SUB AL, AH ; AL – AH ->AL
;
;

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
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: ','$'
.CODE
.STARTUP
LEA DX,PROMPT1 ;DISPLAY PROMPT1
MOV AH,09H
INT 21H

MOV AH,01H ;READ FIRST NUMBER


INT 21H
SUB AL,30H ;Convert character to number
MOV CL,AL ;SAVE THE NUMBER IN CL

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
ADD AL,CL ;PERFORM ADDITION AND SAVE RESULT IN CL

MOV CL,AL
ADD CL,30H ;CONVERT DIGIT TO CHARACTER

Prepared by: Khurram Tanvir 3 CS203 HO#5


LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
LEA DX,PROMPT3 ;DISPLAY PROMPT3
MOV AH,09H
INT 21H

MOV DL,CL ;DISPLAY SUM


MOV AH,02H
INT 21H
.EXIT
END

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 ?
RES DB ?
.CODE
.STARTUP
LEA DX,PROMPT1 ;DISPLAY 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
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
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


MOV AH,02H
INT 21H
.EXIT
END

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,

Opcode Code Example Meaning Flags Affected


OF SF ZF AF PF CF
Operation
DEC DEC VAR1 [VAR1] ← [VAR1] – 1 * * * * * -
Multiplication MUL MUL CL AX ← AL * CL * ? ? ? ? *
(DX,AX) ← AX* CX
MUL CX

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 ; Load AL
mov bl, 2 ; Load BL
mul bl ; BL * AL -> AX
; Product in AX
; overflow and carry set

multiplication of unsigned 16-bit integers


; 8-bit unsigned multiply
mov ax, 3 ; Load AL
mov bx, 2 ; Load BL
mul bx ; 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


Flags Affected
Operation Opcode Code Example Meaning OF SF ZF AF PF CF

Division DIV DIV CX AX ← Q(([DX,AX])/CX) ? ? ? ? ? ?


DX ←R(([DX,AX])/CX)

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 ; Load dividend 700
mov bl, 36 ; Load divisor DIV 36
div bl ; Divide BL ------
; Quotient in AL 19
; Remainder in AH 16

Prepared by: Khurram Tanvir 6 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 register
b. AX register
c. DX register
d. BX register

AS a result of MUL instruction, the higher 16 bits are saved in

a. CX register
b. AX register
c. DX register
d. BX register

Question: How division of unsigned numbers is carried out in 8086.

Prepared by: Khurram Tanvir 7 CS203 HO#5

Potrebbero piacerti anche