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 PortalCOMP2118Integer Arithmetic
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2118

    Computer Organization and Assembly Language

    Progress0 / 35 done
    Topics35
    1Introduction to Computer Systems2Information is Bits + Context3Programs are Translated by Other Programs4Understanding Compilation Systems5Processors Read and Interpret Instructions6Caches Matter7Storage Devices Form a Hierarchy8The Operating System Manages the Hardware9Systems Communicate Using Networks10Representing and Manipulating Information11Information Storage12Integer Representations13Integer Arithmetic14Floating Point15Machine-Level Representation of Programs16A Historical Perspective17Program Encodings18Data Formats19Accessing Information20Arithmetic and Logical Operations21Control22Procedures23Array Allocation and Access24Heterogeneous Data Structures25Understanding Pointers26Using the GDB Debugger27Out-of-Bounds Memory References and Buffer Overflow
    28Extending IA-32 to 64 Bits29Machine-Level Representations of Floating-Point Programs30Processor Architecture31The Y86 Instruction Set Architecture32Logic Design and the Hardware Control Language (HCL)33Sequential Y86 Implementations34General Principles of Pipelining35Pipelined Y86 Implementations
    6 min read
    Topic 13 of 35
    Topic Study Note Illustration
    13

    Integer Arithmetic

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

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.0kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language