How a CPU works: fetch, decode, execute, step by step
A CPU looks like magic, but it runs on one simple loop it repeats forever. Here it is, one step at a time, running a real little program.
Press Next to walk through it. Watch the accumulator go from 0 to 5 to 8 as the CPU fetches each instruction, decodes it, and executes it.
What just happened
Every CPU, from the one in a calculator to the one in your phone, runs the same three-beat cycle over and over. Fetch: the program counter holds the address of the next instruction, and the CPU copies that instruction out of memory into the instruction register. Decode: the control unit reads the instruction and figures out which operation it names and what it acts on. Execute: the CPU carries it out, using the ALU for arithmetic and the accumulator to hold the running value, then bumps the program counter to the next line.
That is the entire idea. A program is just a list of these tiny instructions sitting in memory, and "running" it means walking the program counter down the list, one fetch-decode-execute at a time. Speed is the only trick: a real chip does this a few billion times every second. The instructions themselves are built from the machine code a decoder turns into control signals, which are built from gates, which are built from transistors. You can build those gates and, one level deeper, see how a transistor switches.
Common questions
How does a CPU work?
A CPU runs a program by repeating a three-step cycle: fetch, decode, execute. It fetches the next instruction from memory (the program counter says where), decodes what it means, and executes it, doing arithmetic in the ALU, moving data, or storing a result. Then the program counter advances and the cycle repeats, billions of times a second.
What is the fetch-decode-execute cycle?
It is the basic loop a CPU runs for every instruction. Fetch: read the instruction at the program counter's address into the instruction register. Decode: the control unit works out what operation it asks for. Execute: carry it out, then advance the program counter.
What is the program counter?
The program counter (PC) is a small register holding the address of the next instruction. After each instruction it usually increments by one, so the CPU steps through the program in order; jumps and branches work by writing a new address into it.
What does the ALU do?
The ALU (arithmetic logic unit) does the actual computing: add, subtract, and bitwise AND, OR, XOR, NOT. During execute, the control unit feeds it the operands and selects the operation. See it built up in the ALU lesson.
What is the accumulator?
The accumulator is a register that holds a running value. Instructions like LOAD and ADD read from or write to it, so it accumulates the result as the program runs. It is one of the CPU's registers.