A CPU works by repeating one loop billions of times per second: fetch the next instruction from memory, decode what it asks for, then execute it. Every program you run, from a browser to a game, is a long list of these small instructions.
This article explains that loop step by step. For what the chip is and how to check which one you have, see what is a CPU. Here the focus is what happens inside it while a program runs.
The short version: fetch, decode, execute
The names below are the standard textbook ones. Microchip's PIC documentation describes the same steps for its processors, and notes that a fetch cycle begins with the Program Counter (PC) incrementing, and that the fetched instruction is latched into the Instruction Register (IR).
- Fetch. The CPU reads the instruction stored at the address held in the program counter, then advances the counter to the next address.
- Decode. The CPU works out what the instruction asks for: which operation, and which values or registers it uses.
- Execute. The CPU carries out the operation, such as adding two numbers, copying a value, or jumping to a different address.
Then the loop starts again with the next instruction. Nothing in the CPU "understands" the program. It follows this loop mechanically, and the useful behaviour comes from the instructions it is given.
A worked example with a tiny made-up program
The listing below is a simplified illustration, not real x86 or Arm code. It adds 2 and 3 and stores the result. Each line is one instruction at its own address.
Address Instruction
0 LOAD A, 2 ; put the number 2 in register A
1 LOAD B, 3 ; put the number 3 in register B
2 ADD A, B ; A = A + B
3 STORE A, [100] ; write A to memory address 100
4 JUMP 0 ; go back to address 0
Here is what the loop does with it:
| Step | Program counter | Fetch | Execute result |
|---|---|---|---|
| 1 | 0 | LOAD A, 2 | A holds 2 |
| 2 | 1 | LOAD B, 3 | B holds 3 |
| 3 | 2 | ADD A, B | A holds 5 |
| 4 | 3 | STORE A, [100] | Memory address 100 holds 5 |
| 5 | 4 | JUMP 0 | Program counter is set back to 0 |
The last row shows how loops work. Most instructions leave the program counter to advance by one. A jump overwrites it. Microchip's documentation says the same about its design: if an instruction causes the PC to change, for example GOTO, two cycles are required to complete the instruction.
What are registers, and why does the CPU use them?
Registers (A and B above) are the CPU's own tiny storage slots. Instructions mostly operate on values held in registers because that is the fastest place to read from. Larger data sits in RAM and has to be loaded into a register first, which is why the example starts with LOAD instructions.
How can a CPU do three steps at once? Pipelining
Running fetch, decode and execute strictly one after another would leave each part of the chip idle two thirds of the time. Processors overlap the steps instead, so while one instruction executes, the next is already being fetched. This is called pipelining.
Microchip's PIC documentation gives a concrete small-scale example. Fetch and execute are pipelined: a fetch takes one instruction cycle, while decode and execute take another. Because they overlap, each instruction effectively completes in one instruction cycle, except instructions that change the PC, which take two. The same page defines one instruction cycle as four cycles of the oscillator clock on that chip. A PIC is a microcontroller, much simpler than a desktop CPU, so treat its numbers as an illustration of the idea and not as a description of your laptop's processor. The vendor manuals for each design give the real details.
The PIC page shows why a jump costs extra: it changes the PC, so the instruction that was already being fetched is not the one that runs next.
What does clock speed have to do with it?
The clock is the CPU's metronome. Each tick lets the chip move work forward one step. Windows reports a processor's speed in MHz through the CurrentClockSpeed and MaxClockSpeed properties of Win32_Processor, according to Microsoft.
You can do the arithmetic yourself. A reported MaxClockSpeed of 3400 means 3,400 MHz, or 3.4 GHz, which is 3,400,000,000 clock ticks per second. One tick lasts 1 / 3,400,000,000 s, about 0.29 nanoseconds.
A tick is not an instruction. Some instructions need several ticks, and a modern core can finish more than one instruction per tick on favourable code. That is why clock speed alone does not rank CPUs. CPU clock speed explained covers this in more detail.
Why does the CPU have cache?
Fetching an instruction or a value from RAM is slow compared with the speed at which the CPU can use it. Cache is the fix. Microsoft's documentation describes L2 and L3 cache as memory with a faster access time than main RAM, and the Win32_Processor class reports their sizes in kilobytes (L2CacheSize, L3CacheSize).
The idea is simple: keep recently used instructions and data close to the cores, so the next fetch is more likely to be answered quickly. When the data is not in cache, the CPU has to wait for RAM.
What do multiple cores change?
Everything above describes one core running one instruction stream. Microsoft defines a core as a physical processor on the integrated circuit, so a dual-core processor has two, each running its own fetch, decode and execute loop. Intel describes the CPU design as a small number of powerful cores focused on individual tasks, aimed at getting them done quickly.
Two cores can run two programs, or two parts of one program, at the same time. A single stream of instructions does not speed up just because more cores exist, unless the program is written to split its work. Hyper-threading adds logical processors: Microsoft notes that if it is enabled, NumberOfCores is less than NumberOfLogicalProcessors. The difference between cores and threads has its own article: CPU cores vs threads.
See the numbers for your own CPU
On Windows, this PowerShell command prints the values mentioned above. The values depend on your hardware.
Get-CimInstance Win32_Processor | Select-Object Name, NumberOfCores, NumberOfLogicalProcessors, MaxClockSpeed, L2CacheSize, L3CacheSize
Example output (yours will differ):
Name : Example Processor 3.40GHz
NumberOfCores : 8
NumberOfLogicalProcessors : 16
MaxClockSpeed : 3400
L2CacheSize : 8192
L3CacheSize : 16384
Here 3400 is MHz and the cache sizes are in kilobytes, so this example has 8 MB of L2 and 16 MB of L3 cache. Sixteen logical processors on 8 cores suggests two threads per core.
What a CPU does not do
The CPU does not store your files, and it is not the only processor in the computer. Intel's comparison notes that a GPU divides highly parallel work, such as graphics, across many smaller cores. The CPU stays the general-purpose part that runs the operating system and your programs. To see what your own CPU is doing right now, you can check CPU and GPU temperature.
Frequently asked questions
- How many instructions can a CPU run per second?
- It depends on the chip and the program, and the clock speed alone does not tell you. A clock of 3.4 GHz means 3.4 billion cycles per second. How many instructions finish per cycle varies by design and by workload, so two CPUs at the same GHz can do different amounts of work.
- What is the program counter?
- The program counter (PC) is a register that tracks which instruction to fetch next. In Microchip's PIC documentation, a fetch cycle begins with the PC incrementing. A jump or branch instruction changes the PC, which is how a program loops and makes decisions.
- Does the CPU understand code like Python or JavaScript?
- No. A CPU runs machine instructions only. Source code is first translated into them by a compiler, or run by an interpreter that is itself a program made of machine instructions.
- Why does a CPU need cache if it already has RAM?
- Cache sits on the processor and has a faster access time than main RAM, according to Microsoft's documentation. Keeping recently used data close means the CPU waits less often for memory.