How the Fetch-Decode-Execute Cycle Works in a CPU (Step by Step)

Fetch-Decode-Execute Cycle: How a CPU Runs a Simple Counting Program

The fetch-decode-execute cycle is the basic loop that every processor repeats, whether it sits inside a phone, a laptop, or a game console. Every app you open, every video you stream, and every key you press is handled by this loop, one tiny step at a time.

In this guide, we’ll walk through the fetch-decode-execute cycle using a small counting program. You’ll see what the CPU, its clock, and its registers actually do, and how a few simple instructions in memory become a working program.

What’s in this article

  1. What a CPU is and how the clock sets the pace
  2. The CPU registers you need to know
  3. How RAM stores instructions and data
  4. The fetch-decode-execute cycle in three stages
  5. From machine code to high-level languages
  6. Why speed makes complex tasks possible
  7. Frequently asked questions

What Is a CPU and Why Does the Clock Matter?

A CPU, or Central Processing Unit, sits at the center of a phone or PC and connects to the other parts that make the device work. It reads instructions, does the calculations, and tells other components what to do.

Every CPU follows a clock. With each tick, the processor takes one small step in the fetch-decode-execute cycle. A slower clock means fewer steps per second, and a faster clock means more. The clock doesn’t make the CPU smarter. It simply sets the rhythm.

The CPU Registers Every Beginner Should Know

CPU registers are small, extremely fast storage areas inside the processor that hold the values the CPU is using right now. A simplified computer needs only three of them:

  • Program Counter: tracks the address of the next instruction.
  • Instruction Register: holds the instruction that was just fetched from memory.
  • Accumulator: holds the value the CPU is currently working with, such as the result of an addition.

These CPU registers are tiny compared to main memory, but they’re far faster to reach. That’s why the processor keeps its working values there instead of constantly going back to RAM. The program counter is especially important, because it decides the order in which the program runs.

RAM: Where Instructions and Results Live

RAM, or Random Access Memory, is where the program lives while it runs. The CPU can read or write any location in RAM without following a set order, which is what “random access” means.

RAM stores both the program’s instructions and its results. Each instruction has an operation, such as LOAD or ADD, and usually a memory address. A real computer stores these in binary, but in our example they’re shown as readable numbers so the logic is easy to follow.

The Fetch-Decode-Execute Cycle in Three Stages

Our example program counts upward by adding one again and again. Its instructions sit in RAM, and the CPU repeats the same three stages for each one. This is the fetch-decode-execute cycle in action.

Stage 1: Fetch

The program counter starts at address 0. The CPU fetches the instruction stored there and places it in the instruction register.

After each instruction, the program counter moves to the next address. A jump instruction can change where it points instead. This is how loops and decisions are possible.

Stage 2: Decode

Next, the CPU reads the instruction and works out its operation and its address. The first instruction is LOAD with address 6. The following instructions are ADD with address 7, STORE with address 6, and JUMP with address 1.

Decoding is where the CPU figures out what a pattern of bits actually means.

Stage 3: Execute

Now the CPU carries out the instruction:

  • LOAD takes the value at address 6 and places it in the accumulator. The value there is 1.
  • ADD takes the value at address 7, also 1, and adds it to the accumulator. The result is 2.
  • STORE writes that result back to address 6, replacing the 1 stored there.
  • JUMP sends the program counter back to address 1.

After the jump, the CPU runs ADD, STORE, and JUMP again, adding one and saving the new value each time. The count goes up once every nine clock ticks, because three instructions each pass through the three stages of the fetch-decode-execute cycle.

This small program has no halt command and no way to be interrupted, so it never stops by itself. Eventually the value grows too large for its memory location to hold.

From Machine Code to High-Level Languages

At the lowest level, instructions are encoded in binary, which is very hard for people to read. They can be written in hexadecimal (base 16) as machine code. Machine code is exactly what the CPU understands, but it’s tedious for humans to write.

Assembly language is a more readable alternative. It uses symbolic names like LOAD and ADD instead of raw numbers, yet it still works very close to the processor’s basic instructions. The original Prince of Persia game was programmed entirely in assembly language, which meant painstakingly encoding each pixel of animation. That kind of work is difficult to write and debug, and human errors can lead to serious security problems.

High-level languages solve this. They let programmers focus on what they want the computer to do, while the computer handles memory reading and writing behind the scenes. For our counting example, the code simply defines a variable, X, then loops forever and increases X by one each time.

A compiler then turns that code into machine code, which the computer loads into memory and runs. To use the same program on a different kind of device, such as a phone instead of a PC, you compile it for that CPU. Whatever the language, the machine code it produces still runs through the same fetch-decode-execute cycle.

How Speed Makes Complex Tasks Possible

In our demonstration, the CPU runs slowly, finishing one instruction every couple of seconds on a single thread. A modern CPU can run billions of instructions per second, measured in gigahertz, across multiple threads.

That speed is what lets a computer perform complex tasks, such as decompressing and displaying a video. Underneath all that power, the CPU still relies on a clock, a handful of CPU registers, and the same fetch-decode-execute cycle. Nothing about the basic idea changes. It just happens billions of times faster.

Key Takeaways

  • The fetch-decode-execute cycle is how a CPU runs every instruction.
  • The program counter tells the CPU which instruction comes next.
  • CPU registers hold the values the processor is working with right now.
  • RAM stores both instructions and results.
  • Machine code is what the CPU runs, assembly language is its readable form, and high-level languages are compiled down to machine code.

Frequently Asked Questions


Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab

Subscribe to get the latest posts sent to your email.

Leave a Comment

Discover more from PiEmbSysTech - Embedded Systems & VLSI Lab

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version