# The Instruction Execution Cycle

Computer Science I · Introduction to Computers · https://tryals.app/learn/computer-science-i/the-instruction-execution-cycle

## The Instruction Cycle

A CPU runs programs by repeating the **fetch–decode–execute cycle** billions of times per second. 

| Step | What happens | Key register |
| --- | --- | --- |
| **Fetch** | Read instruction at PC; advance PC | **Program counter (PC)** |
| **Decode** | Interpret the operation and operands | **Instruction register (IR)** |
| **Execute** | Perform the ALU operation or memory move | ALU / registers / memory |

During fetch, the **PC** holds the instruction address and advances immediately, assuming sequential flow: $\text{PC} \leftarrow \text{PC} + 1$. A **jump** or **branch** instruction changes this by writing a new address into the PC: $\text{PC} \leftarrow \text{target}$.

> **Common pitfall:** thinking a branch skips the cycle or works by magic. Every instruction goes through fetch–decode–execute; a branch simply overwrites a PC value that was already advanced.

## Following a Real Instruction

**RISC-V** is an open instruction set where every instruction is 32 bits wide. Let's trace `add x5, x6, x7` (add registers x6 and x7, store in x5) sitting at PC address 200:

- **Fetch**: Memory at 200 is copied to the IR. The PC advances to 204.
- **Decode**: The control unit reads the operation and fetches values from registers x6 and x7.
- **Execute**: The ALU adds them.
- **Write back**: The sum is written into x5.

Notice **when** the PC advanced: during fetch, before decoding. Hardware assumes sequential flow. A taken branch corrects it later by overwriting that already-advanced value.

> **Common pitfall:** picturing the PC incrementing at the end of the cycle. It advances during fetch, and control-flow instructions overwrite that pre-set value.

## Practice questions

4 of this lesson's 12 practice questions, with answers. The full set is in the app.

### 1. A program executes ADD R1, R2 followed by JMP 200. Order what the CPU does, from the start of the first instruction to the moment the jump takes effect.

**Answer:**

1. The PC's address is used to read the ADD instruction from memory.
2. The PC advances to point at the JMP instruction.
3. ADD is decoded and the ALU adds the two registers.
4. The JMP is fetched, and the PC advances again to the instruction after it.
5. JMP executes and writes 200 into the PC, discarding that advance.

**Why:** The sequence shows why incrementing the PC during fetch costs nothing. After the ADD is fetched, the PC advances to the JMP - which is exactly right, and no extra work was needed to arrange it.

The JMP is then fetched and the PC advances again, to whatever follows it. That advance is immediately discarded when the jump executes and writes 200 into the PC.

So a jump is not special sequencing hardware; it is one register write that happens to overwrite a value the fetch phase had speculatively set. Everything the CPU does about control flow reduces to what number ends up in the PC when the cycle comes round again - which is also why a corrupted PC is so catastrophic, and why buffer overflows that overwrite a return address are able to redirect execution anywhere.

Page: https://tryals.app/practice/computer-science-i/the-instruction-execution-cycle/a-program-executes-add-r1-r2-followed-by-jmp-200-order-what-the-cpu

### 2. A jump instruction sits at address 40 and jumps to address 80. Instructions are 4 bytes long. Order what happens.

**Answer:**

1. The PC holds 40, so the CPU fetches the instruction stored at address 40
2. The PC advances to 44
3. The control unit decodes the instruction and finds it is a jump to 80
4. The execute step writes 80 into the PC, replacing the 44
5. The next fetch reads the instruction at address 80

**Why:** The PC is advanced to 44 automatically during the fetch, because the hardware does not yet know what it has fetched. Only when the instruction executes is 44 overwritten with 80. This is the whole mechanism of a jump: no magic, just a later write to the same register.

Page: https://tryals.app/practice/computer-science-i/the-instruction-execution-cycle/a-jump-instruction-sits-at-address-40-and-jumps-to-address-80

### 3. The program counter is incremented during the FETCH phase, before the instruction has even been decoded. Why increment it so early rather than after the instruction completes?

A. Because the address has already been used, so the PC is free for the next one.
B. Because the decode phase destroys the PC's value, so it must be updated first.
C. Because incrementing later would skip an instruction, since the memory address advances on its own.
D. Because the PC must be incremented before the ALU can be used, as they share a data path.

**Answer:** A. Because the address has already been used, so the PC is free for the next one.

**Why:** Once the fetch has used the address, advancing the PC immediately leaves the machine pointing at the default next instruction. That default is what makes branching clean, a jump is one register assignment, not special sequencing logic. It also means the PC already holds the *next* address during execution, which is where relative branch offsets are measured from.

Page: https://tryals.app/practice/computer-science-i/the-instruction-execution-cycle/the-program-counter-is-incremented-during-the-fetch-phase-before-the

### 4. A buffer overflow that overwrites a return address can make a program execute the attacker's code. Which architectural feature makes that possible?

A. The absence of bounds checking in the instruction cycle
B. The stack growing downward in memory
C. The program counter being incremented during fetch rather than after execution
D. Instructions and data sharing one memory and one address space

**Answer:** D. Instructions and data sharing one memory and one address space

**Why:** Stored-program design is what lets a program be loaded and compiled, and it is what lets data become an address to jump to. The whole class of attack follows from the von Neumann trade, which is why the defences are bolt-ons: non-executable stacks, address randomisation and stack canaries.

Page: https://tryals.app/practice/computer-science-i/the-instruction-execution-cycle/a-buffer-overflow-that-overwrites-a-return-address-can-make-a-program
