How a CPU Executes Your Code
When you write a simple program like:
1x = 102y = 203z = x + yit looks almost like normal English.
But a CPU doesn't understand x, y, +, or = directly.
The CPU only understands machine instructions — very small operations represented as binary data.
So how does your code go from something you write into instructions that a CPU can actually execute?
Let's follow the journey.
From Code to CPU
Imagine you write a simple C program:
1int a = 10;2int b = 20;3int c = a + b;The journey looks roughly like this:
1Your Code2 │3 ▼4Compiler5 │6 ▼7Machine Code8 │9 ▼10Memory11 │12 ▼13CPU14 │15 ▼16Instructions Executed17 │18 ▼19ResultThe CPU doesn't directly read your source code.
A compiler first translates it into instructions that the processor understands.
What Does the CPU Actually Understand?
At the lowest level, a CPU works with machine instructions.
A simplified instruction might mean:
1LOAD2ADD3STOREFor example, your code:
1c = a + b;could conceptually become something like:
1LOAD a2LOAD b3ADD4STORE cThe actual machine instructions are much more complicated and depend on the CPU architecture, but this simplified version helps us understand the basic idea.
The CPU essentially performs tiny operations one after another.
The CPU Has Different Parts
A modern CPU contains several important components.
Some of the most important ones are:
- Control Unit
- ALU
- Registers
- Cache
- Instruction Decoder
You can imagine the CPU like a small factory:
1 CPU2 ┌───────────────────┐3 │ │4 │ Instruction │5 │ Decoder │6 │ │ │7 │ ▼ │8 │ Control │9 │ Unit │10 │ │ │11 │ ┌────┴────┐ │12 │ ▼ ▼ │13 │ Registers ALU │14 │ │15 └───────────────────┘Each part has a different job.
Registers: The CPU's Tiny Workspace
Registers are very small storage locations inside the CPU.
They are extremely fast compared with normal memory.
For example, imagine the CPU has registers like:
1R1 = 102R2 = 20The CPU can perform:
1R3 = R1 + R2Now:
1R3 = 30Registers are useful because the CPU frequently needs to work with values while executing instructions.
Think of them as the CPU's scratchpad.
The ALU: Where Calculations Happen
The ALU, or Arithmetic Logic Unit, performs many basic arithmetic and logical operations.
For example:
110 + 20210 - 5310 AND 1410 OR 1When the CPU needs to perform an addition, the ALU does the actual operation.
For example:
1R1 = 102R2 = 203 4 ┌─────────┐5R1 ───►│ │6 │ ALU │───► 307R2 ───►│ │8 └─────────┘The ALU is one of the places where your program's calculations are physically carried out.
Memory: Where Your Program Lives
Before the CPU can execute your program, the program's instructions and data need to be available in memory.
For example, your program might contain:
1Instruction 12Instruction 23Instruction 34Instruction 4The CPU needs to fetch these instructions one by one.
A simplified view looks like:
1 RAM2 ┌──────────────────┐3 │ Instruction 1 │4 │ Instruction 2 │5 │ Instruction 3 │6 │ Instruction 4 │7 │ Data │8 └────────┬─────────┘9 │10 ▼11 CPUBut the CPU doesn't normally fetch everything directly from RAM.
That would be too slow.
This is where CPU caches become important.
CPU Cache: Keeping Data Close
Modern CPUs have small amounts of extremely fast memory called cache.
Common cache levels are:
1L1 → Very small and very fast2L2 → Larger but slower3L3 → Larger again4RAM → Much larger but slowerYou can imagine them like different distances from your desk:
1CPU2 │3 ├── L1 Cache ← Right beside you4 │5 ├── L2 Cache ← Very close6 │7 ├── L3 Cache ← Nearby8 │9 └── RAM ← Further awayIf the CPU repeatedly needs the same data, keeping it in cache can save a lot of time.
This is one reason modern CPUs can execute instructions extremely quickly.
The Instruction Cycle
Now we get to the most important part.
The CPU repeatedly performs a basic process often described as:
Fetch → Decode → Execute
Let's see what that means.
1. Fetch
The CPU needs to know which instruction to execute next.
A special register called the Program Counter (PC) keeps track of where the next instruction is located.
Imagine memory contains:
11000 → LOAD R1, 1021004 → LOAD R2, 2031008 → ADD R1, R241012 → STORE R1, resultThe Program Counter might initially contain:
1PC = 1000The CPU fetches the instruction stored at that location.
1PC2 │3 ▼41000 → LOAD R1, 10Now the CPU has the instruction it needs to work on.
2. Decode
The CPU now needs to understand what the instruction means.
For example:
1ADD R1, R2The instruction decoder identifies:
1Operation → ADD2Input 1 → R13Input 2 → R2It then tells the appropriate parts of the CPU what needs to happen.
Think of this as translating a command into a series of actions the hardware can perform.
3. Execute
Now the CPU actually performs the operation.
If:
1R1 = 102R2 = 20and the instruction is:
1ADD R1, R2the ALU performs the addition:
110 + 20 = 30The result can then be placed into a register:
1R1 = 30The CPU then moves on to the next instruction.
The CPU Keeps Repeating This
The CPU doesn't execute just one instruction.
It repeats this process continuously:
1 ┌─────────┐2 │ Fetch │3 └────┬────┘4 ▼5 ┌─────────┐6 │ Decode │7 └────┬────┘8 ▼9 ┌─────────┐10 │ Execute │11 └────┬────┘12 │13 └──────────► Fetch next instructionThis happens incredibly quickly.
A CPU running at several gigahertz can have billions of clock cycles per second, although clock cycles are not the same thing as instructions; modern CPUs can execute multiple instructions in overlapping ways.
What Is a CPU Clock?
You may have seen specifications such as:
13.5 GHz CPUGHz means gigahertz.
One gigahertz means one billion cycles per second.
So a 3.5 GHz clock has approximately:
13.5 billion clock cycles per secondThe clock acts like a timing signal that helps synchronize operations inside the CPU.
Think of it like a conductor keeping an orchestra synchronized.
But again, one clock cycle does not necessarily mean one instruction.
Modern processors use techniques that allow multiple instructions to be in progress at the same time.
What About an if Statement?
Consider this code:
1if (x > 10) {2 y = 20;3}The CPU doesn't see this as a high-level if.
The compiler converts it into lower-level instructions involving a comparison and a conditional branch.
Conceptually:
1Compare x with 102 │3 ▼4Is x greater than 10?5 / \6 Yes No7 │ │8 ▼ ▼9 y = 20 ContinueThe CPU performs the comparison and then decides which instruction should execute next.
This is called a branch.
Modern CPUs even try to predict which way a branch will go before the result is known.
This is called branch prediction.
What About a Loop?
Consider:
1for (int i = 0; i < 5; i++) {2 printf("%d", i);3}The CPU doesn't understand "repeat five times" as a single magical operation.
The compiler turns the loop into lower-level instructions that roughly do this:
1Set i = 02 3Check i < 54 │5 ├── No → Exit6 │7 ▼8Execute loop body9 │10 ▼11Increase i12 │13 └──────► Check againThe CPU keeps jumping back to the comparison until the condition becomes false.
What Makes Modern CPUs So Fast?
A modern CPU does much more than simply:
1Fetch2Decode3Execute4Fetch5Decode6ExecuteIt can work on multiple instructions at the same time.
One important technique is called pipelining.
Imagine an assembly line:
1Instruction 1 → Fetch → Decode → Execute2Instruction 2 → Fetch → Decode → Execute3Instruction 3 → Fetch → Decode → ExecuteInstead of waiting for Instruction 1 to completely finish before starting Instruction 2, different stages can overlap.
This keeps the CPU's different components busy.
Modern CPUs also use techniques such as:
- Multiple CPU cores
- Out-of-order execution
- Branch prediction
- Instruction pipelining
- Multiple levels of cache
- SIMD/vector instructions
These techniques allow CPUs to process huge amounts of work very quickly.
What Does a CPU Core Mean?
Modern processors often have multiple cores.
For example:
1CPU2├── Core 13├── Core 24├── Core 35└── Core 4Each core can execute its own stream of instructions.
This means multiple tasks can make progress at the same time.
For example:
1Core 1 → Browser2Core 2 → Music player3Core 3 → Game4Core 4 → Background tasksThe operating system decides how software threads are scheduled across the available CPU cores.
So What Happens to Your x + y?
Let's go back to our original example:
1int x = 10;2int y = 20;3int z = x + y;A simplified version of what happens is:
1Your source code2 │3 ▼4 Compiler5 │6 ▼7Machine instructions8 │9 ▼10Instructions loaded into memory11 │12 ▼13 CPU14 │15 ├── Fetch16 ├── Decode17 ├── Execute18 │19 ▼20 Registers21 │22 ▼23 ALU24 │25 ▼26 10 + 2027 │28 ▼29 30The actual CPU instructions depend on the processor architecture, compiler, optimization settings, and programming language.
But the fundamental idea remains the same:
Your high-level code is transformed into machine instructions, and the CPU executes those instructions using its internal hardware.
What looks like a simple line of code to us can become many small machine instructions.
The CPU then processes those instructions at incredible speed, using registers, caches, execution units, pipelines, and multiple cores to keep the work moving.
Conclusion
A CPU doesn't understand the programming languages we write.
It understands machine instructions.
A compiler or other translation stage turns our code into instructions that match the target CPU architecture. The instructions are loaded into memory, and the CPU repeatedly fetches, decodes, and executes them.
When you write:
1z = x + y;the CPU doesn't see a simple mathematical statement.
It sees a sequence of very small operations:
Get the values → perform the addition → store the result.
And it performs those tiny operations billions of times per second.