Skip to content

Instruction Cycle ​

The instruction cycle is the series of steps the CPU goes through from taking one instruction out of memory to finishing its execution. The CPU keeps repeating this cycle until the program ends.

At a Glance ​

  • Analogy: it is like a cook following a recipe.
    1. Look at the line where the bookmark (PC) is placed and read that one line of the recipe. (Fetch)
    2. Understand what that line means: "Stir-fry the onion on fridge shelf 3." (Decode)
    3. Actually take the onion out of the fridge and stir-fry it. (Execute)
    4. Move the bookmark to the next line and go back to step 1.
  • During this process, several registers inside the CPU each briefly hold the values they are responsible for.

Registers and Units Involved ​

NameWhat it does
PC (Program Counter)Stores the address of the instruction to execute next
MAR (Memory Address Register)Specifies the address of the memory location to read or write next
MBR (Memory Buffer Register)Temporarily stores data to be written to memory, or data read from memory
IR (Instruction Register)Stores the instruction currently being executed
ALU (Arithmetic Logic Unit)Performs arithmetic and logical operations

The role of each component is covered in more detail in How a CPU Works.

Overall Flow ​

The instruction cycle is divided into four cycles: fetch, indirect, execute, and interrupt. The indirect cycle and the interrupt cycle are only gone through when needed.

text
          ┌─────────────────────────────────────────────┐
          ▼                                             │
   ┌─────────────┐  if indirect   ┌─────────────┐       │
   │ Fetch cycle │ ─────────────▶ │  Indirect   │       │
   │   (Fetch)   │                │    cycle    │       │
   └──────┬──────┘                └──────┬──────┘       │
          │ if direct                    │              │
          ▼                              ▼              │
   ┌──────────────────────────────────────────┐         │
   │          Execute cycle (Execute)         │         │
   └─────────────────────┬────────────────────┘         │
                         │                              │
            Was there an interrupt request?             │
             ├── No ────────────────────────────────────┤
             └── Yes ─▶ Interrupt cycle (Interrupt) ────┘

Fetch Cycle ​

The step that brings the next instruction to execute from main memory into the CPU's instruction register (IR)

  1. Take the instruction address in the PC into the MAR.
  2. Fetch the value at the address stored in the MAR from memory and store it in the MBR. At the same time, increment the PC so it points to the next instruction.
  3. Move the instruction in the MBR to the IR.
text
T0 : MAR ← PC
T1 : MBR ← M[MAR],  PC ← PC + 1
T2 : IR  ← MBR
  • What the fetch cycle brings in is always an instruction (opcode + operand). The data used for computation is read separately later, in the indirect cycle or the execute cycle.
  • PC + 1 means "the next instruction"; in practice, the address increases by the size of one instruction (e.g. 4 bytes).

Decode ​

The instruction that has arrived in the IR is interpreted by the control unit.

  • It looks at the opcode and figures out "this is an addition" or "this means store to memory."
  • It generates the control signals that match the result and sends them to the ALU, registers, memory, and so on.

The IR is where the instruction is held, and the interpreting is done by the control unit (the instruction decoder inside it). Decoding does not turn the instruction into data; it turns the instruction into signals to send to each unit. Depending on the textbook, decoding is sometimes included at the end of the fetch cycle or at the start of the execute cycle.

Indirect Cycle ​

This is the step taken when the operand field does not directly hold the address where the data is, but instead holds "the address of the place where the address is written." It reads the actual data address from memory one more time and replaces the address part of the IR.

text
T0 : MAR      ← IR(Addr)
T1 : MBR      ← M[MAR]
T2 : IR(Addr) ← MBR

As an analogy, it is an instruction like "take out the ingredient from the shelf written on the note on fridge shelf 3." You have to read the note first to learn the real shelf number.

Execute Cycle ​

This is the step that actually carries out the decoded instruction. The needed data is brought from memory into the MBR, and the ALU performs the operation.

  • The result is usually stored in a register inside the CPU (such as the AC).
  • The result is stored in memory only for store instructions like STA (store), in which case the result goes through the MBR into memory.
  • Branch instructions like JUMP change the PC value instead of calculating, which changes where the next instruction is fetched from.

The execution steps for each instruction (ADD, LOAD, STA, JUMP) are laid out in the "Execute Cycle" section of How a CPU Works.

Interrupt Cycle ​

After the execute cycle finishes, the CPU checks whether an interrupt request, such as keyboard input or a timer, came in during that time.

  • If there was no request, it goes straight to the fetch cycle of the next instruction.
  • If there was a request, it saves the current PC value (where to come back to) in a safe place (such as the stack) and changes the PC to the start address of the interrupt service routine. When handling is done, it restores the saved PC value and continues what it was doing.
text
T0 : MBR    ← PC
T1 : MAR    ← save location,  PC ← interrupt service routine address
T2 : M[MAR] ← MBR

What interrupts are and how the operating system handles them is covered in Interrupts.

Going Further: Pipelining ​

Modern CPUs do not wait for one instruction's cycle to finish completely. Just as you put the previous load into the dryer while the washing machine is running, the CPU decodes instruction 2 and fetches instruction 3 while executing instruction 1. This is called pipelining.

text
Time →         1        2        3        4        5
Instruction 1  Fetch    Decode   Execute
Instruction 2           Fetch    Decode   Execute
Instruction 3                    Fetch    Decode   Execute

Even though each instruction takes the same amount of time, the number of instructions finished in a given period increases.

Real CPUs split the stages even more finely. The figure below is the 5-stage pipeline often seen in textbooks. It proceeds in the order IF (fetch), ID (decode), EX (execute), MEM (memory access), WB (write the result back to a register), and at the moment shaded green, five instructions are each processing a different stage at the same time.

An instruction pipeline proceeding in five overlapping stages: IF, ID, EX, MEM, WB

Image source: Wikimedia Commons (CC BY-SA 3.0)

Summary ​

ConceptPlain-language summary
Instruction cycleThe steps from fetching one instruction to executing it. Repeated continuously
FetchBring the instruction the bookmark (PC) points to into the IR and move the bookmark to the next line
DecodeThe control unit figures out what the instruction means and creates signals to send to each unit
IndirectRead the note with the address one more time to learn the real address
ExecuteThe ALU calculates and usually puts the result in a register
InterruptIf an urgent request came in, save the current position and go handle it
PipeliningOverlap the stages of several instructions so they proceed at the same time