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
    DC-221
    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
    DC-221›Arithmetic and Logical Operations
    Computer Organization and Assembly LanguageTopic 20 of 35

    Arithmetic and Logical Operations

    10 minread
    1,737words
    Intermediatelevel

    Arithmetic and Logical Operations in Computer Organization and Assembly Language

    In the context of Computer Organization and Assembly Language, arithmetic and logical operations form the basis of low-level programming and how a computer performs calculations and makes decisions. These operations are handled at the hardware level (i.e., by the Arithmetic and Logic Unit, or ALU) and are implemented using assembly language instructions.

    1. Arithmetic Operations

    Arithmetic operations are basic mathematical operations that a processor can perform on numbers. In computer organization, these operations are performed using binary numbers, and assembly language provides instructions to perform these operations.

    Common Arithmetic Operations:

    • Addition (ADD): Adds two operands.
    • Subtraction (SUB): Subtracts one operand from another.
    • Multiplication (MUL): Multiplies two operands.
    • Division (DIV): Divides one operand by another.

    a) Addition (ADD)

    • The ADD instruction performs the addition of two operands and stores the result in a register or memory.

      Example in Assembly (x86 architecture):

      ADD AX, BX  ; Adds the contents of register BX to AX, result stored in AX
      
    • Flags affected:

      • Carry flag (CF): Set if there is a carry-out from the most significant bit.
      • Zero flag (ZF): Set if the result is zero.
      • Sign flag (SF): Set if the result is negative (in two's complement).

    b) Subtraction (SUB)

    • The SUB instruction subtracts the second operand from the first operand and stores the result in the destination operand.

      Example in Assembly (x86 architecture):

      SUB AX, BX  ; Subtracts the contents of BX from AX, result stored in AX
      
    • Flags affected:

      • Carry flag (CF): Set if there is a borrow in the subtraction.
      • Zero flag (ZF): Set if the result is zero.
      • Sign flag (SF): Set if the result is negative.

    c) Multiplication (MUL)

    • The MUL instruction multiplies two unsigned integers and stores the result in registers.

      Example in Assembly (x86 architecture):

      MUL BX  ; Multiplies AX by BX and stores the result in DX:AX (for 16-bit multiplication)
      
    • Flags affected:

      • Overflow flag (OF): Set if the result cannot fit in the destination register.
      • Zero flag (ZF): Set if the result is zero.

    d) Division (DIV)

    • The DIV instruction divides an unsigned integer (usually stored in a combination of registers) by another operand.

      Example in Assembly (x86 architecture):

      DIV BX  ; Divides DX:AX by BX, quotient in AX, remainder in DX
      
    • Flags affected:

      • Zero flag (ZF): Set if the quotient is zero.
      • Overflow flag (OF): Set if there is a division by zero or if the quotient exceeds the size of the destination register.

    e) Increment (INC)

    • Increments the value of an operand (adds 1).

      Example in Assembly (x86 architecture):

      INC AX  ; Increments the value in AX by 1
      

    f) Decrement (DEC)

    • Decrements the value of an operand (subtracts 1).

      Example in Assembly (x86 architecture):

      DEC AX  ; Decrements the value in AX by 1
      

    2. Logical Operations

    Logical operations are operations that manipulate binary values, performing bitwise operations. These operations are essential for making decisions based on conditions, comparisons, and controlling the flow of programs.

    Common Logical Operations:

    • AND (AND): Performs a bitwise AND on two operands.
    • OR (OR): Performs a bitwise OR on two operands.
    • XOR (XOR): Performs a bitwise XOR (exclusive OR) on two operands.
    • NOT (NOT): Performs a bitwise NOT (inversion) on an operand.
    • Shift Operations (SHL, SHR): Shifts the bits of an operand left or right.

    a) AND (AND)

    • The AND instruction compares corresponding bits of two operands and sets the result bit to 1 if both bits are 1; otherwise, the result bit is 0.

      Example in Assembly (x86 architecture):

      AND AX, BX  ; Performs bitwise AND between AX and BX, result stored in AX
      
    • Flags affected:

      • Zero flag (ZF): Set if the result is zero.
      • Sign flag (SF): Set if the result is negative.

    b) OR (OR)

    • The OR instruction compares corresponding bits of two operands and sets the result bit to 1 if either bit is 1; otherwise, the result bit is 0.

      Example in Assembly (x86 architecture):

      OR AX, BX  ; Performs bitwise OR between AX and BX, result stored in AX
      
    • Flags affected:

      • Zero flag (ZF): Set if the result is zero.
      • Sign flag (SF): Set if the result is negative.

    c) XOR (XOR)

    • The XOR instruction compares corresponding bits of two operands and sets the result bit to 1 if the bits are different (1 and 0, or 0 and 1), and 0 if the bits are the same.

      Example in Assembly (x86 architecture):

      XOR AX, BX  ; Performs bitwise XOR between AX and BX, result stored in AX
      
    • Flags affected:

      • Zero flag (ZF): Set if the result is zero (when both operands are identical).
      • Sign flag (SF): Set if the result is negative.

    d) NOT (NOT)

    • The NOT instruction inverts each bit of the operand (also known as a bitwise negation).

      Example in Assembly (x86 architecture):

      NOT AX  ; Inverts each bit of AX
      
    • Flags affected:

      • Zero flag (ZF): Set if the result is zero.
      • Sign flag (SF): Set if the result is negative.

    e) Shift Operations

    • Shift Left (SHL): The SHL instruction shifts the bits of the operand to the left by a specified number of positions. This operation effectively multiplies the number by 2 for each shift.

      Example in Assembly (x86 architecture):

      SHL AX, 1  ; Shifts the bits of AX left by 1 position (multiplies AX by 2)
      
    • Shift Right (SHR): The SHR instruction shifts the bits of the operand to the right by a specified number of positions. This operation effectively divides the number by 2 for each shift (ignoring the remainder).

      Example in Assembly (x86 architecture):

      SHR AX, 1  ; Shifts the bits of AX right by 1 position (divides AX by 2)
      
    • Flags affected:

      • Carry flag (CF): Holds the last bit shifted out (from the left or right).
      • Zero flag (ZF): Set if the result is zero.
      • Sign flag (SF): Set if the result is negative.

    f) Rotate Operations

    • Rotate Left (ROL): The ROL instruction rotates the bits of the operand to the left, meaning the most significant bit is moved to the least significant bit position.

      Example in Assembly (x86 architecture):

      ROL AX, 1  ; Rotates the bits of AX left by 1 position
      
    • Rotate Right (ROR): The ROR instruction rotates the bits of the operand to the right, meaning the least significant bit is moved to the most significant bit position.

      Example in Assembly (x86 architecture):

      ROR AX, 1  ; Rotates the bits of AX right by 1 position
      

    3. Comparison Operations

    Although not strictly arithmetic or logical operations, comparison operations are critical for decision-making in programming. These operations often set flags in the processor, which are later used by conditional jump instructions (like JZ, JNE, etc.) to control the flow of a program.

    • CMP: Compares two operands by subtracting the second operand from the first, without storing the result. It affects the flags and is used for decision-making.

      Example:

      CMP AX, BX  ; Compares AX and BX by subtracting BX from AX
      
    • TEST: Performs a bitwise AND between two operands and updates the flags based on the result. It's often used for testing specific bits in a register or memory.

      Example:

      TEST AX, BX  ; Performs bitwise AND on AX and BX and updates the flags
      

    4. Flag Registers and Their Role in Arithmetic and Logical Operations

    Flags are special bits in the CPU that indicate the results of arithmetic and logical operations. The Flag Register holds several individual flags that are affected by various operations:

    • Zero Flag (ZF):
      Set if the result of the operation is zero.

    • Carry Flag (CF):
      Set if there is a carry out or borrow in arithmetic operations (e.g., in addition or subtraction).

    • Sign Flag (SF):
      Set if the result is negative (for signed operations).

    • Overflow Flag (OF):
      Set if an overflow occurs during an arithmetic operation (i.e., when the result cannot be represented with the given number of bits).

    • Parity Flag (PF):
      Set if the number of 1s in the result is even.

    5. Control Flow Based on Arithmetic and Logical Results

    Many programs use conditional statements to decide what action to take based on the results of arithmetic and logical operations. For example:

    • Jump Instructions:
      Based on the state of the flags (such as zero, carry, overflow), the CPU can jump to a different part of the program.

      • Example:
        JE label (Jump if Equal, i.e., Zero flag is set)
    • Looping and Conditional Execution:
      The results of logical comparisons can control whether a loop continues or if a certain block of code is executed.

    Summary of Arithmetic and Logical Operations

    Operation Description Example
    Addition (ADD) Adds two numbers together. 5 + 3 = 8
    Subtraction (SUB) Subtracts one number from another. 5 - 3 = 2
    Multiplication (MUL) Multiplies two numbers together. 5 * 3 = 15
    Division (DIV) Divides one number by another. 6 / 3 = 2
    AND Performs bitwise AND between two values. 5 AND 3 = 1
    OR Performs bitwise OR between two values. 5 OR 3 = 7
    XOR Performs bitwise XOR between two values. 5 XOR 3 = 6
    NOT Inverts all the bits in a value. NOT 5 = -6
    Shift Left (SHL) Shifts bits left, inserts zeros at the right end. 5 << 1 = 10
    Shift Right (SHR) Shifts bits right, inserts zeros at the left end. 10 >> 1 = 5
    Compare (CMP) Compares two values by subtracting them (flags set). CMP AX, BX
    Test (TEST) Performs a bitwise AND and sets flags based on the result. TEST AX, BX

    Conclusion

    Arithmetic and logical operations are essential components of Computer Organization and Assembly Language, enabling the processor to perform calculations, make decisions, and control data flow. These operations are executed by the Arithmetic and Logic Unit (ALU) and are accessed using assembly language instructions that directly manipulate data at the binary level. Understanding these operations is key to working with low-level programming and optimizing machine-level performance.

    Previous topic 19
    Accessing Information
    Next topic 21
    Control

    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 time10 min
      Word count1,737
      Code examples0
      DifficultyIntermediate