Fetch-Execute cycle

The Fetch-Execute Cycle

The Fetch-Execute cycle (also called the instruction cycle) is the continuous process by which the CPU reads and carries out program instructions. From the moment a computer is switched on until it is switched off, the CPU repeats this cycle billions of times per second. Every program instruction - whether it adds two numbers, displays a pixel, or reads from a file - passes through all three stages of the cycle before it is complete.

The three stages are Fetch, Decode, and Execute. They happen in sequence, and as soon as one instruction finishes executing, the cycle immediately begins again for the next instruction.

FETCH get instruction DECODE work out what it is EXECUTE carry it out continuously repeated

The CPU repeats Fetch → Decode → Execute for every instruction in a program.

The Three Stages in Detail

Stage 1: Fetch

The CPU needs to know which instruction to run next. It keeps track of this using the Program Counter (PC) - a register that always holds the memory address of the next instruction to be fetched. At the start of the fetch stage, the address held in the PC is sent out along the address bus to main memory.

Main memory responds by sending the instruction stored at that address back to the CPU along the data bus. The instruction arrives at a register inside the CPU ready to be examined. Once the instruction has been fetched, the Program Counter is automatically updated to point to the address of the next instruction in sequence, ready for the next cycle.

CPU Program Counter: 1004 Instruction arrives here Main Memory Address 1004: ADD R1, R2 address bus (1004) data bus (instruction) PC updates to 1005 ready for next cycle

Key point: the Program Counter is updated automatically during (or immediately after) the fetch stage, not after execution. This means the CPU always knows where to fetch the next instruction from without needing to work it out.

Stage 2: Decode

Once the instruction has been fetched into the CPU, it must be decoded. The Control Unit (CU) examines the instruction and determines what operation needs to be performed. An instruction typically consists of two parts:

  • The opcode - the operation code that specifies what action to take (e.g. ADD, LOAD, STORE, JUMP)
  • The operand(s) - the data or memory address(es) the operation should act upon

The Control Unit interprets the opcode and sends appropriate control signals to the relevant components of the CPU, directing them to prepare for the required action. For example, if the opcode is ADD, the CU will direct data to be loaded into the ALU ready for an arithmetic operation. If the opcode is LOAD, the CU will prepare to fetch data from a specified memory address.

Decoding happens entirely within the CPU - no data leaves the processor during this stage. It is a very fast process that takes only a fraction of a clock cycle on modern processors.

Stage 3: Execute

The decoded instruction is now carried out. The exact actions depend on the type of instruction:

  • Arithmetic/logic instruction (e.g. ADD, SUB, AND): the ALU performs the calculation or logical operation using data held in registers. The result is placed back into a register.
  • Load instruction: data is read from a specified address in main memory and loaded into a CPU register. This involves sending the address along the address bus and receiving the data along the data bus.
  • Store instruction: data is written from a CPU register to a specified address in main memory. This involves sending both the address and the data out over the buses.
  • Jump instruction: the Program Counter is updated to a different address rather than the next sequential one, causing the CPU to execute instructions from a different location in the program.

After execution is complete, the cycle returns to the Fetch stage immediately. The Program Counter already holds the address of the next instruction, so the CPU knows exactly where to fetch from. This continuous loop repeats for the entire duration the program is running.

Execute: possible outcomes ALU operation arithmetic / logic Memory read LOAD instruction Memory write STORE instruction Jump update PC address

The Complete Cycle: A Worked Example

Imagine a program contains the instruction "ADD the value in register 1 to the value in register 2 and store the result." Here is how the FDE cycle handles it:

  1. Fetch: The CPU checks the Program Counter, which holds address 1004. It sends 1004 along the address bus to memory. Memory responds with the ADD instruction, which travels back along the data bus. The PC is updated to 1005.
  2. Decode: The Control Unit reads the instruction. It identifies the opcode as ADD and determines that the operands are register 1 and register 2. It sends control signals to direct the relevant data to the ALU.
  3. Execute: The ALU receives the values from registers 1 and 2, performs the addition, and places the result in a register. The cycle immediately returns to Fetch, using the address now in the PC (1005) to retrieve the next instruction.

 Key Takeaways

  • The Fetch-Execute cycle is the continuous process by which the CPU retrieves and carries out instructions. It repeats for every instruction in a running program.
  • Fetch: the CPU reads the next instruction from the address held in the Program Counter and loads it into the CPU. The Program Counter is then updated to the next address.
  • Decode: the Control Unit interprets the instruction's opcode to determine what action is needed and sends control signals to prepare the appropriate components.
  • Execute: the instruction is carried out - this may involve the ALU performing a calculation, or reading/writing data to and from main memory.
  • Some instructions (LOAD, STORE) require a second access to main memory during the Execute stage, using the buses to transfer data between the CPU and RAM.