Watch on Instagram

Code Bytes · "How Computers Work" · Episode 5 · Hardware

The fetch-decode-execute cycle

A CPU runs a program by repeating three steps for every instruction. Fetch grabs the next instruction from memory (RAM), decode works out what it means, and execute does the job. Then it starts again. That loop is the fetch-decode-execute cycle, also called the instruction cycle.

Posted · About a 1 minute watch, 3 minute read · Take the quiz

A recipe, one step at a time

Think about following a recipe. You read the next step, work out what it means, then do it. Then you move on to the next step and do the same again.

Your CPU (central processing unit, the "brain" of the computer) works the same way. A program is a long list of small instructions, and while it runs, those instructions wait in RAM (the computer's working memory, from Code Bytes EP29), one after another.

The three steps

  1. Fetch. The CPU grabs the next instruction from memory.
  2. Decode. It works out what that instruction means: which job to do and which data it needs. For example, "add these two numbers".
  3. Execute. It does the job. For "add 2 + 3", the answer is 5.

Then it goes back to step 1 for the next instruction. This loop is called the fetch-decode-execute cycle (or the instruction cycle), and a CPU keeps running it from the moment the computer starts until it shuts down.

The steps in Python

This Python loop doesn't make the CPU do anything special. It just prints the names of the three steps in order, the way the CPU repeats them:

``python steps = ["fetch", "decode", "execute"] for step in steps: print(step) ``

`` fetch decode execute ``

The for loop takes each item in the list steps, one at a time and in order, and runs print(step) for it.

The "add 2 + 3" example looks like this in Python:

``python x = 2 x = x + 3 print(x) ``

`` 5 ``

Even these three short lines become many smaller instructions before anything runs. How code gets turned into those instructions comes later in this course.

Try it yourself

Open any free online Python editor (search "online Python") and run:

``python recipe = ["read", "understand", "do"] for step in recipe: print("Now:", step) ``

You should see:

`` Now: read Now: understand Now: do ``

Try adding a fourth word to the list and run it again: the loop runs once more, without changing anything else.

Good to know

  • Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown.
  • The CPU keeps track of where the next instruction is with a small counter called the program counter.
  • "Billions of times a second" is a simplification. One instruction can take several clock ticks, while modern CPUs also overlap the steps of several instructions at once (called pipelining) and have several cores working side by side. They also keep copies of instructions in small, very fast memory called cache, so they don't have to wait for RAM every time.
  • "Add these two numbers" is an illustration. Real machine instructions are written in binary, in a format that depends on the CPU.
  • You can see this in Python: the dis module shows that x = 2 / x = x + 3 / print(x) becomes many more, smaller steps (bytecode).

Answer to the comment question

A program has 4 instructions. Each goes through all three steps once. How many steps is that?

12 steps.

Each of the 4 instructions is fetched, decoded and executed: 4 × 3 = 12. You can check it in Python 3:

``python print(4 * 3) ``

`` 12 ``

Test your knowledge

Quick quiz: The fetch-decode-execute cycle

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What are the three steps of the CPU's cycle, in order?
2Where do a running program's instructions wait before the CPU fetches them?
3What happens in the decode step?
4What does this print? for step in ["fetch", "decode"]: print(step)

Sources

  • Britannica, "central processing unit": the CPU fetches instructions from main memory, decodes them and carries them out
  • Wikipedia, "Instruction cycle": the fetch-decode-execute cycle, also called the instruction cycle, that a CPU repeats from boot-up until shutdown
  • Python Software Foundation, "for Statements": a for loop runs its body once for each item in a list, in order
  • Python Software Foundation, "dis: Disassembler for Python bytecode": used to check that a three-line Python program becomes many lower-level steps

Video transcript

What three steps does your CPU repeat billions of times a second?

Think of a recipe. You read the next step, work out what it means, then do it. Then repeat.

Your CPU works the same way. Its instructions wait in RAM, one after another.

Step one is fetch: the CPU grabs the next instruction from memory.

Step two is decode: it works out what that instruction means, like add these two numbers.

Step three is execute: it does the job, and the answer is five.

Together, that's the fetch-decode-execute cycle. This Python loop prints the three steps in order, the way the CPU repeats them.

A program has four instructions. Each goes through all three steps once. How many steps is that? Comment your answer below!

That's your byte.

More from "How Computers Work"

All "How Computers Work" lessons