IT236 Microprocessor and Computer Architecture

Microprocessor and Computer ArchitectureUnit 59 min read

Arithmetic & Logic Operations: ALU, Flags, Binary Math, Logic Gates & Boolean Algebra

Unit 5 of Microprocessor and Computer Architecture explores how microprocessors perform arithmetic (addition, subtraction, multiplication, division) and logic operations (AND, OR, NOT, XOR) using the ALU, how status flags (zero, carry, overflow) work, and how these operations are implemented in hardware (logic gates, c

Core Concepts: ALU and Status Flags

The Arithmetic Logic Unit (ALU) is the heart of a microprocessor where all arithmetic and logic operations are performed. It takes inputs (operands) and produces results, while status flags (or condition codes) store the outcome of operations to influence program flow.

08162431Zero Flag (ZF)1 bitsCarryFlag (CF)1 bitsOverflow Flag (O1 bitsSign Flag (SF)1 bits
8-bit status flags register (x86-style)

1. ALU Operations

The ALU performs two main types of operations:

  • Arithmetic Operations: Addition, subtraction, multiplication, division, increment, decrement.
  • Logic Operations: AND, OR, NOT, XOR, shifts (left/right), rotates.

How ALU Works

ALU Inputs (Operands + ControlSignals)ALU Core (Arithmetic/LogicUnit)Result OutputStatus Flags (Zero, Carry,Overflow, Sign)
Simplified ALU operation flow: inputs → processing → result + flags

Status Flags (Condition Codes)

Flags are single-bit registers that store the result of an operation. Common flags:

Flag Meaning Example Scenario
Zero (Z) Set if result = 0 CMP A, B → JZ label (jump if A == B)
Carry (C) Set if unsigned overflow (e.g., addition > 8 bits) ADD 255, 1 → Carry flag set (result = 0, carry = 1)
Overflow (V) Set if signed overflow (e.g., 127 + 1 = -128 in 8-bit) ADD 127, 1 → Overflow flag set (signed overflow)
Sign (S) Set if result is negative (MSB = 1) SUB A, B → JS label (jump if result < 0)
Parity (P) Set if even number of 1s in result (less common) Used in error detection (e.g., memory parity checks)

2. Arithmetic Operations in Binary

A. Addition and Subtraction

Binary addition follows these rules:

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0, carry = 1

Example: Adding 5 + 3 in 8-bit binary

  00000101 (5)
+ 00000011 (3)
-----------
  00000110 (6)

Subtraction is addition of two’s complement:

A - B = A + (-B)

Example: 5 - 3 in 8-bit

  00000101 (5)
+ 11111101 (-3, two’s complement)
-----------
  00000010 (2)

B. Multiplication and Division

Multiplication (Shift-and-Add)

Multiply by shifting and adding partial products:

Example: 5 × 3 = 15
Binary: 0101 × 0011
Step 1Shift 0101 << 1 →1010 (partial product)Step 2Add 1010 + 0101 =1111 (15)CaptionBinary multiplication: 5 × 3 = 15 (shift

Division (Shift-and-Subtract)

Divide by repeatedly subtracting the divisor:

Example: 15 ÷ 3 = 5
Binary: 1111 ÷ 0011
Step 11111 - 0011 = 1100(subtract)Step 21100 >> 1 = 0110(shift right)Step 30110 - 0011 = 0011(subtract)Step 40011 >> 1 = 0001(shift right)ResultQuotient = 0101(5)
Binary division: 15 ÷ 3 = 5 (shift-and-subtract method)

3. Logic Operations and Boolean Algebra

Logic operations are performed using logic gates (AND, OR, NOT, XOR, NAND, NOR). They are fundamental to decision-making in processors.

A. Basic Logic Gates

Gate Symbol Truth Table Example Use Case
AND A • B 0 if any input is 0 AND A, B (check if both flags are set)
OR A + B 1 if any input is 1 OR A, B (check if either condition is true)
NOT ¬A Inverts input NOT A (toggle a bit)
XOR A ⊕ B 1 if inputs differ Parity checks, encryption (e.g., XOR cipher)
1111111111ABANDORNOTXOR
Logic gate truth table relationships (A=1, B=0)

B. Boolean Algebra Laws

Key laws used to simplify logic expressions:

  1. Commutative: A + B = B + A, A • B = B • A
  2. Associative: (A + B) + C = A + (B + C)
  3. Distributive: A • (B + C) = (A • B) + (A • C)
  4. De Morgan’s: ¬(A + B) = ¬A • ¬B, ¬(A • B) = ¬A + ¬B

Example: Simplify A • (A + B) Using absorption law: A • (A + B) = A


4. Real-World Applications

A. Banking (Interest Calculations)

Banks use ALU for compound interest calculations:

Formula: A = P (1 + r/n)^(nt)
  • Example: If you deposit NPR 10,000 in a bank with 5% annual interest compounded annually for 2 years:
    A = 10000 (1 + 0.05)^2 = 10000 × 1.1025 = 11025
    
    The ALU performs repeated multiplication and addition to compute this.

B. E-Commerce (Order Processing)

When you place an order on Daraz or Hamrobazaar:

  1. The server checks stock (logic operations: IF stock > 0 THEN proceed).
  2. It calculates total price (arithmetic: price = quantity × unit_price).
  3. It updates inventory (subtraction: stock = stock - quantity).

Example: Daraz Order Queue

C. Mobile Apps (Encryption)

WhatsApp uses XOR encryption for secure messaging:

  • Plaintext P is XORed with a key K to get ciphertext C: C = P ⊕ K.
  • The receiver XORs C with K again to recover P: P = C ⊕ K.

Example: Simple XOR Cipher

Plaintext: "A" (01000001)
Key:       "X" (01011000)
Ciphertext: "A ⊕ X" = 00011001 ("1")

5. Assembly Language Examples

A. Arithmetic Operations in x86 Assembly

; Add two numbers and store result
MOV AL, 5    ; Load 5 into AL
ADD AL, 3    ; AL = AL + 3 (Result = 8)
JZ  OVERFLOW ; Jump if result is zero (unlikely here)

; Subtract and check carry flag
MOV BL, 10
SUB BL, 15
JC  NEGATIVE ; Jump if carry (result < 0)

B. Logic Operations in ARM Assembly

; Check if two flags are both set (AND)
LDR R0, =FLAG1
LDR R1, =FLAG2
AND R2, R0, R1 ; R2 = FLAG1 AND FLAG2
CMP R2, #0
BEQ NOT_BOTH_SET

; Toggle a bit (XOR)
MOV R3, #1
EOR R4, R4, R3 ; Toggle least significant bit

6. Performance Considerations

Operation Time Complexity Hardware Support Example Use Case
Addition O(1) Dedicated adder circuit ADD A, B in ALU
Multiplication O(n) Shift-and-add or multiplier MUL AX, BX (x86)
Division O(n²) Divider circuit DIV AX, BX (slow, rare in apps)
Logic Gates O(1) Hardwired in ALU AND, OR, NOT instructions

Note: Modern CPUs use hardware multipliers/dividers for speed, but software emulation is slower.


7. Common Pitfalls and Errors

  1. Signed vs. Unsigned Overflow:

    • ADD 127, 1 → Overflow (signed), but no carry (unsigned).
    • Fix: Use JO (jump on overflow) for signed checks.
  2. Incorrect Flag Handling:

    • Forgetting to clear flags before operations (e.g., CLC to clear carry).
  3. Boolean Algebra Mistakes:

    • Misapplying De Morgan’s laws (e.g., ¬(A + B) ≠ ¬A + ¬B).
  4. Assembly Syntax Errors:

    • Using ADD A, B instead of ADD B, A (order matters in some architectures).

Exam Tip

What to Expect in TU/PU Exams

  1. Theory Questions (30-40%):

    • Define ALU, status flags, and logic gates.
    • Explain signed vs. unsigned arithmetic with examples.
    • Compare RISC/CISC handling of arithmetic operations.
  2. Worked Examples (30-40%):

    • Binary addition/subtraction with carry/overflow.
    • Boolean algebra simplification (e.g., simplify A + AB).
    • Assembly code tracing (e.g., predict flags after SUB).
  3. Short Answer (20-30%):

    • "How does the ALU handle division?"
    • "What is the difference between carry and overflow flags?"
    • "Write an assembly snippet to check if two numbers are equal."

High-Scoring Strategies

  • Draw diagrams for ALU operations, logic gates, and flag interactions.
  • Show binary traces for arithmetic operations (e.g., 5 + (-3) in 8-bit).
  • Relate to real-world apps (e.g., "How does WhatsApp use XOR?").
  • Practice assembly on simulators like MARS (MIPS) or DOSBox (x86).

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

Based on the TU BIM syllabus for Microprocessor and Computer Architecture (IT236), unit 5.

Discussion

Loading…