01What Does the CPU Actually Execute?

When we write programs, we might see C++, Python, or other high-level languages. But the CPU doesn't ultimately execute these texts; it executes machine instructions.

Each machine instruction tells the processor to do something relatively specific, such as: read data, add, compare, jump, or write results to a location.

Calculate

Addition, subtraction, bitwise operations, etc.

Move Data

Move data between registers and memory

Compare

Determine the relationship between two values

Jump

Decide where to execute next

02How Does the CPU Know Where the Next Instruction Is?

Inside the CPU, information about "where to fetch the next instruction from" is stored. This location is usually represented by a special register, often called Program Counter (PC); in different processor architectures, it might have different names.

Machine Instructions in Program

Address 100Instruction A
Address 104Instruction B
Address 108Instruction C
Address 112Instruction D

Program Counter PC

104

Points to the location of the next instruction to fetch

03What Do Fetch, Decode, and Execute Mean?

For beginners, we often use Fetch → Decode → Execute to understand the basic working cycle of the CPU.

1

Fetch

Fetch the machine instruction to be executed based on the instruction address

2

Decode

Determine what this instruction does and what operands it needs

3

Execute

Complete calculation, data access, or jump operations through corresponding execution units

4

Write Back / Complete Result

Write results that need to be kept to registers, memory, or other locations

04Why Are Registers and Cache Important?

CPU speed is much higher than main memory. If every small calculation had to directly wait for RAM, a lot of time would be wasted.

So faster storage levels are used inside and near the CPU: Registers → CPU Cache → RAM → SSD / Hard Drive.

Registers

Located directly inside the processor, storing small amounts of data most commonly used in current calculations

CPU Cache

Stores recently used instructions and data

RAM

Larger capacity, slower than cache

SSD or Hard Drive

SSD / Hard Drive

Long-term storage, large capacity, but higher latency

05What Happens When Encountering if Statements and Loops?

Programs don't always go straight from top to bottom to the end. if, for, while, function calls, etc., ultimately make the CPU encounter jump or branch-related machine instructions.

Compare Condition

For example: is x greater than 10?

Yes → Execute Path A
No → Execute Path B

One essence of loops is to make the program, when certain conditions are met, jump back to a previous instruction location to continue execution.

06Do Modern CPUs Really Work One by One Slowly?

If we only look at the introductory model, it's easy to think that the CPU completely finishes one instruction before starting the next. Modern high-performance CPUs usually aren't this simple.

Pipelining

Different instructions can be in different stages like fetch, decode, execute at the same time

Out-of-Order Execution

Without breaking observable program results, the processor can do work that's already ready first

Branch Prediction

Predict which branch the program might take in advance to reduce waiting

Superscalar Execution

Some CPUs can issue or complete multiple independent operations per clock cycle

07Putting It All Together with a Small Example

Suppose the program needs to complete: First calculate 3 + 5, and if the result is greater than 6, save the result.

result = 3 + 5; if (result > 6) { save(result); }
1

Fetch Related Machine Instructions

PC points to the next instruction to be processed

2

Fetch Data 3 and 5

May come from registers, cache, or lower-level storage

3

Execute Addition

Get result 8

4

Compare 8 and 6

Get condition result "8 is greater than 6"

5

Change Execution Path

Enter the part of the code that saves the result

What You've Learned

  • The CPU ultimately executes machine instructions, not directly executing C++ source code.
  • Mechanisms like the program counter help the processor determine where to fetch the next instruction from.
  • "Fetch → Decode → Execute" is a classic introductory model for understanding CPU work.
  • During execution, the CPU heavily uses registers and cache, rather than directly accessing RAM every time.
  • if statements, loops, and function calls ultimately affect which segment of machine instructions the program executes next.
  • The CPU doesn't "think"; it just changes its state and produces results according to instructions and data.
  • Modern CPUs use techniques like pipelining, out-of-order execution, branch prediction, and superscalar to improve efficiency.
  • Real CPU internal implementation is more complex than the teaching model, and different processor architectures may vary.

Next Question: If the CPU only has a limited number of cores at any one time, why can we listen to music, browse web pages, and have chat software running at the same time? Next article: How Can Computers Run Many Programs at Once?