ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3137Division Multiplicatio…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3137

    Computer Organization and Assembly Language

    Progress0 / 73 done
    Topics73
    1Introduction to Computer Organization2Assembly Language3Comparison of Low-Level and High-Level Languages
    4General Purpose and Special Purpose Registers5Introduction and Usage of RAM6Processor7Registers8System Bus9Instruction Execution Cycle10Assembly and Machine Language11Assembler12Linker and Link Libraries13Programmer's View of a Computer System14RISC and CISC Architecture15Physical Address Calculation16Basic Memory Organization17CPU Organization18Top Level View of Computer Function and Interconnection19Assembler Instruction Cycle20Execute Cycle21Interrupts22Interrupt Cycle23Memory Connection24Input/Output Connection25CPU Connection26MASM27MIPS28Defining Data in MASM Assembler29Elements of Assembly Language30Integer Constants31Integer Expressions32Real Number Constants33Character Constants34String Constants35Reserved Words36Identifiers37Directives38Instructions39The NOP (No Operation) Instruction40Adding and Subtracting Integer41INC and DEC Instructions42NEG Instruction43How to Move Integer Number in Register44Adding and Subtracting Numbers in Registers45Declaration and Initialization of Variables46Moving Data from Variable to Register47Data Definition Statement48BYTE and SBYTE Data49WORD and SWORD Data50Defining DWORD and SDWORD Data51Knowledge about Different Data Types52Operations, Array & Loops53Division and Multiplication in Assembly54Jumps Based on Specific Flags55Jumps Based on Equality56Simple Jump Statements57Jumps Based on Specific Condition58Code Examples59Practice on MASM60Procedures61File Operations Procedures62Labels in Procedures63Stack64Runtime Stack65Conditional Control Flow Directives66Compound Expressions67Data Representation & Conversion68Architecture69Data Path70Control Unit71Critical Path72General Principles of Pipelining73Pipelined Y86 Implementations
    7 min read
    Topic 53 of 73
    Topic Study Note Illustration
    53

    Division and Multiplication in Assembly

    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...

    Division and Multiplication in Assembly Language

    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.


    1. Multiplication in Assembly

    Unsigned Multiplication: MUL

    The 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).

    16-bit Example (16-bit registers):

    ; 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).

    32-bit Example (32-bit registers):

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

    Signed Multiplication: IMUL

    The 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.

    16-bit Example (Signed multiplication):

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

    32-bit Example (Signed multiplication):

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


    2. Division in Assembly

    Unsigned Division: DIV

    The 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.

    16-bit Example (Unsigned division):

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

    • The quotient 3 is stored in AX.
    • The remainder 3 is stored in DX.

    32-bit Example (Unsigned division):

    ; 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)
    
    • The quotient 4 is stored in EAX.
    • The remainder 0 is stored in EDX.

    Signed Division: IDIV

    The 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).

    16-bit Example (Signed division):

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

    • The quotient -3 is stored in AX.
    • The remainder 1 is stored in DX.

    32-bit Example (Signed division):

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

    • The quotient -4 is stored in EAX.
    • The remainder 0 is stored in EDX.

    3. Handling Overflow and Errors in Division

    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.

    • For 16-bit signed division, the valid range of the dividend is from -32768 to 32767, and for 32-bit division, it's from -2147483648 to 2147483647.
    • If the division operation results in a value outside of this range, an overflow condition will occur.

    To avoid overflow, it's common practice to check for certain edge cases (like dividing INT_MIN by -1) before performing a division.


    4. Example: Multiplying and Dividing Together

    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
    

    Summary

    • Multiplication (MUL and IMUL):

      • MUL is for unsigned multiplication.
      • IMUL is for signed multiplication.
      • The result is stored across registers like AX (for 16-bit) or EAX (for 32-bit), with EDX or DX storing the high-order bits if necessary.
    • Division (DIV and IDIV):

      • DIV is for unsigned division.
      • IDIV is for signed division.
      • The quotient is stored in AX (for 16-bit) or EAX (for 32-bit), and the remainder is stored in DX (for
    ← Previous topic 52
    Operations, Array & Loops
    Next topic 54 →
    Jumps Based on Specific Flags

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language