Theory: The Fetch–Decode–Execute Cycle & RTL Traces
A program is a sequence of stored instructions. The CPU runs it by repeating one loop, the fetch–decode–execute cycle (also called the instruction cycle), from power-on until it halts. Every instruction, no matter how simple or complex, passes through the same stages. Being able to trace the registers through this cycle is one of the most common exam questions on MARIE.
The Four Stages
- Fetch: copy the instruction whose address is in the PC from memory into the IR, and advance the PC so it points at the next instruction.
- Decode: look at the opcode IR[15–12] to find out which instruction this is, and copy the address field IR[11–0] into the MAR.
- Get operand (only if needed): if the instruction uses a value from memory (Load, Add, Subt, AddI, …), read it into the MBR.
- Execute: carry out the instruction’s remaining microoperations. Then go back to step 1.
Diagram loads as you scroll.
The RTL of Fetch and Decode
These microoperations are identical for every instruction:
Fetch: MAR <- PC
IR <- M[MAR]
PC <- PC + 1
Decode: MAR <- IR[11-0]
(decode IR[15-12])
Operand: MBR <- M[MAR] (only if the instruction needs it)
Execute: ... depends on the opcode ...
The PC is incremented during fetch, before the instruction executes. So while an instruction is executing, the PC already holds the address of the next instruction. This is why Skipcond skips by doing one more PC ← PC + 1, and why JnS saves the PC as the return address.
Mini-example
The PC holds 100 and M[100]=1104 (Load 104). Fetch: MAR = 100, IR = 1104, PC = 101. Decode: MAR = IR[11–0] = 104, opcode 1 = Load. Operand: MBR = M[104]. Execute: AC ← MBR.
Reading a Trace Table
A trace table has one row per microoperation and one column per register (PC, IR, MAR, MBR, AC). Write a register’s value after that microoperation; values that do not change are copied down. Dashes mean “unknown / not yet set”. Highlighting in your head which cell changes in each row is the fastest way to check yourself: each microoperation changes exactly one register (the one on the left of the arrow), except the fetch step that also increments the PC.
The Addition Program (Null & Lobur, Table 4.3)
| Address | Instruction | Hex |
|---|
| 100 | Load 104 | 1104 |
| 101 | Add 105 | 3105 |
| 102 | Store 106 | 2106 |
| 103 | Halt | 7000 |
| 104 | 0023 | 0023 |
| 105 | FFE9 | FFE9 |
| 106 | 0000 | 0000 |
It computes 002316+FFE916, i.e. 35+(−23)=12=000C16, and stores the result in 106. The full register traces are in Worked Example 1.
Interrupts (brief)
Real machines add one more step to the loop: after each instruction, the CPU checks for an interrupt (a signal from an I/O device, a timer, or an error such as division by zero; a program can also raise a software interrupt). If one is pending, the CPU saves the PC (and other state), runs an interrupt service routine, then restores the state and continues the interrupted program. MARIE’s basic cycle omits interrupts so the core loop stays simple.
Diagram loads as you scroll.
Worked Examples
Exercise 1
Trace the register contents (PC, IR, MAR, MBR, AC) for every microoperation of the addition program at addresses 100–106 (Load 104, Add 105, Store 106, Halt, with data 0023, FFE9, 0000). Initially PC = 100. What is in location 106 at the end?
Show solution ↓Hide solution ↑
Solution
(a) Load 104
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| (initial values) | | 100 | ---- | ---- | ---- | ---- |
| Fetch | MAR ← PC | 100 | ---- | 100 | ---- | ---- |
| IR ← M[MAR] | 100 | 1104 | 100 | ---- | ---- |
| PC ← PC + 1 | 101 | 1104 | 100 | ---- | ---- |
| Decode | MAR ← IR[11–0] | 101 | 1104 | 104 | ---- | ---- |
| (decode IR[15–12]) | 101 | 1104 | 104 | ---- | ---- |
| Get operand | MBR ← M[MAR] | 101 | 1104 | 104 | 0023 | ---- |
| Execute | AC ← MBR | 101 | 1104 | 104 | 0023 | 0023 |
(b) Add 105
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| (initial values) | | 101 | 1104 | 104 | 0023 | 0023 |
| Fetch | MAR ← PC | 101 | 1104 | 101 | 0023 | 0023 |
| IR ← M[MAR] | 101 | 3105 | 101 | 0023 | 0023 |
| PC ← PC + 1 | 102 | 3105 | 101 | 0023 | 0023 |
| Decode | MAR ← IR[11–0] | 102 | 3105 | 105 | 0023 | 0023 |
| (decode IR[15–12]) | 102 | 3105 | 105 | 0023 | 0023 |
| Get operand | MBR ← M[MAR] | 102 | 3105 | 105 | FFE9 | 0023 |
| Execute | AC ← AC + MBR | 102 | 3105 | 105 | FFE9 | 000C |
0023+FFE9=1000C; the carry out of bit 15 is discarded in a 16-bit register, leaving 000C = 12 ✓ (35−23=12).
(c) Store 106
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| (initial values) | | 102 | 3105 | 105 | FFE9 | 000C |
| Fetch | MAR ← PC | 102 | 3105 | 102 | FFE9 | 000C |
| IR ← M[MAR] | 102 | 2106 | 102 | FFE9 | 000C |
| PC ← PC + 1 | 103 | 2106 | 102 | FFE9 | 000C |
| Decode | MAR ← IR[11–0] | 103 | 2106 | 106 | FFE9 | 000C |
| (decode IR[15–12]) | 103 | 2106 | 106 | FFE9 | 000C |
| Get operand | (not necessary) | 103 | 2106 | 106 | FFE9 | 000C |
| Execute | MBR ← AC | 103 | 2106 | 106 | 000C | 000C |
| M[MAR] ← MBR | 103 | 2106 | 106 | 000C | 000C |
(d) Halt
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| (initial values) | | 103 | 2106 | 106 | 000C | 000C |
| Fetch | MAR ← PC | 103 | 2106 | 103 | 000C | 000C |
| IR ← M[MAR] | 103 | 7000 | 103 | 000C | 000C |
| PC ← PC + 1 | 104 | 7000 | 103 | 000C | 000C |
| Decode | MAR ← IR[11–0] | 104 | 7000 | 000 | 000C | 000C |
| Execute | (halt) | 104 | 7000 | 000 | 000C | 000C |
Location 106 now holds 000C (= 12).
Exercise 2
Using the addition program, answer without drawing the full table:
(a) Which registers change during the fetch of any instruction?
(b) Which instruction(s) of the program do not need the “get operand” step? Why?
(c) What value is in the PC while Add 105 is executing?
(d) How many memory reads does the complete program perform (fetches + operands)?
Show solution ↓Hide solution ↑
Solution
(a) MAR (← PC), IR (← M[MAR]) and PC (← PC + 1). MBR and AC are untouched by fetch.
(b) Store 106 (it writes the AC to memory; it does not read a value) and Halt (no memory operand at all).
(c) 102: the PC was incremented during fetch, so it already points at the next instruction (Store 106).
(d) 4 instruction fetches (Load, Add, Store, Halt) + 2 operand reads (Load, Add) = 6 memory reads, plus 1 memory write (Store).
Exercise 3
Trace the sample1.mas program (it starts at address 010):
ORG 010
load x
subt y
output
halt
x, dec -12
y, dec 20
Give its hex code and trace PC, IR, MAR, MBR and AC through all four instructions. What does it output?
Show solution ↓Hide solution ↑
Solution
Addresses: 010 load x, 011 subt y, 012 output, 013 halt, 014 x, 015 y. So x = 014, y = 015.
Hex: 1014, 4015, 6000, 7000, FFF4 (−12: 12=000C, invert → FFF3, +1 → FFF4), 0014 (20).
| Instr. | Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| initial | | 010 | ---- | ---- | ---- | ---- |
| load x | Fetch | MAR ← PC | 010 | ---- | 010 | ---- | ---- |
| | IR ← M[MAR] | 010 | 1014 | 010 | ---- | ---- |
| | PC ← PC + 1 | 011 | 1014 | 010 | ---- | ---- |
| Decode | MAR ← IR[11–0] | 011 | 1014 | 014 | ---- | ---- |
| Operand | MBR ← M[MAR] | 011 | 1014 | 014 | FFF4 | ---- |
| Execute | AC ← MBR | 011 | 1014 | 014 | FFF4 | FFF4 |
| subt y | Fetch | MAR ← PC | 011 | 1014 | 011 | FFF4 | FFF4 |
| | IR ← M[MAR] | 011 | 4015 | 011 | FFF4 | FFF4 |
| | PC ← PC + 1 | 012 | 4015 | 011 | FFF4 | FFF4 |
| Decode | MAR ← IR[11–0] | 012 | 4015 | 015 | FFF4 | FFF4 |
| Operand | MBR ← M[MAR] | 012 | 4015 | 015 | 0014 | FFF4 |
| Execute | AC ← AC − MBR | 012 | 4015 | 015 | 0014 | FFE0 |
| output | Fetch | MAR ← PC | 012 | 4015 | 012 | 0014 | FFE0 |
| | IR ← M[MAR] | 012 | 6000 | 012 | 0014 | FFE0 |
| | PC ← PC + 1 | 013 | 6000 | 012 | 0014 | FFE0 |
| Decode | MAR ← IR[11–0] | 013 | 6000 | 000 | 0014 | FFE0 |
| Execute | OutREG ← AC | 013 | 6000 | 000 | 0014 | FFE0 |
| halt | Fetch | MAR ← PC | 013 | 6000 | 013 | 0014 | FFE0 |
| | IR ← M[MAR] | 013 | 7000 | 013 | 0014 | FFE0 |
| | PC ← PC + 1 | 014 | 7000 | 013 | 0014 | FFE0 |
| Decode | MAR ← IR[11–0] | 014 | 7000 | 000 | 0014 | FFE0 |
−12−20=−32. Check the hex: 32=0020, invert → FFDF, +1 → FFE0 ✓. The program outputs −32.
Exercise 4
Part of a program to find the larger of A = 7 and B = 12:
| Address | Instruction | Hex |
|---|
| 102 | Skipcond 000 | 8000 |
| 103 | Jump 107 | 9107 |
| 104 | Load B (B at 10D) | 110D |
| 105 | Store Max (Max at 10E) | 210E |
| 106 | Jump 109 | 9109 |
Just before address 102 executes: PC = 102, IR = 410D, MAR = 10D, MBR = 000C, AC = FFFB. Trace the instructions at 102, 104 and 106 (in the order they execute) and say where execution goes after each.
Show solution ↓Hide solution ↑
Solution
AC = FFFB = −5 (it is A−B=7−12).
Skipcond 000 at 102
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| initial | | 102 | 410D | 10D | 000C | FFFB |
| Fetch | MAR ← PC | 102 | 410D | 102 | 000C | FFFB |
| IR ← M[MAR] | 102 | 8000 | 102 | 000C | FFFB |
| PC ← PC + 1 | 103 | 8000 | 102 | 000C | FFFB |
| Decode | MAR ← IR[11–0] | 103 | 8000 | 000 | 000C | FFFB |
| Execute | IR[11–10] = 00 and AC < 0, so PC ← PC + 1 | 104 | 8000 | 000 | 000C | FFFB |
The Jump 107 at 103 is skipped. Next instruction: 104.
Load B at 104
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| Fetch | MAR ← PC | 104 | 8000 | 104 | 000C | FFFB |
| IR ← M[MAR] | 104 | 110D | 104 | 000C | FFFB |
| PC ← PC + 1 | 105 | 110D | 104 | 000C | FFFB |
| Decode | MAR ← IR[11–0] | 105 | 110D | 10D | 000C | FFFB |
| Operand | MBR ← M[MAR] | 105 | 110D | 10D | 000C | FFFB |
| Execute | AC ← MBR | 105 | 110D | 10D | 000C | 000C |
(105 Store Max then writes 000C to 10E; PC becomes 106.)
Jump 109 at 106 (registers before: PC = 106, IR = 210E, MAR = 10E, MBR = 000C, AC = 000C)
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| Fetch | MAR ← PC | 106 | 210E | 106 | 000C | 000C |
| IR ← M[MAR] | 106 | 9109 | 106 | 000C | 000C |
| PC ← PC + 1 | 107 | 9109 | 106 | 000C | 000C |
| Decode | MAR ← IR[11–0] | 107 | 9109 | 109 | 000C | 000C |
| Execute | PC ← IR[11–0] | 109 | 9109 | 109 | 000C | 000C |
Execution continues at 109 with Max = 000C = 12, the larger value ✓.
Exercise 5
Memory holds: M[300]=D305 (LoadI 305), M[305]=0310, M[310]=0099. With PC = 300, trace the instruction through fetch, decode and execute. What ends up in the AC?
Show solution ↓Hide solution ↑
Solution
| Step | RTN | PC | IR | MAR | MBR | AC |
|---|
| initial | | 300 | ---- | ---- | ---- | ---- |
| Fetch | MAR ← PC | 300 | ---- | 300 | ---- | ---- |
| IR ← M[MAR] | 300 | D305 | 300 | ---- | ---- |
| PC ← PC + 1 | 301 | D305 | 300 | ---- | ---- |
| Decode | MAR ← IR[11–0] | 301 | D305 | 305 | ---- | ---- |
| Operand | MBR ← M[MAR] | 301 | D305 | 305 | 0310 | ---- |
| Execute | MAR ← MBR | 301 | D305 | 310 | 0310 | ---- |
| MBR ← M[MAR] | 301 | D305 | 310 | 0099 | ---- |
| AC ← MBR | 301 | D305 | 310 | 0099 | 0099 |
The AC gets 0099: location 305 held the address 310, and location 310 held the data. Note the two memory reads after decode, which is the cost of indirection.
Exercise 6
Explain, register by register, what happens in the fetch–decode–execute cycle for Subt 0FF stored at address 200, given M[0FF]=0005 and AC = 0012 before the instruction.
Show solution ↓Hide solution ↑
Solution
Subt 0FF encodes as 40FF.
- MAR ← PC: the MAR gets 200, the address of the instruction.
- IR ← M[MAR]: memory location 200 is read; the IR gets 40FF.
- PC ← PC + 1: the PC becomes 201, ready for the next instruction.
- MAR ← IR[11–0]: the address field 0FF goes into the MAR; the control unit decodes IR[15–12] = 0100 = Subt.
- MBR ← M[MAR]: Subt needs an operand, so M[0FF] = 0005 is read into the MBR.
- AC ← AC − MBR: the ALU computes 0012−0005=000D (18 − 5 = 13) and the result goes back into the AC via the dedicated AC/MBR/ALU path.
Final: PC = 201, IR = 40FF, MAR = 0FF, MBR = 0005, AC = 000D ✓.