In assembly language, multiplication and division are essential operations that allow you to perform arithmetic calculations at a low level. These operations are typically supported by specialized instructions that directly interact with the CPU's registers. Depending on the architecture, the exact behavior and syntax can vary, but the general principles remain similar.
In x86 assembly (which is a common architecture), there are specific instructions for multiplication and division that you can use. Below is a detailed explanation of both operations.
MULThe MUL instruction performs unsigned multiplication of two operands. It operates on the AX (for 16-bit) or EAX (for 32-bit) register, and the result is stored in DX:AX or EDX:EAX respectively (i.e., in two registers, because the result of multiplication can exceed the size of a single register).
; Multiplying two 16-bit numbers
MOV AX, 5 ; Load AX with 5 (the first number)
MOV BX, 3 ; Load BX with 3 (the second number)
MUL BX ; AX = AX * BX (AX = 5 * 3 = 15)
Here, MUL BX multiplies the value in AX (5) with BX (3), and stores the result in AX (since 15 is within the 16-bit range, it fits entirely in AX).
; Multiplying two 32-bit numbers
MOV EAX, 1000 ; Load EAX with 1000
MOV EBX, 2000 ; Load EBX with 2000
MUL EBX ; EDX:EAX = EAX * EBX (EAX = 1000 * 2000 = 2000000)
In this case, MUL EBX multiplies EAX (1000) with EBX (2000), and the result is stored across EDX:EAX. If the result exceeds 32 bits, the higher 32 bits will be stored in EDX, and the lower 32 bits in EAX.
IMULThe IMUL instruction performs signed multiplication. It works in the same way as MUL, but it accounts for the sign of the operands, meaning it handles both positive and negative numbers.
; Signed multiplication of two 16-bit numbers
MOV AX, -5 ; Load AX with -5
MOV BX, 3 ; Load BX with 3
IMUL BX ; AX = AX * BX (-5 * 3 = -15, stored in AX)
; Signed multiplication of two 32-bit numbers
MOV EAX, -1000 ; Load EAX with -1000
MOV EBX, 2000 ; Load EBX with 2000
IMUL EBX ; EDX:EAX = EAX * EBX (EAX = -1000 * 2000 = -2000000)
The IMUL instruction treats EAX and EBX as signed values and performs multiplication accordingly, storing the result in EDX:EAX.
DIVThe DIV instruction performs unsigned division. Like multiplication, division may result in a quotient and a remainder. The DIV instruction divides the value in the AX register (for 16-bit) or EAX register (for 32-bit) by the operand and stores the quotient in AX or EAX and the remainder in DX or EDX.
; Dividing two 16-bit numbers
MOV AX, 15 ; Load AX with 15 (the numerator)
MOV BX, 4 ; Load BX with 4 (the denominator)
DIV BX ; AX = AX / BX, DX = remainder (AX = 15 / 4 = quotient 3, remainder 3)
In this example:
; Dividing two 32-bit numbers
MOV EAX, 2000 ; Load EAX with 2000 (the numerator)
MOV EBX, 500 ; Load EBX with 500 (the denominator)
DIV EBX ; EAX = EAX / EBX, EDX = remainder (EAX = 2000 / 500 = quotient 4, remainder 0)
IDIVThe IDIV instruction performs signed division, which considers the signs of both the dividend and the divisor. As with DIV, the quotient is stored in AX (for 16-bit) or EAX (for 32-bit), and the remainder is stored in DX (for 16-bit) or EDX (for 32-bit).
; Signed division of two 16-bit numbers
MOV AX, -15 ; Load AX with -15 (the numerator)
MOV BX, 4 ; Load BX with 4 (the denominator)
IDIV BX ; AX = AX / BX, DX = remainder (AX = -15 / 4 = quotient -3, remainder 1)
In this example:
; Signed division of two 32-bit numbers
MOV EAX, -2000 ; Load EAX with -2000 (the numerator)
MOV EBX, 500 ; Load EBX with 500 (the denominator)
IDIV EBX ; EAX = EAX / EBX, EDX = remainder (EAX = -2000 / 500 = quotient -4, remainder 0)
In this case:
In signed division, overflow can occur if the dividend is too small or too large for the size of the result. For example, if dividing INT_MIN (the smallest signed integer) by -1, the result exceeds the range that can be represented in the register.
-32768 to 32767, and for 32-bit division, it's from -2147483648 to 2147483647.To avoid overflow, it's common practice to check for certain edge cases (like dividing INT_MIN by -1) before performing a division.
You can combine multiplication and division operations to calculate more complex results. Here's an example that multiplies two numbers and then divides the result by a third number.
section .data
; Data section for multiplication and division
num1 dw 50 ; First number
num2 dw 10 ; Second number
num3 dw 5 ; Third number
section .text
MOV AX, [num1] ; Load AX with num1 (50)
MOV BX, [num2] ; Load BX with num2 (10)
MOV CX, [num3] ; Load CX with num3 (5)
; Multiply num1 and num2
MUL BX ; AX = AX * BX (AX = 50 * 10 = 500)
; Divide the result by num3
MOV DX, 0 ; Clear DX (required for division)
DIV CX ; AX = AX / CX (AX = 500 / 5 = 100)
; At this point, AX contains the result 100
Multiplication (MUL and IMUL):
MUL is for unsigned multiplication.IMUL is for signed multiplication.Division (DIV and IDIV):
DIV is for unsigned division.IDIV is for signed division.Open this section to load past papers