Elective Computer Architecture

Computer ArchitectureUnit 48 min read

Arithmetic & Logic Operations: ALU, Flags, Overflow, Micro-ops & Binary Math

Unit 4 of Computer Architecture covers the core arithmetic and logic unit (ALU) operations, flag registers, overflow detection, micro-operations, binary arithmetic (add/subtract/multiply/divide), and how these form the basis of CPU execution—with real-world examples from banking (interest calculations), eSewa (transact

Core Concepts: ALU and Flag Registers

What is an ALU?

The Arithmetic Logic Unit (ALU) is the heart of the CPU that performs all arithmetic and logical operations. It executes instructions like:

  • Arithmetic: Addition, subtraction, multiplication, division.
  • Logical: AND, OR, NOT, XOR, shifts, rotates.
ALU CoreArithmetic OperationsFlag RegisterLogical Operations
ALU and its interaction with the Flag Register (simplified block diagram)

Flag Registers: How the CPU Knows What Happened

Flags are single-bit status indicators set by the ALU after operations. The most important flags are:

Flag Name Set When Example Use Case
Zero Flag (Z) Result = 0 Loop termination (e.g., while (x != 0) in assembly).
Carry Flag (C) Unsigned overflow (e.g., 8 + 9 in 8-bit). Detecting overflow in addition/subtraction (e.g., eSewa transaction limits).
Overflow Flag (V) Signed overflow (e.g., 127 + 1 in 8-bit signed). Banking interest calculations (e.g., NMB loan repayment checks).
Sign Flag (N) Result is negative (MSB = 1). Pathao’s fare computation (distance-based, signed values).

Binary Arithmetic Operations

02467Bit 71 bitsBit 61 bitsBit 51 bitsBit 41 bitsBit 31 bitsBit 21 bitsBit 11 bitsBit 01 bits
8-bit binary number representation (used in overflow examples)

1. Addition and Subtraction

How It Works:

  • Addition: Bitwise addition with carry propagation. Example: 5 + 3 in 4-bit binary:
    0101 (5)
    
  • 0011 (3)

    1000 (8)
    - **Carry Flag (C)**: Set if there’s a carry out of the MSB.
    - **Overflow Flag (V)**: Set if signed addition exceeds representable range (e.g., `127 + 1` in 8-bit signed).
    

Worked Example: eSewa Transaction Limit

Suppose eSewa uses 16-bit unsigned integers for transaction amounts. If a user tries to send 65,000 (0xFF9C) to another user, but the limit is 65,535 (0xFFFF), the ALU will set the Carry Flag (C) to indicate overflow.

# Pseudocode for eSewa's overflow check
amount = 0xFF9C  # 65,000
limit = 0xFFFF    # 65,535
if (amount + 1) < amount:  # Check for unsigned overflow
    print("Transaction exceeds limit!")  # C flag is set

Subtraction:

  • Borrow Flag: Used in subtraction (similar to carry in addition).
  • Two’s Complement: Subtraction is done by adding the negative (e.g., A - B = A + (-B)).

2. Multiplication and Division

Multiplication:

  • Hardware Method: Shift-and-add (e.g., 5 * 3 = 5 << 1 + 5).
  • Flags Affected: Zero Flag (Z), Overflow Flag (V).
  • Example: Multiply 6 (0110) by 3 (0011):
    0110 (6)
    × 0011 (3)
    --------
    0000 (0)
    0110 (6)
    1100 (12)
    --------
    1110 (14)
    

Division:

  • Hardware Method: Repeated subtraction (shift-and-subtract).
  • Flags Affected: Zero Flag (Z), Overflow Flag (V) if remainder is non-zero.
  • Example: Divide 14 (01110) by 3 (0011):
    14 ÷ 3 = 4 with remainder 2
    

Logic Operations

The ALU also performs bitwise logical operations:

Operation Symbol Example (A=5, B=3) Use Case
AND & 0101 & 0011 = 0001 Masking bits (e.g., Pathao’s route flags).
OR ` ` `0101
XOR ^ 0101 ^ 0011 = 0110 Error detection (e.g., WhatsApp message integrity).
NOT ~ ~0101 = 1010 Inverting bits (e.g., Daraz’s stock availability toggles).
Shift Left << 0101 << 1 = 1010 Fast multiplication (e.g., NEPSE’s volume scaling).
Shift Right >> 0101 >> 1 = 0010 Fast division (e.g., Khalti’s fee reduction).

Micro-operations and Register Transfers

What Are Micro-operations?

Micro-operations (μ-ops) are the smallest low-level operations performed by the ALU. Examples:

  • Arithmetic μ-ops: ADD, SUB, INC, DEC.
  • Logical μ-ops: AND, OR, NOT, XOR.
  • Shift μ-ops: SHL, SHR, ROL, ROR.

Register Transfer Notation (RTN)

RTN describes how data moves between registers and the ALU. Example:

  • Addition: A ← A + B (Add contents of B to A, store result in A).
  • Subtraction: C ← B - A (Subtract A from B, store in C).

Overflow Detection

110Unsigned AdditionSigned AdditionCarry FlagOverflow Flag
Overflow flag vs. carry flag in unsigned vs. signed operations

Types of Overflow:

  1. Unsigned Overflow: Occurs when the result exceeds the maximum representable value (e.g., 255 + 1 in 8-bit unsigned).
    • Detected by: Carry Flag (C).
  2. Signed Overflow: Occurs when the result exceeds the signed range (e.g., 127 + 1 in 8-bit signed).
    • Detected by: Overflow Flag (V).

Worked Example: NMB Loan Interest Calculation

Suppose NMB calculates monthly interest on a loan using 32-bit signed integers. If the principal is 2,147,483,647 (0x7FFFFFFF) and the interest rate is 1%, the calculation:

New Amount = Principal + (Principal * 0.01)
           = 2,147,483,647 + 21,474,836.47
           = 2,168,958,483.47
  • Problem: 2,147,483,647 + 21,474,836 exceeds the 32-bit signed limit (2,147,483,647).
  • Solution: The ALU sets the Overflow Flag (V), and the bank’s software must handle this gracefully (e.g., switch to floating-point or cap the amount).

Real-World Applications

1. eSewa: Transaction Validation

  • Idea Used: Overflow Detection (Carry Flag).
  • How: eSewa uses 32-bit unsigned integers for transaction amounts. If a user tries to send 4,294,967,295 (0xFFFFFFFF) to another user, the ALU’s Carry Flag (C) is set when adding to the recipient’s balance, triggering a "Transaction Limit Exceeded" error.

2. Pathao: Distance-Based Fare Calculation

  • Idea Used: Signed Arithmetic and Overflow.
  • How: Pathao calculates fares using signed integers for distance (e.g., -100 to 100 meters). If the distance exceeds the representable range (e.g., 32,767 meters in 16-bit signed), the Overflow Flag (V) is set, and the app displays an error ("Distance too long").

3. NMB Bank: Loan Repayment Checks

  • Idea Used: Signed Overflow and Division.
  • How: When calculating monthly installments, NMB uses 64-bit integers to avoid overflow. If a user’s repayment exceeds the loan amount due to rounding errors in division, the Zero Flag (Z) or Overflow Flag (V) helps detect and correct the discrepancy.

Exam Tip: How to Score Full Marks

  1. Define Clearly: Always start with definitions (e.g., "The ALU is the component that performs arithmetic and logical operations...").
  2. Use RTN: For questions on register transfers, write operations in Register Transfer Notation (e.g., A ← A + B).
  3. Flag Examples: For overflow/carry questions, always give a numerical example (e.g., "In 8-bit signed arithmetic, 127 + 1 sets the Overflow Flag").
  4. Real-World Tie: Link concepts to eSewa, Pathao, or banking (e.g., "The Carry Flag is used in eSewa to detect transaction limits").
  5. Diagrams: Draw ALU block diagrams or flag register tables to visualize operations.
  6. Common Pitfalls:
    • Don’t confuse unsigned overflow (Carry Flag) with signed overflow (Overflow Flag).
    • Always check if the question asks for 8-bit, 16-bit, or 32-bit operations (affects overflow behavior).
    • For micro-operations, specify whether it’s arithmetic or logical.

binary addition with carryStep-by-step binary addition showing carry propagation. (Image: Phlsph7, CC0, via Wikimedia Commons)

Based on the PU BE Computer (PU) syllabus for Computer Architecture, unit 4.

Discussion

Loading…