[go: up one dir, main page]

0% found this document useful (0 votes)
589 views31 pages

Multiplication and Division Instructions

The document discusses instructions for multiplication, division, and signed integer operations in assembly language. It provides examples of using the MUL, IMUL, DIV, and IDIV instructions and demonstrates how to implement arithmetic expressions. It also covers techniques for signed integer division and decimal input/output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
589 views31 pages

Multiplication and Division Instructions

The document discusses instructions for multiplication, division, and signed integer operations in assembly language. It provides examples of using the MUL, IMUL, DIV, and IDIV instructions and demonstrates how to implement arithmetic expressions. It also covers techniques for signed integer division and decimal input/output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Multiplication and Division

Instructions

• MUL Instruction

• IMUL Instruction

• DIV Instruction

• Signed Integer Division (IDIV Instruction)

• Implementing Arithmetic Expressions

• Decimal Input & Output

1
MUL Instruction
• The MUL (unsigned multiply) instruction multiplies an 8/16 bit
operand by AL/AX

• The instruction formats are:


MUL multiplier

• Multiplier can be register or memory

Implied operands:

2
MUL Instruction

3
IMUL Instruction
• IMUL (signed integer multiply ) multiplies an 8/16 bit signed
operand by AL/AX

• Preserves the sign of the product by sign-extending it into the


upper half of the destination register

• The instruction formats are:


IMUL multiplier ; AX = 00C0h, OF=1

• Multiplier can be register or memory

4
MUL & IMUL Examples
MOV AL, 80h ; AL = -128D
MOV BL, FFh ; BL = -1D

Instructions Decimal Product Hex Product AH AL


MUL BL 128 7F80 7F 80
IMUL BL 128 0080 00 80

5
MUL & IMUL Examples
MOV AX, 1
MOV BX, FFFFh ; BX = 65535D/ -1D

Instructions Decimal Product Hex Product DX AX


MUL BX 65535 0000FFFF 0000 FFFF
IMUL BX -1 FFFFFFFF FFFF FFFF

6
MUL & IMUL Examples
MOV AX, FFFFh ; AX =65535D/-1
MOV BX, FFFFh ; BX =65535D/-1

Instructions Decimal Product Hex Product DX AX


MUL BX 4294836225 FFFF0001 FFFF 0001
IMUL BX 1 00000001 0000 0001

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

• Divisor can be register or memory operand

• Uses same operands as DIV

1
4
DIV & IDIV Examples
MOV DX, 0000
MOV AX, 0005
MOV BX, 0002

Instructions Decimal Quotient Decimal remainder AX DX


DIV BX 2 1 0002 0001
IDIV BX 2 1 0002 0001

1
5
DIV & IDIV Examples
MOV DX, 0000h
MOV AX, 0005h
MOV BX, 0FFFEh ; BX=65534D/-2D

Instructions Decimal Quotient Decimal remainder AX DX


DIV BX 0 5 0000 0005
IDIV BX -2 1 FFFE 0001

1
6
DIV & IDIV Examples
MOV AX, 00FBh ; AX=251D
MOV BL, FFh ; BL =256D/-1D

Instructions Decimal Quotient Decimal remainder AH AL


DIV BL 0 251 FB 00
IDIV BL -251(Too Big) Divide Overflow

1
7
DIV & IDIV Examples
MOV DX, FFFFh
MOV AX, FFFBh ; DX:AX=FFFFFFFB=4294967291D/-5D
MOV BX, 0002h

Instructions Decimal Quotient Decimal remainder AX DX


DIV BX 2147483646/7FFFFFFEh Divide Overflow
(Too Big)
IDIV BX -2 -1 FFFE FFFF

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:

• CBW (convert byte to word) extends sign bit of AL into AH


• CWD (convert word to doubleword) extends sign bit of AX into DX

• 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, -50


CWD
MOV BX, 7
IDIV BX

AX = FFF9h (-7) , DX = FFFFh (-1) 2


4
Implementing Arithmetic Expressions
Example: var4 = (var1 + var2) * var3

MOV AX,VAR1
ADD AX,VAR2
MUL VAR3
MOV VAR4,AX ; save product

Example: eax = (-var1 * var2) + var3


MOV AX,VAR1
NEG AX
MUL VAR2
ADD AX,VAR3

2
5
Implementing Arithmetic Expressions

Example: var4 = (var1 * 5) / (var2 – 3)

MOV AX,VAR1 ; left side


MOV BX,5
MUL BX ; DX:AX = product
MOV BX,VAR2 ; right side
SUB BX,3
DIV BX ; final division
MOV VAR4,AX
Implementing Arithmetic Expressions

Example: var4 = (var1 * -5) / (-var2 % var3);

MOV AX,VAR2 ; begin right side


NEG AX
CWD ; sign-extend dividend
IDIV VAR3 ; DX = remainder
MOV BX,DX ; BX = right side
MOV AX,-5 ; begin left side
IMUL VAR1 ; DX:AX = left side
IDIV BX ; final division
MOV VAR4,AX ; quotient

2
7
Your turn . . .

Implement the following expression. Do not modify any


variables other than var3:

var3 = (var1 * -var2) / (var3 – ebx)

; 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?

You might also like