Back to Articles
CPUTechnology

How a CPU Executes Your Code

CIBIVISHNU A C
CIBIVISHNU A C
10 min2 views

When you write a simple program like:

1x = 10
2y = 20
3z = x + y

it 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 Code
2 │
3 ▼
4Compiler
5 │
6 ▼
7Machine Code
8 │
9 ▼
10Memory
11 │
12 ▼
13CPU
14 │
15 ▼
16Instructions Executed
17 │
18 ▼
19Result

The 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:

1LOAD
2ADD
3STORE

For example, your code:

1c = a + b;

could conceptually become something like:

1LOAD a
2LOAD b
3ADD
4STORE c

The 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 CPU
2 ┌───────────────────┐
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 = 10
2R2 = 20

The CPU can perform:

1R3 = R1 + R2

Now:

1R3 = 30

Registers 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 + 20
210 - 5
310 AND 1
410 OR 1

When the CPU needs to perform an addition, the ALU does the actual operation.

For example:

1R1 = 10
2R2 = 20
3 
4 ┌─────────┐
5R1 ───►│ │
6 │ ALU │───► 30
7R2 ───►│ │
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 1
2Instruction 2
3Instruction 3
4Instruction 4

The CPU needs to fetch these instructions one by one.

A simplified view looks like:

1 RAM
2 ┌──────────────────┐
3 │ Instruction 1 │
4 │ Instruction 2 │
5 │ Instruction 3 │
6 │ Instruction 4 │
7 │ Data │
8 └────────┬─────────┘
9 │
10 ▼
11 CPU

But 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 fast
2L2 → Larger but slower
3L3 → Larger again
4RAM → Much larger but slower

You can imagine them like different distances from your desk:

1CPU
2 │
3 ├── L1 Cache ← Right beside you
4 │
5 ├── L2 Cache ← Very close
6 │
7 ├── L3 Cache ← Nearby
8 │
9 └── RAM ← Further away

If 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, 10
21004 → LOAD R2, 20
31008 → ADD R1, R2
41012 → STORE R1, result

The Program Counter might initially contain:

1PC = 1000

The CPU fetches the instruction stored at that location.

1PC
2 │
3 ▼
41000 → LOAD R1, 10

Now 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, R2

The instruction decoder identifies:

1Operation → ADD
2Input 1 → R1
3Input 2 → R2

It 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 = 10
2R2 = 20

and the instruction is:

1ADD R1, R2

the ALU performs the addition:

110 + 20 = 30

The result can then be placed into a register:

1R1 = 30

The 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 instruction

This 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 CPU

GHz means gigahertz.

One gigahertz means one billion cycles per second.

So a 3.5 GHz clock has approximately:

13.5 billion clock cycles per second

The 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 10
2 │
3 ▼
4Is x greater than 10?
5 / \
6 Yes No
7 │ │
8 ▼ ▼
9 y = 20 Continue

The 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 = 0
2 
3Check i < 5
4 │
5 ├── No → Exit
6 │
7 ▼
8Execute loop body
9 │
10 ▼
11Increase i
12 │
13 └──────► Check again

The 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:

1Fetch
2Decode
3Execute
4Fetch
5Decode
6Execute

It can work on multiple instructions at the same time.

One important technique is called pipelining.

Imagine an assembly line:

1Instruction 1 → Fetch → Decode → Execute
2Instruction 2 → Fetch → Decode → Execute
3Instruction 3 → Fetch → Decode → Execute

Instead 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:

1CPU
2├── Core 1
3├── Core 2
4├── Core 3
5└── Core 4

Each core can execute its own stream of instructions.

This means multiple tasks can make progress at the same time.

For example:

1Core 1 → Browser
2Core 2 → Music player
3Core 3 → Game
4Core 4 → Background tasks

The 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 code
2 │
3 ▼
4 Compiler
5 │
6 ▼
7Machine instructions
8 │
9 ▼
10Instructions loaded into memory
11 │
12 ▼
13 CPU
14 │
15 ├── Fetch
16 ├── Decode
17 ├── Execute
18 │
19 ▼
20 Registers
21 │
22 ▼
23 ALU
24 │
25 ▼
26 10 + 20
27 │
28 ▼
29 30

The 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.