You are on page 1of 10

Integer Multiplication and

Division
Assembly Language Programming
Multiply Instruction

Signed Multiply • Byte form


IMUL source • AX=AL*source
Unsigned Multiply • Word form
MUL source • DX:AX=AX*source
• source can be a register • CF=OF
or memory location • 1 if answer size > operand size
• 0 otherwise
(not a constant)
• SF, ZF, AF, and PF
• Undefined

2/4/2015 Ali Saleh - Assembly Language Programming 2


IMUL Example

mov BL,0FEh
mov AL,0E5h
imul BL ;AX=AL*BL
• BL contains -2, AL contains -27
• After IMUL
• AX contains 0036h (54d)
• CF=OF=0 (result still fits in byte)
• SF, AF, PF, and ZF are undefined

2/4/2015 Ali Saleh - Assembly Language Programming 3


MUL Example

mov BL,0FEh
mov AL,0E5h
mul BL
• BL contains 254, AL contains 229
• After MUL
• AX contains 0E336h (58166d)
• CF=OF=1 (result requires a word)
• SF, AF, PF, and ZF are undefined

2/4/2015 Ali Saleh - Assembly Language Programming 4


Another IMUL Example

mov AWORD,-136
mov AX,6784
imul AWORD
• AWORD contains 0FF78h, AX is 1A80h
• After IMUL
• DX:AX contains 0FFF1EC00h (-922624d)
• CF=OF=1 (result requires a doubleword)

2/4/2015 Ali Saleh - Assembly Language Programming 5


Another MUL Example

mov AWORD,-136
mov AX,6784
mul AWORD
• AWORD is 0FF78h (65400d), AX is 1A80h
• After MUL
• DX:AX contains 1A71EC00h (443673600d)
• CF=OF=1 (result requires a doubleword)

2/4/2015 Ali Saleh - Assembly Language Programming 6


One More IMUL Example

mov AX,-1
mov BX,2
imul BX
• AX is 0FFFFh, BX is 0002h
Same as
• After IMUL 0FFFEh
• DX:AX contains 0FFFFFFFEh (-2d)
• CF=OF=0 (result still fits in a word)

2/4/2015 Ali Saleh - Assembly Language Programming 7


Divide Instruction

Signed Divide • Byte form


IDIV divisor • AL=AX / divisor
• AH=AX % divisor
Unsigned Divide
• Word form
DIV divisor
• AX=DX:AX / divisor
• divisor can be a register • DX=DX:AX % divisor
or memory location • All Flags undefined
(not a constant)
• For IDIV
• sign of dividend = sign of
remainder

2/4/2015 Ali Saleh - Assembly Language Programming 8


Divide Overflow

• Occurs when result will not fit in Word/Byte


• Terminates program with Divide Overflow message!
• Need to anticipate and avoid: It will occur if
• Byte form: AH>=divisor
• Word form: DX>=divisor

2/4/2015 Ali Saleh - Assembly Language Programming 9


Preparing Dividend

• To divide a word in AX by a word, AX must be converted to a


doubleword in DX:AX
• If signed: CWD (called sign-extension)
• If unsigned: MOV DX,0
• For byte (in AL) to word (AX) conversion
• If signed: CBW (these do not affect Flags)
• If unsigned: MOV AH,0

2/4/2015 Ali Saleh - Assembly Language Programming 10

You might also like