📘 CodingMarble Learn

Processor Internals: Registers, the Fetch–Execute Cycle, Addressing Modes and Interrupts

A processor has an ALU, a control unit, a clock and registers (PC, MAR, MDR, CIR, accumulator, status register), joined to main memory by the address, data and control buses. Each instruction is fetched (MAR ← [PC]; MDR ← [Memory[MAR]], PC ← [PC] + 1; CIR ← [MDR]), decoded into opcode and operand, and executed. Operands use immediate addressing (the value itself) or direct addressing (a memory address). Assembly language uses mnemonics such as LDR, STR, ADD, SUB, CMP, B and BEQ. Performance depends on cores, cache, clock speed, word length and bus widths. Interrupts make the processor save its volatile environment on a stack, run an interrupt service routine and then resume.

🎬 Step-by-step story

  1. These are the parts of the processor: the ALU does sums, the CU gives orders, the clock ticks, and registers hold tiny bits of data. Buses carry addresses and data.
  2. Fetch: the address in PC goes to MAR. The instruction comes from memory into MDR, then into CIR. PC goes up by one.
  3. Decode and execute: the CU splits the instruction into an opcode and an operand. With direct addressing the operand is an address. With immediate addressing it is the value.
  4. Now a short assembly program runs. Load 7, add 3, store 10, halt. Watch the accumulator change.
  5. An interrupt arrives. The processor finishes the instruction, saves PC and registers on a stack, runs a special routine, then restores them and carries on.
  6. Free play: step through the program yourself, switch the addressing mode and send an interrupt.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why do we need registers if we have RAM?

Registers are inside the CPU and are far faster; the CPU works on values in registers and uses RAM for everything else.

Why does PC go up before the instruction runs?

So it already points at the next instruction; a branch can then simply overwrite PC.

How does the CPU know whether an operand is a value or an address?

The addressing mode is part of the opcode bits; the CU reads it while decoding.

Why is MDR used when storing a result?

All transfers to and from memory go through MDR, with MAR holding the address.

Why use a stack for saving state?

A stack is last-in, first-out, so nested interrupts unwind in the right order.

What changes if I switch ADD to direct addressing?

ADD then fetches Memory[10] instead of using 3. Try it in free play and compare ACC.

The processor and its components

The fetch–decode–execute cycle

Written in register transfer notation ([ ] means 'contents of'):

  1. MAR ← [PC]: copy the address of the next instruction into MAR.
  2. PC ← [PC] + 1: PC now points at the following instruction.
  3. MDR ← [Memory]addressed: the instruction is read along the data bus into MDR.
  4. CIR ← [MDR]: copy the instruction to CIR.
  5. Decode: the CU splits the instruction into opcode (what to do) and operand (what to do it to).
  6. Execute: for example load a value into ACC, use the ALU, store a result, or change PC for a branch.
  7. Check for interrupts, then repeat.

Instruction sets and addressing modes

An instruction set is the complete list of machine instructions a particular processor can run; each processor family has its own. A machine-code instruction has an opcode (which includes the basic operation and the addressing mode) and one or more operands.

Example: if address 10 holds 7, then ADD #10 adds 10 but ADD 10 adds 7.

Assembly language operations

Assembly language uses short mnemonics that map one-to-one onto machine code. Typical operations (shown in a common style):

Selection and loops are written with CMP and conditional branches to labels.

Factors affecting processor performance

Interrupts and saving the volatile environment

An interrupt is a signal to the processor that something needs attention: a key press, a timer, a printer out of paper, a hardware fault, or a software error.

  1. The processor finishes the current fetch–execute cycle, then checks for interrupts.
  2. If one is waiting and has a higher priority than the current task, the volatile environment (PC, registers, status flags) is pushed onto a stack.
  3. The PC is loaded with the address of the right interrupt service routine (ISR), often found through an interrupt vector table.
  4. The ISR runs (it may itself be interrupted by a higher-priority interrupt).
  5. The saved values are popped back from the stack and the original program continues exactly where it stopped.

Key formulas and definitions

Worked examples

1. Describe what happens in each register during the fetch stage.

PC holds the address of the next instruction; it is copied to MAR. The instruction at that address travels on the data bus to MDR, and PC is incremented. The instruction is then copied from MDR to CIR, ready for decoding.

2. Address 20 holds 5 and the accumulator holds 2. Give ACC after (a) ADD #20 (b) ADD 20.

(a) Immediate: ACC = 2 + 20 = 22. (b) Direct: ACC = 2 + Memory[20] = 2 + 5 = 7.

3. How many memory locations can a 24-bit address bus address?

2²⁴ = 16,777,216 locations (16 Mi).

4. Trace: MOV R0, #4 / ADD R0, R0, #6 / CMP R0, #10 / BEQ done / MOV R0, #0 / done: HALT.

R0 = 4, then 10. CMP sets the zero flag because R0 = 10. BEQ jumps to done, so MOV R0, #0 is skipped. The program halts with R0 = 10.

5. Why must the volatile environment be saved when an interrupt happens?

The ISR will use the same registers and change PC. Without saving PC, registers and flags, the original program could not resume at the right place with the right values.

6. Doubling the cores does not double the speed of a program. Why?

Only parts of the program that can run in parallel benefit; the rest runs on one core. Cores also share cache and memory bandwidth and must coordinate, which costs time.

Common mistakes

Practice quiz

1. Which register holds the address of the next instruction?
2. Which bus is one-way from CPU to memory?
3. In ADD #5, the addressing mode is:
4. Where is the volatile environment saved during an interrupt?
5. Which does NOT directly improve processor performance?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the main registers in a CPU?

PC, MAR, MDR, CIR, the accumulator or general-purpose registers, and the status register.

What is the difference between immediate and direct addressing?

Immediate uses the operand as the value; direct uses it as the memory address where the value is.

What is an interrupt service routine?

A small program the processor runs to deal with an interrupt, after saving its current state on a stack.

Where this is taught

England (GCSE, A level)Year 124.7 Computer organisation and architecture
England (GCSE, A level)Year 134.6-4.7 Computer systems and architecture (A-level)

Learn first

Learn next

Related lessons

All Computer Science lessons