Multiplication and Division Instructions
Multiplication and Division Instructions
Multiplication and Division Instructions
Instructions
• MUL Instruction
• IMUL Instruction
• DIV Instruction
1
MUL Instruction
• The MUL (unsigned multiply) instruction multiplies an 8/16 bit
operand by AL/AX
Implied operands:
2
MUL Instruction
3
IMUL Instruction
• IMUL (signed integer multiply ) multiplies an 8/16 bit signed
operand by AL/AX
4
MUL & IMUL Examples
MOV AL, 80h ; AL = -128D
MOV BL, FFh ; BL = -1D
5
MUL & IMUL Examples
MOV AX, 1
MOV BX, FFFFh ; BX = 65535D/ -1D
6
MUL & IMUL Examples
MOV AX, FFFFh ; AX =65535D/-1
MOV BX, FFFFh ; BX =65535D/-1
7
Your turn . . .
What will be the hexadecimal values of DX and AX after the
following instructions execute?
MOV AX,0FFFh
MUL AX
IMUL AX
DX = ?
AX = ?
8
Your turn . . .
What will be the hexadecimal values of DX and AX after the
following instructions execute?
MOV AX,0100h
MOV CX,FFFFh
MUL CX
IMUL CX
DX = ?
AX = ?
9
Your turn . . .
What will be the hexadecimal values of DX and AXg after
the following instructions execute?
MOV AX,8760h
MOV BX,100h
MUL BX
IMUL BX
DX = ?
AX = ?
1
0
Your turn . . .
Translate the following high level language assignment into
assembly Language. Let A and B are word variables.
A = 5A-12B
Calculate factorial of N
N! =N*(N-1)*(N-2)*……*1
1
1
DIV Instruction
The DIV (unsigned divide) instruction performs 8-bit and
16-bit division on unsigned integers
Instruction formats:
DIV divisor
Divisor can be register or memory operand
DIV r/m16
DIV r/m32 Default Operands:
1
2
DIV Instruction
DIV r/m16
DIV r/m32
1
3
IDIV Instruction
• IDIV (signed divide) performs signed integer division
• Instruction formats:
IDIV divisor
1
4
DIV & IDIV Examples
MOV DX, 0000
MOV AX, 0005
MOV BX, 0002
1
5
DIV & IDIV Examples
MOV DX, 0000h
MOV AX, 0005h
MOV BX, 0FFFEh ; BX=65534D/-2D
1
6
DIV & IDIV Examples
MOV AX, 00FBh ; AX=251D
MOV BL, FFh ; BL =256D/-1D
1
7
DIV & IDIV Examples
MOV DX, FFFFh
MOV AX, FFFBh ; DX:AX=FFFFFFFB=4294967291D/-5D
MOV BX, 0002h
1
8
Your turn . . .
What will be the hexadecimal values of DX and AX after the
following instructions execute? Or, if divide overflow occurs,
you can indicate that as your answer:
MOV DX,0087H
MOV AX,6000H
MOV BX,100H
DIV BX
DX = ?, AX = ?
1
9
Your turn . . .
What will be the hexadecimal values of DX and AX after the
following instructions execute? Or, if divide overflow occurs,
you can indicate that as your answer:
MOV DX,0087H
MOV AX,6002H
MOV BX,10H
DIV BX
?????
2
0
Signed Integer Division
• Signed integers must be sign-extended before division takes
place
• fill high byte/word with a copy of the low byte/word's sign bit
• Byte Division
•For DIV, AH should be cleared
•For IDIV, AH should be made the sign extension of AL. The instruction
CBW (convert byte to word) will do this extension.
• Word Division
•For DIV, DX should be cleared
•For IDIV, DX should be made the sign extension of AX. The instruction
CWD (convert word to doubleword) will do this extension.
2
1
Signed Integer Division
• For example, the high byte contains a copy of the sign bit
from the low byte:
0 0 0 0 1 1 11 1 0 0 0 1 1 11
00000000 1 0 0 0 1 1 11 11111111 1 0 0 0 1 1 11
2
2
CBW, CWD Instructions
• The CBW and CWD instructions provide important
sign-extension operations:
• For example:
MOV AX,0FFFBH
CWD ; DX:AX = FFFFFFFBh
2
3
CBW & CWD Examples
Example 1:
MOV AL, -7
CBW
MOV BL, -7
IDIV BL
AL = 01 , AH = 00
Example 2:
MOV AX,VAR1
ADD AX,VAR2
MUL VAR3
MOV VAR4,AX ; save product
2
5
Implementing Arithmetic Expressions
2
7
Your turn . . .
; eax = quotient
2
8
Decimal Input
Example: Input of 123
number = 0
Read ‘1’
Convert ‘1’ to 1
number = 0*10 +1 =1
Read ‘2’
Convert ‘2’ to 2
number = 0*10 +2 =12
Read ‘3’
Convert ‘3’ to 3
number = 12*10 +3 =123
Decimal Output
Any Query?