Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ا
ا د
Royal Commission at Yanbu ا ا
University College – Yanbu - ا ا
Department of ACS & AIT
Yanbu Al-Sinaiyah ا
5. Introduction
This unit deals with the arithmetic instructions used with the 8086 processor.
8086 uses same instructions to add and subtract signed and unsigned numbers.
These are as follows:
Flags Affected
Operation Opcode Code Example Meaning OF 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 ‘Number1’ added with AX
ADD Number1, BX; Data of register BX is added with Memory Location
ADD NUM1, NUM2 -> WRONG (memory to memory addition not allowed in 8086)
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.
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.
The next examples show 8-bit signed and unsigned addition and subtraction.
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
Example 1
TITLE "PROGRAM 1 EXPERIMENT 4"
.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 CL,AL
ADD CL,30H ;CONVERT DIGIT TO CHARACTER
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
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,
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.
The DIV instruction divides unsigned numbers, and IDIV divides signed numbers. Both
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
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
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)
a. CX register
b. AX register
c. DX register
d. BX register
a. CX register
b. AX register
c. DX register
d. BX register