Wednesday, August 20, 2025

C08 How a CPU Reads Your Mind A Step by Step Guide


How a CPU Executes Programs

Dr Sudheendra S G outlines the fundamental principles of how a Central Processing Unit (CPU) runs a program, detailing the instruction cycle, program flow control, and the interaction between core CPU components and memory.

1. What is a Program?

A program is defined as "a list of tiny steps called instructions stored in memory." The CPU executes these instructions sequentially, one at a time. This process involves the CPU reading an instruction, interpreting it, performing the specified action, and then moving to the next instruction.

Key Components:

  • Instruction: A single, tiny step within a program.
  • Program: A sequence of instructions.
  • Memory (RAM): Where both instructions and data are stored.

2. Instruction Structure (Our Tiny CPU Example)

In the simplified CPU model discussed, each instruction is 8 bits long, divided into two main parts:

  • Opcode (first 4 bits): Specifies "what to do."
  • Operand (last 4 bits): Indicates "who/where" the operation should act upon, such as a register or a memory address.

Example: 0010 1110 translates to LOAD_A (opcode 0010) acting on address 14 (operand 1110).

3. Special Registers for Program Flow

Two critical registers facilitate the smooth execution of a program:

  • Instruction Address Register (a.k.a. Program Counter - PC): Stores the memory address of the next instruction to be fetched.
  • Instruction Register: Temporarily holds the instruction that has just been fetched from memory, allowing the CPU to decode and execute it.

Flow: Program Counter's value points to a location in RAM, from which an instruction is fetched and placed into the Instruction Register.

4. The Instruction Cycle: Fetch, Decode, Execute

The CPU operates on a continuous loop, repeatedly performing three core steps for each instruction:

  1. Fetch: The CPU retrieves the next instruction from RAM, using the address stored in the Program Counter.
  2. Decode: The CPU interprets the fetched instruction, identifying the opcode (what operation to perform) and the operand (the data or address to use).
  3. Execute: The CPU performs the operation. This involves "wiring up the ALU/Registers/RAM" as needed. After execution, the Program Counter is incremented to point to the next instruction, and the cycle repeats.

5. Running a Sample Program (Step-by-Step Execution)

A sample 4-instruction program demonstrates the Fetch-Decode-Execute cycle:

Program at Memory Addresses 0-3:

  • Addr 0: 0010 1110 → LOAD_A 14 (Load data from address 14 into Register A)
  • Addr 1: 0001 1111 → LOAD_B 15 (Load data from address 15 into Register B)
  • Addr 2: 1000 0100 → ADD B, A (Add contents of Register B to Register A, store sum in A)
  • Addr 3: 0100 1101 → STORE_A 13 (Store contents of Register A into address 13)

Data at Memory Addresses 14-15:

  • Addr 14: 00000011 → data 3
  • Addr 15: 00001110 → data 14

Execution Trace (Highlights):

  • PC=0: LOAD_A 14. Fetches 0010 1110. Decodes. Executes: Register A gets RAM[14] (which is 3). PC becomes 1.
  • PC=1: LOAD_B 15. Fetches 0001 1111. Decodes. Executes: Register B gets RAM[15] (which is 14). PC becomes 2.
  • PC=2: ADD B, A. Fetches 1000 0100. Decodes. Executes: ALU calculates A (3) + B (14) = 17. Register A gets 17. PC becomes 3.
  • PC=3: STORE_A 13. Fetches 0100 1101. Decodes. Executes: RAM[13] gets A (17). PC becomes 4.

6. The Importance of HALT

"Instructions and data live together as plain numbers in the same RAM." Without a HALT instruction, the CPU would continue fetching and attempting to execute whatever bit patterns are next in memory, potentially leading to a "crash." Real programs always terminate with a HALT instruction.

7. Changing Program Order: JUMP Instructions

Normally, the Program Counter increments sequentially. However, JUMP instructions allow for non-sequential execution, enabling loops and conditional logic.

  • JUMP k (Unconditional Jump): This instruction overwrites the Program Counter with the value k, causing the CPU to fetch the next instruction from address k. This is "great for loops."
  • Example: Replacing an instruction with JUMP 2 would create a loop, sending execution back to address 2.

8. Conditional JUMPs and Program Logic

To create more sophisticated programs, CPUs use conditional JUMP instructions. The ALU (Arithmetic Logic Unit) sets "flags" (e.g., Negative, Zero) based on the results of operations.

  • JUMP_NEGATIVE k: Jumps to address k only if the Negative flag is set.
  • Similarly, JUMP_IF_EQUAL, JUMP_IF_GREATER, etc., allow programs to make decisions based on data.

Insight: Conditional jumps demonstrate how "software synthesized division remainder even though the ALU had no divide—power of programs." This highlights how complex operations can be built from simple instructions and control flow.

9. Instruction Set Limits and Scaling

The tiny CPU example demonstrates limitations:

  • Opcode Bits: 4 opcode bits allow for a maximum of 16 distinct instructions.
  • Operand Bits: 4 operand bits limit addressing to only 16 memory locations.

Real CPUs overcome these limitations by:

  1. Longer Instructions: Using 32-bit or 64-bit instructions to accommodate more opcodes and larger address spaces.
  2. Variable-Length Instructions: Instructions can vary in length, with the opcode often determining if additional bytes are needed for larger immediate values or addresses (e.g., 8-bit or 16-bit addresses).

Impact: "Bigger/variable formats let programs jump farther and address more memory."

10. Reality Check: Evolution of CPUs

While the fundamental "Fetch → Decode → Execute" heartbeat remains, modern CPUs are significantly more complex:

  • Intel 4004 (1971): Supported 46 instructions.
  • Modern CPUs (e.g., Core i7): Have "thousands of opcodes/variants" and instructions ranging from "1–15 bytes long."

11. Quick Recap: The CPU's Program Execution Flow

  1. Program and Data in RAM: Instructions and data are loaded into Random Access Memory.
  2. Fetch: The CPU retrieves an instruction from RAM, guided by the Program Counter.
  3. Decode: The Control Unit interprets the instruction's opcode and operand.
  4. Execute: The CPU performs the operation using its registers, ALU, and RAM as necessary.
  5. PC Update/Jump: The Program Counter is updated (incremented for sequential flow or changed by a JUMP instruction).
  6. Repeat: The cycle continues until a HALT instruction is encountered.

Conclusion: "With jumps and conditions, tiny steps become powerful programs. That’s the magic of software driving hardware."

 


No comments: