System Programming Lab Computer Engineering Department College of Engineering Objectives Write programs in 8086 assembly language for multiplication and division by using different size of data as bytes or words ID: 921290
Download Presentation The PPT/PDF document "Arithmetic Operations Multiplication â€..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Arithmetic OperationsMultiplication – Division
System Programming
Lab
Computer Engineering
Department
College
of
Engineering
Slide2ObjectivesWrite programs in 8086 assembly language for multiplication and
division
by
using different size of data as bytes or words.
Use arithmetic instructions to accomplish simple binary, BCD, and ASCII arithmetic.
Slide3TheoryAnother arithmetic instructions found in any microprocessor includes multiplication and division. Also
shown are their uses in manipulating register and memory data.
These
operations used any addressing mode with 8-bit or 16
bit.
These arithmetic
instructions can be divided into two
types of instructions
:
First
Group
:
MUL, IMUL, TEST
Second group:
DIV, IDIV, CBW, CWD
Slide4TheoryThese types of operands are supported:REG
Memory
REG
:
AX, BX, CX,
DX,
DI, SI, BP, SP
.
Memory
:
[BX], [BX+SI+7], variable, etc...
Multiplication is performed on bytes, words, or double words, and can be signed integer or
unsigned.
Slide5TheoryAfter operation between operands, result is always stored in AL, AX.
The
product after a multiplication is always a double-width product
.
Some flag bits (overflow and carry) change when the multiply instruction
executes
CF
,
OF
Slide6Multiplication MUL – unsigned multiply When operand is a byte AX = AL * operand (8 bit) When operand is a word (AX,DX) = AX * operand (8 or 16 bit)IMUL
– signed
multiply
When operand is a byte
AX
= AL * operand (8 bit)
When operand is a word (AX,DX) = AX * operand
(
8 or 16 bit)
Slide7Unsigned MultiplyInstr.OperandsDescriptionÂ
MUL
REG
Memory
Algorithm:
when operand is a byte:
AX = AL * operand.
when operand is a word:
(DX AX) = AX * operand.
Example:
MOV AL, 200 ; AL = 0C8h
MOV BL, 4
MUL BL ; AX = 0320h (800)
RET
Flags:
C
,
O
Slide8Signed MultiplyInstr.OperandsDescriptionÂ
IMUL
REG
Memory
Algorithm:
when operand is a byte:
AX = AL * operand.
when operand is a word:
(DX AX) = AX * operand.
Example:
MOV AL, -2
MOV BL, -4
IMUL BL ; AX = 8
RET
Flags
:
C
,
O
Slide9TEST Instruction Instr.OperandsDescriptionÂ
TEST
REG , Memory
Memory
, REG
REG
, REG
Memory ,
Immed
.
REG ,
Immed
.
Logical
AND
between all bits of two operands for flags only.
MOV AL, 00000101bTEST AL, 1 ; ZF = 0.TEST AL, 10b ; ZF = 1.RETFlags: Z, S, P
1
AND
1 = 1
0
AND
1 = 0
1
AND
0 = 0
0
AND
0 = 0
Slide10DivisionDIV – unsigned division When operand is a byte AL = AX / operand (8 bit) AH = remainder (modulus) When operand is a word AX = (AX,DX) / operand (8 or 16 bit)
DX = remainder (modulus
)
IDIV
– signed division
When operand is a byte AL = AX / operand (8 bit)
AH
= remainder (modulus)
When operand is a word AX = (AX,DX) / operand
(
8 or 16 bit)
DX = remainder (modulus)
Slide11Unsigned DivideInstr.OperandsDescriptionÂ
DIV
REG
M
emory
Algorithm:
when operand is a byte:
AL = AX / operand
AH = remainder (modulus)
when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus)
Example:
MOV AL, 203 ; AX = 00CBh
MOV BL, 4
DIV BL ; AL = 50 (32h), AH = 3
RET
Flags: NON
Slide12Signed DivideInstr.OperandsDescriptionÂ
IDIV
REG
M
emory
Algorithm:
when operand is a byte:
AL = AX / operand
AH = remainder (modulus)
when operand is a word:
AX = (DX AX) / operand
DX = remainder (modulus)
Example:
MOV AX, -203 ; AX = 0FF35h
MOV BL, 4
IDIV BL ;
AL = -50 (0CEh), AH = -3 (0FDh)
RETFlags: NON
Slide13Convert Byte into WordInstr.OperandsDescription
Â
CBW
No operands
Algorithm:
If high bit of
AL = 1
then:
AH = 255 (0FFh)
else
AH = 0
Example:
MOV AX, 0 ; AH = 0, AL = 0
MOV AL, -5 ; AX = 000FBh (251)
CBW ; AX = 0FFFBh (-5)
RET
Flags: NON
Slide14Convert Word to Double wordInstr.OperandsDescriptionÂ
CWD
No operands
Algorithm:
if high bit of
AX = 1
then:
DX = 65535 (0FFFFh)
else
DX = 0
Example:
MOV DX, 0 ; DX = 0
MOV AX, 0 ; AX = 0
MOV AX, -5 ; DX AX = 00000h:0FFFBh
CWD ; DX AX = 0FFFFh:0FFFBh
RET
Flags
: NON
Slide15ExamplesMultiply two 8-bit unsigned numbers the first in the register BL, and the second in CL, the result is stored in DX (assumed BL=05, CL=10)ORG 100MOV BL, 05H
MOV CL, 10
MOV AL, CL
MUL BL
MOV DX, AX
HLT
Slide16ExamplesDivide the unsigned byte contents of memory location NUMB by the contents of memory location NUMB1. Store the quotient in location ANSQ and reminder in location ANSR (assume [NUMB]=15 and [NUMR]=4).ORG 100MOV AL, NUMB
MOV AH, 00H
DIV NUMB1
MOV ANSQ,AL
MOV ANSR,AH
HLT
NUMB DB 15H
NUMB1 DB 04H
ANSQ DB 0H
ANSR DB 0H
Slide17ExamplesDivide two 16-bit signed numbers, 1st number= -100 the 2nd = +9, store the result in DX, AX.ORG 100hMOV AX, -100
MOV CX, 9
CWD
IDIV CX
HLT
After the division the results appear in DX, AX
AX = -11 quotient
DX = -1 reminder
Slide18ExamplesWrite 8086 program that generate a byte size integer in the memory location define as RESULT, the value of integer is to be calculated from the following equation:(RESULT) = ( AL . NUM1 ) + ( NUM2 . AL ) + BL
MOV AL, 01H
MOV DL, AL
MOV
CL, NUM1
MUL CL ; ( AL . NUM1 )
MOV SI, AX
MOV
AL, DL
MUL NUM2 ;
( AL . NUM2 )
ADD AX, SI
MOV BX, 0001H
ADC
AX, BX
MOV RESULT, AX
HLT
NUM1 DB 02h
NUM2 DB 02h
RESULT DW ?
Slide19ExamplesWrite the program to find the value of AX=((3*A+B ))/2 ;A = 2H, B = 2HA) By using successive addition.B) By using multiplication and division instruction.
MOV
AL, A
ADD
AL
, AL
ADC
AL
, A
ADC AL, B
SUB AL,02H
SUB AL,02H
HLT
A
DB 02H
B DB 02H
MOV AL, A
MOV BL, 03H
MUL BL
ADD AL, B
MOV BL, 02H
DIV BL
HLT
A DB 02H
B DB 02H
10+10+10 =30
30 + 6 = 36
36 – 2
–
2
–
2
–
2
–
2
–
2
–2 –2 = 16
2+2+2 =6
6 + 2 = 8
8 – 2
–2 = 4
Slide20ExamplesWrite 8086 program to compute SUM
+
2
Where Xi & Yi are signed 8-bit numbers, N=10, assume no overflow
MOV SI, OFFSET
X
MOV DI, OFFSET Y
MOV BX, 00H
MOV DX, 00H
MOV CX, 0BH
L1
:
MOV
AL, [SI+BX]
IMUL [DI+BX]
ADD AX, 0002H
ADC
DX, AX
INC BX
LOOP L1
MOV
SUM
,
DX
HLT
X
DB
11 DUP (+2)
Y
DB 11
DUP (+3)
SUM DW ?
MOV SI, OFFSET
X
MOV DI, OFFSET Y
MOV BX, 00H
MOV DX, 00H
MOV CX, 0BH
L1
:
MOV
AL, [SI+BX]
IMUL [DI+BX]
ADD AX, 0002H
ADC
DX, AX
INC BX
LOOP L1
MOV
SUM
,
DX
HLT
X
DB
11 DUP (+2)
Y
DB 11
DUP (+3)
SUM DW ?