### Multiplication Instructions

# Multiplication Instructions

## Statement Purpose:

Objective of this lab is to introduce students with Integer multiplication in x86 assembly language that can be performed as a 32-bit, 16-bit, or 8-bit operation. In many cases, it revolves around EAX or one of its subsets (AX, AL). The MUL and IMUL instructions perform unsigned and signed integer multiplication, respectively.

## Activity Outcomes:

This lab teaches you the following topics:

**Students will know that MUL and IMUL instructions are very helpful in performing multiplication for unsigned and signed integers.****They will also learn the way these two differs from each other and the way we can use the suitable****IMUL comes with multiple operands so the students will be able to distinguish the flexibility provide by IMUL over MUL**

** **Instructor Note:

MUL Instruction

**The MUL (unsigned multiply) instruction multiplies an 8-, 16-, or 32-bit operand by either AL, AX, or**

The IMUL (signed multiply) instruction performs signed integer multiplication.

**Unlike the MUL instruction, IMUL preserves the sign of the****It does this by sign extending the highest bit of the lower half of the product into the upper bits of the**

** **__Introduction__

MUL instruction formats are:

**MUL r/m8****MUL r/m16****MUL r/m32**

The x86 instruction set supports three formats for the IMUL instruction:

**One operand****Two operands****Three operands**

The one-operand formats store the product in AX, DX:AX, or EDX:EAX:

The two-operand version of the IMUL instruction stores the product in the first operand, which must be a register.

**The second operand (the multiplier) can be a register, memory operand, or immediate value**

The three-operand formats store the product in the first operand.

**The second operand can be a 16-bit register or memory operand, which is multiplied by the third operand, an 8- or 16-bit immediate value:**

__Activities:__

## Activity 1:

**Unsigned Multiplication: Multiply AL by BL, storing the product in AX.**

## Solution:

The Carry flag is clear (CF = 0) because AH (the upper half of the product) equals zero

## Activity 2:

**Unsigned Multiplication: 100h * 2000h, using 16-bit operands.**

## Solution:

TITLE Lab Multiplication and Division Instructions

; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc

.data

val1 WORD 2000h val2 WORD 100h

.code

mov ax,val1

mul val2 ; DX:AX = 00200000h, CF=1

exit

main ENDP END main

The Carry flag indicates whether or not the upper half of the product contains significant digits.

## Activity 3:

**Unsigned Multiplication: 12345h * 1000h, using 32-bit operands.**

## Solution:

TITLE Lab Multiplication and Division Instructions

; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc

.data

.code Main proc

mov eax,12345h mov ebx,1000h

mul ebx ; EDX:EAX = 0000000012345000h, CF=0

exit

main ENDP END main

## Activity 4:

**Singed Multiplication; Multiply 48 by 4, producing +192 in AX.**

## Solution:

** **

Although the product is correct, AH is not a sign extension of AL, so the Overflow flag is set.

## Activity 5:

**Singed Multiplication; Multiply -4 by 4, producing -16 in AX.**

** Solution:**

AH is a sign extension of AL so the Overflow flag is clear.

## Activity 6:

**Singed Multiplication; Multiply 48 by 4, producing 192 in DX:AX.**

## Solution:

DX is a sign extension of AX, so the Overflow flag is clear

## Activity 7:

**Singed Multiplication; 4823424 * -423. **

**Solution:**

TITLE Lab Multiplication and Division Instructions

; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc

.data

.code Main proc

exit

mov eax,4823424 mov ebx,-423

imul ebx ; EDX:EAX = FFFFFFFF86635D80h, OF=0

main ENDP END main

## Activity 8:

**Singed Multiplication; Give examples of two operands as much as possible.**

## Solution:

TITLE Lab Multiplication and Division Instructions

; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc

.data | ||

word1 | SWORD | 4 |

dword1 | SDWORD | 4 |

.code | ||

mov | ax, -16 | ; AX = -16 |

mov | bx , 2 | ; BX = 2 |

mull | bx, ax | ; BX = -32 |

imul | bx, 2 | ; BX = -64 |

imul | bx, word1 | ; BX = -256 |

mov | eax, -16 | ; EAX = -16 |

mov | ebx, 2 | ; EBX = 2 |

imul | ebx, eax | ; EBX = -32 |

imul | ebx, 2 | ; EBX = -64 |

imul | ebx, dword1 | ; EBX = -256 |

exit

main ENDP END main

## Activity 9:

**Singed Multiplication; Give examples of three operands as much as possible.**

## Solution:

TITLE Lab Multiplication and Division Instructions

; Author: Ashfaq Hussain Farooqi INCLUDE Irvine32.inc

.data

word1 | SWORD | 4 | ||

dword1 | SDWORD | 4 | ||

.code | ||||

imul | bx, | word1, -16 | ; BX = -64 | |

imul | ebx, | dword1,-16 | ; EBX = -64 |

imul ebx, dword1,-2000000000 ; OF = 1 exit

main ENDP END main

__Home Activities:__

__Home Activities:__

## Activity 1:

**Take two unsigned numbers from the user and display its product (8Bit, 16Bit, and 32Bit variables).**

** **

## Activity 2:

**Take two signed numbers from the user and display its product (8Bit, 16Bit, and 32Bit variables).**

__Assignment:__

__Assignment:__

## Consider there are two arrays having six indexes. Populate them by taking inputs from the user. Now, populate the third array by multiplying the same index values. If the numbers are positive use MUL and if the numbers are signed use IMUL instruction.

**Note: Consider numbers as Signed/Unsigned and are 32Bit values.**