0% found this document useful (0 votes)
13 views18 pages

Lec8 - Multiplication and Division

assembly

Uploaded by

iamnah97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

Lec8 - Multiplication and Division

assembly

Uploaded by

iamnah97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Lec 8

Multiplication & Division


Multiplication

2 Types:

1. Unsigned (Use MUL instruction)


2. Signed (Use IMUL instruction)
MUL (unsigned)
• MUL instruction is used with unsigned operands
• Unsigned multiplication of 128 and 255 = 32, 640.

• 2 bytes multiplied, product • 2 words multiplied, product


= 1 word (16 bits) = 1 double word (32 bits)
• AL is implied destination • AX is implied destination
operand operands
• Source operand can be a • Source operand can be a
register or variable (8 bits) register or variable (16 bits)
• 16-bit output is AX • 32-bit output is DX:AX
Examples
• 8-bit multiplication:
MUL BL ; product = AX
• 16-bit multiplication:
MUL DX ; product = DX:AX

mov al,5 mov ax,500h


mov bl,10h mov bx,100h
mul bl ; AX = mul bx ; DX:AX =
0050h 00050000h
FLAGS affected by MUL

Carry and Overflow flags are set when the product extends into its high
register:

mov ax,500h mov ax,5000h


mov bx,10h mov bx,10h
mul bx ; DX:AX = mul bx ; DX:AX =
00005000h 00050000h
; CF=0, ; CF=1,
OF=0 OF=1
IMUL Instruction (Integer Multiplication)
• Use with signed operands
• Sign-extends the result into the high register
• CF=1 and OF=1 if the sign of the high register is different from the sign of the
low register

mov al,48
mov al,-4
mov bl,4
mov bl,4
imul bl ; AX = 00C0h
imul bl ; AX = FFF0h (- (+192), CF=1, OF = 1
16), CF=0, OF = 0
Example
mov ax,-128
mov bx,4
imul bx ; DX:AX = FFFFh,FFE0h (-512)
; CF=0, OF = 0
Suppose A and B are two word variables:
Evaluate A = 5 * A – 12 * B (Assume no
overflow occurs)
Solution:
MOV AX, 5 ; AX = 5
IMUL A ; AX = 5 * A
MOV A, AX ; A = 5 * A
MOV AX, 12 ; AX = 12
IMUL B ; AX = 12 * B
Practice
▶ Suppose A, B, C are word variables and all
products will fit in 16 bits. Write a program to
evaluate

▶A = 5 x A – 7
▶ B = (A – B) x (B + 10)
▶A = 6 – 9 x A
Factorial Example
▶ Compute N! (factorial) for a positive integer N
▶ Assumption: product does not overflow 16 bits!
input CX = N
; output AX = N!
MOV AX, 1
TOP:
MUL CX
LOOP TOP
RET
DIV Instruction
• Dividend is divided by divisor
• Result: Quotient and Remainder

• Byte Form: • Word Form:


• Dividend is AX • Dividend is DX:AX
• Divisor can be 8-bit • Divisor can be 16-bit
register or variable register or variable
• Quotient is AL, • Quotient is AX,
Remainder is AH Remainder is DX

• Status flag values are undefined


Examples
mov ax,0083h ; dividend
mov bl,2 ; divisor
div bl ; AL = 41h, AH = 01h

mov dx,0 ; dividend, high


mov ax,8003h ; dividend, low
mov cx,100h ; divisor
div cx ; AX = 0080h, DX = 0003h
Divide Overflow
Happens when the quotient is too large to fit in the destination
register. Causes a processor interrupt.

mov dx,0050h ; dividend, high


mov ax,0000h ; dividend, low
mov cx,10h ; divisor
div cx ; quotient= 50000h, cannot
; fit in AX register
IDIV Instruction
• Use for signed division
• Dividend must be sign-extended before executing the IDIV instruction:
• CBW (Convert byte to word) extends AL into AH
• CWD (Convert word to double word) extends AX into DX
• Status flag values are undefined
mov ax,-257 ; mov ax,-5000 ; dividend
dividend
cwd ;
mov bl,2 ; divisor extend into DX
idiv bl ; AL = -128, AH mov bx,256 ;
= -1 divisor
idiv bx ; AX = -19,
DX = -136
Decimal Output
▶ 240

Step 1: Divide 240 by 10. Quotient = 24, Remainder


=0
Step 2: Divide 24 by 10. Quotient = 2, Remainder = 4
Step 3: Divide 2 by 10. Quotient = 0, Remainder = 2
Decimal Output Algorithm
1. IF AX < 0 THEN
2. Print a minus sign
3. Replace AX by its 2’s complement
4. END IF
5. Get the digits in AX decimal representation
6. Convert these digits to character and print them
Line 5:
Count = 0
REPEAT
divide quotient by 10
push remainder on the stack
count = count + 1
UNTIL quotient = 0
contd...
Line 6
FOR count times DO
POP a digit from stack
Convert it into a character
Output the character
END FOR
Decimal Input Algorithm
Example: Input 123

Total = 0
Read an ASCII digit
REPEAT
convert character to binary value
total = 10 x total + value
read a character
UNTIL character is carriage return

You might also like