Understanding Assembly Language: The MUL Multiplication Instruction

In assembly language, the <span>mul</span> instruction is used to perform unsigned multiplication operations. It is one of the basic arithmetic instructions in the x86 architecture, used for handling 8-bit, 16-bit, and 32-bit multiplication operations.<span>mul</span> instruction is characterized by its operands being implicitly specified, with the result stored in fixed registers.

Basic Rules of the MUL Instruction

1. Operand Rules

  • 8-bit Multiplication:

    • One multiplicand must be in the AL register
    • The other multiplicand can be an 8-bit register or a memory byte
    • The result is stored in the AX register
  • 16-bit Multiplication:

    • One multiplicand must be in the AX register
    • The other multiplicand can be a 16-bit register or a memory word
    • The high 16 bits of the result are stored in the DX register, and the low 16 bits are stored in the AX register
  • 32-bit Multiplication (in 80386 and later):

    • One multiplicand must be in the EAX register
    • The other multiplicand can be a 32-bit register or a memory double word
    • The high 32 bits of the result are stored in the EDX register, and the low 32 bits are stored in the EAX register

2. Instruction Format

mul reg        ; Use a register as the multiplicand
mul mem        ; Use a memory location as the multiplicand

Code Examples

Example 1: 8-bit Multiplication (100 × 10)

assume cs:code

code segment
start:
    mov al, 100     ; Load 100 into AL register
    mov bl, 10      ; Load 10 into BL register
    mul bl          ; Perform multiplication: (AX) = (AL) * (BL) = 100 * 10 = 1000

    ; At this point (AX) = 03E8h (1000 in hexadecimal)

    mov ax, 4c00h   ; End program
    int 21h
code ends
end start

Example 2: 16-bit Multiplication (1000 × 100)

assume cs:code

code segment
start:
    mov ax, 1000    ; Load 1000 into AX register
    mov bx, 100     ; Load 100 into BX register
    mul bx          ; Perform multiplication: (DX:AX) = (AX) * (BX) = 1000 * 100 = 100000

    ; At this point (AX) = 86A0h (100000 mod 65536 = 34464)
    ;      (DX) = 0001h (100000 / 65536 = 1)

    mov ax, 4c00h   ; End program
    int 21h
code ends
end start

Example 3: Multiplication Using Memory Operands

assume cs:code, ds:data

data segment
    multiplier db 5     ; Define a byte multiplicand
    value dw 2000       ; Define a word multiplicand
    result dd ?         ; For storing 32-bit result
data ends

code segment
start:
    mov ax, data
    mov ds, ax

    ; 8-bit multiplication example
    mov al, 10          ; AL = 10
    mul multiplier      ; AX = AL * [multiplier] = 10 * 5 = 50

    ; 16-bit multiplication example
    mov ax, 100         ; AX = 100
    mul value           ; DX:AX = AX * [value] = 100 * 2000 = 200000

    ; Store 32-bit result
    mov word ptr [result], ax    ; Store low 16 bits
    mov word ptr [result+2], dx  ; Store high 16 bits

    mov ax, 4c00h       ; End program
    int 21h
code ends
end start

Example 4: More Complex Multiplication Application

assume cs:code, ds:data

data segment
    array dw 100, 200, 300, 400, 500  ; Define a word array
    count dw 5                         ; Number of array elements
    product dd 1                       ; For storing product, initialized to 1
data ends

code segment
start:
    mov ax, data
    mov ds, ax

    mov cx, [count]     ; Set loop counter
    mov si, 0           ; Array index

calculate_product:
    mov ax, [product]   ; Load current product low 16 bits into AX
    mov dx, [product+2] ; Load current product high 16 bits into DX

    ; Multiply by the next element in the array
    mul word ptr array[si]

    ; Update product
    mov [product], ax   ; Store new low 16 bits
    mov [product+2], dx ; Store new high 16 bits

    add si, 2           ; Move to the next array element (each word is 2 bytes)
    loop calculate_product

    ; At this point [product] contains the product of all array elements

    mov ax, 4c00h       ; End program
    int 21h
code ends
end start

Considerations for the MUL Instruction

  1. Operand Types:<span>mul</span> instruction can only be used for multiplication of unsigned numbers. For signed number multiplication, use the <span>imul</span> instruction.

  2. Flag Effects:

  • If the high part of the result (for 8-bit multiplication is AH, for 16-bit multiplication is DX) is not zero, then the CF and OF flags are set to 1
  • If the high part of the result is zero, then the CF and OF flags are cleared to 0
  • The SF, ZF, AF, and PF flags are undefined
  • Efficiency Considerations:<span>mul</span> instruction’s execution time depends on the size and value of the operands. Generally, multiplication requires more clock cycles than addition or subtraction.

  • Result Registers:

    • For 8-bit multiplication: the result is always stored in AX
    • For 16-bit multiplication: the result is stored in DX:AX (DX is high 16 bits, AX is low 16 bits)
    • For 32-bit multiplication: the result is stored in EDX:EAX (EDX is high 32 bits, EAX is low 32 bits)

    Practical Application Scenarios

    1. Mathematical Calculations: Performing various mathematical operations, such as calculating area, volume, etc.
    2. Array Processing: Calculating the product or weighted sum of array elements
    3. Graphics Processing: Performing coordinate transformations and scaling calculations in graphics algorithms
    4. Cryptographic Algorithms: Executing large number multiplications in cryptographic algorithms
    5. Physical Simulations: Calculating forces, velocities, and other physical quantities in physics engines

    Differences Between MUL and IMUL Instructions

    Feature MUL IMUL
    Operand Type Unsigned Numbers Signed Numbers
    Number of Operands 1 Explicit Operand 1, 2, or 3 Operands
    Result Storage Fixed Registers (AX/DX:AX/EDX:EAX) Target Register Can Be Specified
    Flags Affects CF and OF Affects CF and OF, as well as SF, ZF, etc.

    Conclusion

    <span>mul</span> instruction is a fundamental instruction in x86 assembly language for performing unsigned multiplication operations. It is characterized by using implicit operands (AL/AX/EAX) and fixed result registers (AX/DX:AX/EDX:EAX). By using the <span>mul</span> instruction appropriately, various multiplication operations can be performed, from simple numerical calculations to complex algorithm implementations. Care should be taken to match the operand sizes and correctly use the result registers to ensure the accuracy of the calculation results.

    Leave a Comment