COA Notes
Detailed analysis of how instructions are fetched, decoded, and executed in a CPU.
Introduction
Every single thing your computer does — from displaying a character to calculating a spreadsheet formula — boils down to instructions being executed one after another (or in parallel in modern CPUs). But what exactly happens when an instruction executes? It is not magic — it is a precise sequence of micro-operations involving registers, buses, the ALU, and control signals. Understanding instruction execution at this level transforms you from someone who "uses" computers to someone who truly understands them. Let's trace the complete journey of an instruction from memory to completion.
The Instruction Cycle
Every instruction goes through a fundamental cycle:
| FETCH | → | DECODE | → | EXECUTE | → | STORE |
|---|---|---|---|---|---|---|
| (if needed) | ||||||
| Get instr | Figure | Perform | Write | |||
| from mem | out what | the | result | |||
| to do | operation | back |
Phase 1: Fetch
The fetch phase brings the instruction from memory into the CPU:
| Step 1 | MAR ← PC (Put PC value on address bus) |
| Step 2 | MDR ← M[MAR] (Read instruction from memory) |
| Step 3 | IR ← MDR (Place instruction in Instruction Register) |
In RTL notation:
| T0 | MAR ← PC |
| T1 | MDR ← M[MAR], PC ← PC + 1 |
| T2 | IR ← MDR |
Each T represents one clock cycle. After fetch, the IR contains the complete instruction and the PC points to the next instruction.
Phase 2: Decode
The control unit examines the opcode field of the IR and determines:
- What operation to perform
- Where the operands are (registers? memory? immediate?)
- Which control signals to activate
For a hardwired control unit, the opcode feeds into a decoder circuit. For a microprogrammed unit, it indexes into a control memory to retrieve the microinstruction sequence.
Phase 3: Execute
This phase varies dramatically based on the instruction type. Let's trace several examples.
Detailed Execution Examples
Example 1: ADD R1, R2, R3 (Register-to-Register Add)
This instruction means: R1 ← R2 + R3
Fetch
T0: MAR ← PC
T1: MDR ← M[MAR], PC ← PC + 1
T2: IR ← MDR
Decode
T3: Control unit decodes opcode, identifies R2 and R3 as sources, R1 as destination
Execute
T4: A ← R2 (Load ALU input A from R2)
T5: B ← R3 (Load ALU input B from R3)
T6: R1 ← A + B (ALU performs addition, result to R1)
Flags updated (Z, C, V, N flags set based on result)
Total: 7 clock cycles for one instruction.
Example 2: LOAD R1, [Address] (Memory Load)
This instruction means: R1 ← Memory[Address]
Fetch
T0: MAR ← PC
T1: MDR ← M[MAR], PC ← PC + 1
T2: IR ← MDR
Decode
T3: Control unit identifies this as memory load, extracts address field
Execute
T4: MAR ← IR[address field] (Put target address in MAR)
T5: MDR ← M[MAR] (Read data from memory)
T6: R1 ← MDR (Transfer data to destination register)
Notice this takes an extra memory access compared to register operations — this is why load/store operations are slower.
Example 3: STORE R1, [Address] (Memory Store)
| Fetch | (same as above - T0, T1, T2) |
| T3 | Identify store operation, extract address |
| T4 | MAR ← IR[address field] (Set memory address) |
| T5 | MDR ← R1 (Put register data in MDR) |
| T6 | M[MAR] ← MDR (Write data to memory) |
Example 4: BEQ Target (Branch if Equal/Zero)
| Fetch | T0-T2 (normal fetch) |
| T3 | Identify branch instruction, check condition |
| T4 | If (Z flag = 1): (Check zero flag from previous operation) |
Branch instructions are special — they modify the PC, changing the flow of execution. This is why branches cause problems in pipelined processors (the fetch stage already fetched the wrong next instruction).
Example 5: CALL Subroutine (Function Call)
| Fetch | T0-T2 |
| T3 | Identify CALL instruction |
| T4 | SP ← SP - 1 (Make room on stack) |
| T5 | M[SP] ← PC (Save return address to stack) |
| T6 | PC ← IR[target address] (Jump to subroutine) |
The saved PC value is the address of the instruction AFTER the CALL, so when RET executes, it can return to the correct location.
Control Signals During Execution
Each micro-operation requires specific control signals to be asserted:
The control unit generates these signals in the correct sequence. This is the fundamental job of the control unit — timing and sequencing of control signals.
Timing Diagram
Here is how an ADD instruction looks on a timing diagram:
| Clock | __|‾‾|__|‾‾|__|‾‾|__|‾‾|__|‾‾|__|‾‾|__|‾‾|__ |
| PCout | ‾‾‾‾|___|___|___|___|___|___| |
| MARin | ‾‾‾‾|___|___|___|___|___|___| |
| Read | ____|‾‾‾‾‾‾‾|___|___|___|___| |
| MDRin | ____|‾‾‾‾‾‾‾|___|___|___|___| |
| IRin | ________|‾‾‾‾|___|___|___|___| |
| ALUop | __________________|‾‾‾‾‾‾‾‾‾‾| |
| R1in | __________________________|‾‾‾‾| |
Interrupt Handling During Execution
What happens if an interrupt arrives mid-instruction?
- CPU completes the current instruction (instructions are atomic)
- CPU saves state: PC, flags, registers pushed to stack
- CPU acknowledges the interrupt
- Interrupt vector loaded into PC
- ISR (Interrupt Service Routine) executes
- ISR ends with RTI (Return from Interrupt)
- Original state restored, execution resumes
Multi-Cycle vs Single-Cycle Execution
Single-Cycle Design
- Every instruction completes in one (long) clock cycle
- Clock period = longest instruction time
- Simple but wasteful — fast instructions (ADD) must wait as long as slow ones (LOAD)
Multi-Cycle Design
- Instructions take variable number of shorter clock cycles
- Fast instructions take fewer cycles
- Hardware shared across cycles (one ALU used in different phases)
- More complex control but better resource utilization
Pipelined Design
- Multiple instructions overlap — each in different phase
- One instruction completes per cycle (in steady state)
- Best throughput but introduces hazards
Real-World Observation
When you run a program and it seems slow, here is what might be happening at the instruction execution level:
- Cache miss: A LOAD instruction that would normally take 4 cycles takes 100+ cycles waiting for main memory
- Branch misprediction: The pipeline fetched and partially executed 15-20 wrong instructions that must be flushed
- Data dependency: One instruction waits for a previous instruction's result — pipeline stalls
Modern CPUs execute 4-6 billion instructions per second. Each one goes through this fetch-decode-execute cycle, just incredibly fast and overlapped.
Key Takeaways
- Every instruction goes through Fetch → Decode → Execute, broken into micro-operations
- The control unit generates timed control signals that choreograph data movement between registers, ALU, and memory
- Register operations are faster than memory operations because no memory access is needed in the execute phase
- Branch instructions modify the PC, redirecting the sequential flow — this is fundamental to all decision-making in programs
- Interrupts are checked between instructions — the CPU always completes the current instruction before handling an interrupt
- The number of clock cycles per instruction varies by instruction type — this is what CPI measures as an average
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Instruction Execution.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, instruction, set, architecture, execution
Related Computer Organization & Architecture Topics