Lec8 - Multiplication and Division
Lec8 - Multiplication and Division
2 Types:
Carry and Overflow flags are set when the product extends into its high
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
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