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
    COMP2118
    Progress0 / 35 topics
    Topics
    1. Introduction to Computer Systems2. Information is Bits + Context3. Programs are Translated by Other Programs4. Understanding Compilation Systems5. Processors Read and Interpret Instructions6. Caches Matter7. Storage Devices Form a Hierarchy8. The Operating System Manages the Hardware9. Systems Communicate Using Networks10. Representing and Manipulating Information11. Information Storage12. Integer Representations13. Integer Arithmetic14. Floating Point15. Machine-Level Representation of Programs16. A Historical Perspective17. Program Encodings18. Data Formats19. Accessing Information20. Arithmetic and Logical Operations21. Control22. Procedures23. Array Allocation and Access24. Heterogeneous Data Structures25. Understanding Pointers26. Using the GDB Debugger27. Out-of-Bounds Memory References and Buffer Overflow28. x86-64: Extending IA-32 to 64 Bits29. Machine-Level Representations of Floating-Point Programs30. Processor Architecture31. The Y86 Instruction Set Architecture32. Logic Design and the Hardware Control Language (HCL)33. Sequential Y86 Implementations34. General Principles of Pipelining35. Pipelined Y86 Implementations
    COMP2118›Integer Arithmetic
    Computer Organization and Assembly LanguageTopic 13 of 35

    Integer Arithmetic

    6 minread
    1,007words
    Intermediatelevel

    Integer Arithmetic in Computer Organization and Assembly Language

    Integer arithmetic refers to the set of operations that deal with integers (whole numbers), including addition, subtraction, multiplication, and division. These operations are crucial for performing calculations in computer programs and are implemented at the hardware level in a computer system. Understanding how these operations are performed is essential for understanding the functioning of a computer’s processor (CPU) and how assembly language manipulates data.

    In the context of computer organization and assembly language, integer arithmetic involves:

    • Representation of integers in binary
    • Arithmetic operations (addition, subtraction, multiplication, division)
    • Handling overflow and underflow
    • Using registers to perform calculations

    Let's break this down:

    1. Binary Representation of Integers

    Before performing any arithmetic, integers must be represented in binary form, because the processor operates on binary data (0s and 1s).

    • Unsigned Integers: These are positive integers and zero, represented in binary with all bits contributing to the magnitude of the number.
    • Signed Integers: These can be positive or negative. The most common method for representing signed integers in binary is two's complement representation. In this system, the leftmost bit (the "most significant bit") is the sign bit. If the sign bit is 0, the number is positive; if it is 1, the number is negative.

    For example:

    • 8-bit unsigned integer 5 is represented as 00000101 in binary.
    • 8-bit signed integer -5 is represented as 11111011 in two’s complement.

    2. Integer Arithmetic Operations

    Here are the main integer arithmetic operations, how they work in binary, and their assembly language equivalents:

    a. Addition (ADD)

    • The addition operation sums two integers.
    • In binary, addition works similarly to decimal addition, carrying over when the sum of bits exceeds the base (2 in binary).
    • Carry occurs when the sum of bits exceeds 1 (i.e., 1 + 1 = 10 in binary).

    Example (binary addition):

      1010  (10 in decimal)
    + 1101  (13 in decimal)
    --------
      10111 (23 in decimal)
    

    In assembly, the ADD instruction performs the addition. In some architectures, the carry flag (CF) and overflow flag (OF) are set to indicate whether there was a carry or overflow during the addition.

    b. Subtraction (SUB)

    • Subtraction is the process of subtracting one integer from another.
    • In binary, subtraction is performed using the two's complement method, where the negative number is added to the positive number.

    Example (binary subtraction):

      1101  (13 in decimal)
    - 1010  (10 in decimal)
    --------
      0011  (3 in decimal)
    

    In assembly, the SUB instruction is used. If there’s a borrow (similar to carry in addition), it’s indicated by the borrow flag (in some architectures).

    c. Multiplication (MUL)

    • Multiplication is repeated addition. In binary, it’s done by adding shifted versions of the multiplicand.
    • Most processors implement hardware-based multiplication for efficiency.

    Example (binary multiplication):

      0011 (3 in decimal)
    x 0010 (2 in decimal)
    ---------
      0000 (0, shifted)
    + 0011  (3, shifted once)
    ---------
      0110 (6 in decimal)
    

    In assembly, the MUL instruction is used for unsigned multiplication, while some processors may provide specific instructions like IMUL for signed multiplication.

    d. Division (DIV)

    • Division is the process of repeatedly subtracting the divisor from the dividend.
    • In binary, division is performed by shifting and subtracting (similar to long division in decimal).

    Example (binary division):

      1010 (10 in decimal)
    ÷  0010 (2 in decimal)
    ---------
      0101 (5 in decimal)
    

    The DIV instruction in assembly performs unsigned division. For signed division, IDIV (signed division) may be used in certain architectures.

    3. Overflow and Underflow

    • Overflow occurs when the result of an arithmetic operation exceeds the range that can be represented by the number of bits in the operand. This happens in both addition and multiplication.

      Example of overflow:

      • In 8-bit unsigned arithmetic, adding 255 + 1 would cause an overflow, since the result 256 cannot be represented in 8 bits.
    • Underflow occurs in signed integer arithmetic when a result is smaller than the smallest value that can be represented.

      • In 8-bit signed integers, subtracting -128 - 1 results in an underflow because the result -129 is out of range.

    When an overflow or underflow occurs, the overflow flag (OF) or carry flag (CF) may be set, depending on the architecture.

    4. Using Registers and Flags

    • Arithmetic operations in assembly are typically performed on registers, which are small, fast storage locations in the CPU.

    • A typical operation involves loading operands into registers, performing the operation, and then storing the result.

    • Flags: Processors maintain a set of status flags to indicate the result of arithmetic operations:

      • Zero Flag (ZF): Set if the result is zero.
      • Carry Flag (CF): Set if a carry out occurs in unsigned operations (e.g., in addition).
      • Overflow Flag (OF): Set if the result overflows in signed arithmetic.
      • Sign Flag (SF): Indicates if the result is negative.

    5. Example of Assembly Code for Integer Arithmetic

    Here is an example of assembly code that performs basic integer addition and subtraction on a system with x86 assembly syntax:

    ; Add two numbers
    MOV AX, 5       ; Load 5 into register AX
    MOV BX, 3       ; Load 3 into register BX
    ADD AX, BX      ; AX = AX + BX (5 + 3 = 8)
    
    ; Subtract two numbers
    MOV CX, 10      ; Load 10 into register CX
    MOV DX, 4       ; Load 4 into register DX
    SUB CX, DX      ; CX = CX - DX (10 - 4 = 6)
    

    6. Handling Negative Numbers

    For negative numbers, two's complement is used:

    • To get the two's complement of a number, invert the bits and add 1.
    • When performing arithmetic with negative numbers, care must be taken to handle sign extension (i.e., ensuring that the sign bit is correctly extended into larger registers).

    Summary

    In integer arithmetic within computer organization and assembly language, understanding binary representation, arithmetic operations (addition, subtraction, multiplication, and division), and managing overflow/underflow are essential. Additionally, using registers and flags to manage results and errors is key for efficient and accurate processing. This knowledge enables you to write efficient low-level code and understand how modern processors execute these operations at the hardware level.

    Previous topic 12
    Integer Representations
    Next topic 14
    Floating Point

    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 time6 min
      Word count1,007
      Code examples0
      DifficultyIntermediate