ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    COMP3137
    Progress0 / 73 topics
    Topics
    1. Introduction to Computer Organization2. Assembly Language3. Comparison of Low-Level and High-Level Languages4. Register Types (16-bit): General Purpose and Special Purpose Registers5. Introduction and Usage of RAM6. Processor7. Registers8. System Bus9. Instruction Execution Cycle10. Assembly and Machine Language11. Assembler12. Linker and Link Libraries13. Programmer's View of a Computer System14. RISC and CISC Architecture15. Physical Address Calculation16. Basic Memory Organization17. CPU Organization18. Top Level View of Computer Function and Interconnection19. Assembler Instruction Cycle20. Execute Cycle21. Interrupts22. Interrupt Cycle23. Memory Connection24. Input/Output Connection25. CPU Connection26. MASM27. MIPS28. Defining Data in MASM Assembler29. Elements of Assembly Language30. Integer Constants31. Integer Expressions32. Real Number Constants33. Character Constants34. String Constants35. Reserved Words36. Identifiers37. Directives38. Instructions39. The NOP (No Operation) Instruction40. Adding and Subtracting Integer41. INC and DEC Instructions42. NEG Instruction43. How to Move Integer Number in Register44. Adding and Subtracting Numbers in Registers45. Declaration and Initialization of Variables46. Moving Data from Variable to Register47. Data Definition Statement48. BYTE and SBYTE Data49. WORD and SWORD Data50. Defining DWORD and SDWORD Data51. Knowledge about Different Data Types52. Operations, Array & Loops53. Division and Multiplication in Assembly54. Jumps Based on Specific Flags55. Jumps Based on Equality56. Simple Jump Statements57. Jumps Based on Specific Condition58. Code Examples59. Practice on MASM60. Procedures61. File Operations Procedures62. Labels in Procedures63. Stack64. Runtime Stack65. Conditional Control Flow Directives66. Compound Expressions67. Data Representation & Conversion68. Architecture69. Data Path70. Control Unit71. Critical Path72. General Principles of Pipelining73. Pipelined Y86 Implementations
    COMP3137›Division and Multiplication in Assembly
    Computer Organization and Assembly LanguageTopic 53 of 73

    Division and Multiplication in Assembly

    7 minread
    1,177words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,177
      Code examples0
      DifficultyIntermediate