Sub topic 2
The stored-program model and the instruction cycle
This chapter assumes you know binary and hexadecimal numbers, basic digital logic (multiplexers, flip-flops), and what registers and memory are. It builds the model that later chapters on instruction formats, addressing modes, the ALU and the control unit rely on.
The stored-program model
In a stored-program computer, instructions and data live in the same main memory. Both are bit patterns. The processor does not know in advance which words are code. It treats a word as an instruction only when the program counter points to it and the word is fetched into the instruction register.
This design, usually credited to von Neumann, has three consequences:
- Programs are data. A loader can copy a program into memory, and a compiler can write one.
- Control flow is just writing a number into the PC. A jump needs no special hardware path.
- One memory path is shared. Instruction fetches and operand accesses compete for the same memory bus.
The last point is called the von Neumann bottleneck. Split instruction and data caches reduce it in practice, but the programmer-visible model is still one address space.
Registers that drive the cycle
| Register | Name | Role |
|---|---|---|
| PC | Program counter | Address of the next instruction to fetch |
| MAR | Memory address register | Address placed on the address bus |
| MDR (or MBR) | Memory data register | Word read from or written to memory |
| IR | Instruction register | The instruction being executed |
| AC or general registers | Accumulator, register file | Operands and results |
The PC is the only register whose role is fixed by the cycle. The processor reads from memory only through MAR and MDR. The control unit reads the opcode from IR for the rest of the cycle.
The instruction cycle
Each instruction passes through the same phases:
- Fetch. Read the instruction at the address in PC into IR and advance PC.
- Decode. The control unit examines the opcode and address fields in IR.
- Operand fetch. Compute the effective address if the instruction needs a memory operand, then read the operand.
- Execute. The ALU computes, or the PC is changed for a branch.
- Write back. Store the result in a register or in memory.
Not every instruction uses every phase. A register-to-register ADD has no memory operand access. A conditional branch has no write-back.
Fetch as register transfers
Register transfer notation writes each step as destination <- source. The fetch phase is the same for every instruction:
Fetch sequence as register transfers:
T0: MAR <- PC
T1: MDR <- M[MAR], PC <- PC + 1
T2: IR <- MDR
T3: decode IR; control unit selects the execute sequenceEach line is a time step, and the control unit activates the signals for that step.
- T0 puts the PC value on the address path through MAR.
- T1 does two transfers at once. The memory read and the PC increment use different hardware, so they can overlap. This is why the PC already holds the address of the next instruction when execution begins.
- T2 copies the fetched word into IR.
- T3 begins decode. Execute steps differ per instruction and depend on the opcode.
A runnable model
The following program simulates a one-address accumulator machine. Each memory word holds either a data integer or an (opcode, address) pair.
def run(memory):
pc, ac = 0, 0
while True:
mar = pc # T0: MAR <- PC
mdr = memory[mar] # T1: MDR <- M[MAR]
pc = pc + 1 # T1: PC <- PC + 1
ir = mdr # T2: IR <- MDR
op, x = ir # T3: decode
if op == "LOAD":
ac = memory[x]
elif op == "ADD":
ac = ac + memory[x]
elif op == "STORE":
memory[x] = ac
elif op == "JUMP":
pc = x # a branch overwrites the incremented PC
elif op == "HALT":
return ac
else:
raise ValueError(op)
memory = {
0: ("LOAD", 10),
1: ("ADD", 11),
2: ("STORE", 12),
3: ("HALT", 0),
10: 5, 11: 7, 12: 0,
}
print(run(memory)) # 12
print(memory[12]) # 12Words 0 to 3 are instructions and words 10 to 12 are data. They sit in one dictionary because they share one memory. If the program executed a JUMP 10, the processor would try to decode the integer 5 as an instruction and fail. Real hardware would decode the bit pattern and run whatever it means.
Counting memory accesses
GATE questions often ask how many memory references an instruction needs. Count one access for the fetch, plus one for each operand read or write. Addressing modes add more, as the next chapters show.
| Instruction | Fetch | Operand | Total |
|---|---|---|---|
| `ADD R1, R2` (register mode) | 1 | 0 | 1 |
| `LOAD X` (direct) | 1 | 1 | 2 |
| `LOAD X` (indirect) | 1 | 2 (pointer, then data) | 3 |
| `STORE X` (direct) | 1 | 1 (write) | 2 |
If an instruction spans several words, each extra word costs another fetch access.
Common errors
- Using the old PC value. After fetch, PC holds the address of the next instruction, not the current one. A PC-relative branch offset is added to that updated value.
- Thinking a branch skips the increment. The increment always happens in fetch. A taken branch then overwrites PC during execute.
- Confusing IR with MDR. MDR is a bus-side buffer that also carries data. IR holds the instruction for the whole instruction, because MDR will be reused for operands.
- Forgetting the interrupt check. Interrupts are recognised at the end of an instruction, not in the middle. That is what makes an instruction appear atomic to the program.
- Treating decode as free. The control unit needs at least one time step to decode, and it appears in cycle-count questions.
Check your understanding
Exercise
A machine has one-word instructions. The PC holds 500, and the word at address 500 is JUMP 800. What does the PC hold after the fetch phase, and after the execute phase?
Show answer
After fetch, PC = 501, because PC <- PC + 1 happens in T1. After execute, PC = 800, because the jump writes its target address into PC and overwrites the incremented value.
Exercise
A one-address instruction occupies one word and uses indirect addressing for a memory read. How many memory accesses does the whole instruction cycle make?
Show answer
Three. One access fetches the instruction. One reads the pointer stored at the address field. One reads the operand from the address the pointer holds.