Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 67 ยท CPU Design

CPU Registers and the Instruction Cycle in Detail

Meet the registers inside a CPU (PC, IR, MAR, MDR, accumulator, flags) and trace fetch, decode, execute and interrupt cycles step by step with a Python model.

Earlier lessons described the fetch-decode-execute cycle in a sentence. Hardware cannot work with sentences: every step is a transfer between named storage locations inside the processor. In this lesson you will meet those locations, called registers, and follow one instruction through them, including what happens when a device interrupts the CPU in the middle of a program.

The machine we use is deliberately small: one accumulator, a 12-bit address and a few instructions. It is the classic textbook arrangement, and all the ideas carry over to the register file of a real datapath. You should know the Von Neumann model first.

The registers that run the cycle

Some registers are visible to programmers (like the accumulator), and some exist only so the hardware can do its job.

Register Name What it holds
PC Program counter the address of the next instruction to fetch
IR Instruction register the instruction currently being decoded and executed
MAR Memory address register the address placed on the address bus for a memory access
MDR (MBR) Memory data register the word just read from memory, or about to be written
ACC Accumulator the working value of the ALU (in accumulator machines)
FLAGS (PSW) Status register zero, negative, carry, overflow and the interrupt-enable bit
SP Stack pointer the address of the top of the stack

The MAR and MDR are the CPU's two hands on the memory bus. The CPU never talks to memory directly: it puts an address in MAR, asks memory to read, and the data appears in MDR. A write works in reverse: put the address in MAR, the data in MDR, then ask memory to store.

flowchart LR
  PC["PC"] --> MAR["MAR"]
  MAR -->|"address bus"| MEM[("Memory")]
  MEM -->|"data bus"| MDR["MDR"]
  MDR --> IR["IR"]
  IR --> CU["Control unit<br/>decodes"]
  CU --> ALU["ALU and ACC"]
  MDR --> ALU

An instruction fetch: the PC feeds the MAR, memory answers in the MDR, and the word is copied into the IR for decoding.

The instruction cycle

Each instruction passes through the same phases:

  1. Fetch: MAR <- PC, read memory into MDR, IR <- MDR, PC <- PC + 1.
  2. Decode: the control unit splits IR into the opcode and the operand field and selects what to do.
  3. Operand fetch (if the instruction reads memory): MAR <- the operand address, read into MDR.
  4. Execute: the ALU computes, a register is loaded, or MDR is written back to memory. A jump simply changes PC.
  5. Interrupt check: if an interrupt is pending and enabled, handle it before the next fetch.

Incrementing the PC during fetch is important: by the time the instruction executes, the PC already points at the next one, which is why jump targets and PC-relative offsets are computed from the following address.

A machine you can run

The model below has 12-bit words with a 4-bit opcode and 8-bit address: LOAD, ADD, STORE, JUMPZ (jump if the zero flag is set), SUB and HALT. Every transfer is logged, so you can read the register traffic.

Python One instruction at a time, register by register

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the log for instruction 1: three memory-related registers (MAR, MDR, IR) are used just to get a single instruction from memory into the decoder, and a second MAR/MDR pair of transfers fetches the operand. That is why memory speed dominates performance and why caches matter.

Cycles, states and the control unit

The control unit steps through the phases using a small counter or state machine. In a simple design each phase takes one or more clock cycles, and instructions that need an operand fetch take longer than a register-only one. How the control signals for each step are produced is the topic of the control unit lesson; pipelining overlaps these phases for several instructions at once, as shown in pipelining.

The interrupt cycle

Devices like keyboards and timers need attention at unpredictable times. Instead of the CPU checking them constantly, a device raises an interrupt request. At the end of each instruction, the CPU checks for a pending request. If interrupts are enabled it runs an interrupt cycle:

  1. Save the state that the interrupted program needs, at least the PC and the flags (usually pushed on the stack).
  2. Disable further interrupts (so the handler is not itself interrupted unexpectedly).
  3. Load the PC with the address of the handler, found in an interrupt vector table.
  4. Run the handler, ending with a return-from-interrupt instruction that restores the saved PC and flags.

Because the check happens only between instructions, an instruction is never half done when the program is interrupted. That makes it safe to resume.

Python A timer interrupt between instructions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The program still finishes with the right answer even though the handler ran in the middle: that is the whole purpose of saving and restoring the PC.

Many real CPUs also save the general registers in the handler (software) or switch to a shadow register bank (hardware) so the interrupted code finds every register unchanged.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the program counter (PC) hold?

    The PC points at the next instruction and is normally incremented during the fetch phase.
  2. 2. Which pair of registers connects the CPU to main memory?

    The MAR holds the address sent to memory and the MDR holds the data read or to be written.
  3. 3. During which phase is the PC normally incremented?

    The PC is advanced while the instruction is fetched, so it already points to the following instruction when execution starts.
  4. 4. When does a CPU normally check for pending interrupts?

    Checking only at instruction boundaries means the interrupted program is never left with a half-finished instruction.
  5. 5. What must the CPU save before running an interrupt handler, at minimum?

    The PC says where to resume and the flags hold condition bits the interrupted code may still need.

Summary

  • The PC, IR, MAR, MDR, accumulator, flags and stack pointer are the registers that run the instruction cycle.
  • The CPU reaches memory only through MAR (address) and MDR (data).
  • Fetch copies the instruction into IR and advances the PC; decode selects the action; execute does the work; jumps change the PC.
  • The interrupt cycle saves the PC and flags, loads the handler address, runs the handler and then restores the saved state.
  • Interrupts are checked between instructions, so programs can always be resumed cleanly.

Continue with Register Transfer and Micro-operations.