CPU basics (fetch-decode-execute)
Computer Science · Computer architecture & systems
Question 1 of 10
0How many distinct values can a single bit store?
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
CPU basics (fetch-decode-execute) explained
The central processing unit (CPU) is the part of a computer that carries out programs. It repeats a simple cycle called fetch, decode and execute, many millions of times every second.
A program is a list of instructions stored in main memory. A special register called the program counter holds the address of the next instruction to run.
- Fetch: the control unit copies the instruction at the address in the program counter into the instruction register, then moves the program counter on to the next address.
- Decode: the control unit works out what the instruction means and which data it needs.
- Execute: the arithmetic logic unit (ALU) or another part of the CPU carries out the operation, and the result is stored in a register or in memory.
Jump instructions change the program counter, which lets a program repeat code or make choices. Clock speed is measured in hertz (Hz), the number of cycles per second, so a 3 GHz processor ticks three billion times per second.
A common mistake is to think the CPU stores whole programs or files. It only holds the current instruction and data in small, very fast registers, while main memory and storage keep everything else.
Worked example
In this simplified model, a 2 GHz CPU needs 4 clock cycles to complete one instruction. How many instructions does it complete in one second?
- Clock speed: 2 GHz means cycles per second.
- Each instruction takes 4 cycles, so .
- It completes 500 million instructions per second.
Key facts
- The practice questions use these parts of the CPU: the program counter (PC), memory address register (MAR), memory data register (MDR), instruction register (IR, also called the current instruction register, CIR), the accumulator (ACC) and the ALU.
- The PC holds the address of the next instruction. The MAR holds the address being read or written. The MDR holds the data coming from or going to memory. The CIR holds the instruction being decoded.
- The address bus carries the address and the data bus carries the value. Registers are tiny and very fast, and they sit inside the CPU. RAM is much larger and slower.
- Cache keeps frequently used data close to the CPU. Pipelining lets different instructions be in different stages at the same time. An interrupt pauses the current task for an urgent event.
- Time for a program: .
Worked example 2
A 3 GHz CPU runs a program of instructions. In this simplified model every instruction takes 4 clock cycles. How long does the program take?
- Total cycles: cycles.
- Clock speed: 3 GHz is cycles per second.
- Time: s.
Check the units: cycles divided by cycles per second gives seconds. A higher clock speed helps, but so does needing fewer cycles for each instruction.
Worked example 3
Trace a program through the fetch-decode-execute cycle. A made-up processor has these memory contents, where each instruction is stored at its own address:
Address Contents
100 LOAD 200
101 ADD 201
102 STORE 202
103 HALT
200 7
201 5
202 0
LOAD copies a memory value into ACC. ADD adds a memory value to ACC. STORE copies ACC into memory. The PC starts at 100.
Cycle 1
- Fetch: MAR = 100. The instruction LOAD 200 goes to the MDR, then to the CIR. PC becomes 101.
- Decode: the control unit sees LOAD and the address 200.
- Execute: MAR = 200, MDR = 7, ACC = 7.
Cycle 2
- Fetch: MAR = 101, CIR = ADD 201, PC = 102.
- Execute: MAR = 201, MDR = 5. The ALU does , so ACC = 12.
Cycle 3
- Fetch: MAR = 102, CIR = STORE 202, PC = 103.
- Execute: ACC is written to address 202, which now holds 12.
Cycle 4
- Fetch: MAR = 103, CIR = HALT, PC = 104. The CPU stops.
The PC went up by one in every fetch, before the instruction was executed. A jump instruction would replace that new value with a different address.
Worked example 4
A CPU has 3 stages (fetch, decode, execute), each taking 1 clock cycle. Compare the number of cycles needed for 5 instructions without pipelining and with ideal pipelining. The clock speed is 2 GHz.
- Without pipelining each instruction takes 3 cycles and the next one starts only when it finishes: cycles.
- With pipelining, a new instruction enters the fetch stage every cycle. The first instruction finishes after 3 cycles, and then one more finishes every cycle: cycles.
- At 2 GHz one cycle is ns.
- Without pipelining: ns. With pipelining: ns.
Pipelining does not make one instruction faster. It raises the number of instructions finished per second. A jump can break the pipeline, because the instructions already fetched are the wrong ones.
Common mistakes
- Saying the CPU stores programs or files. It holds only the current instruction and data in registers.
- Updating the PC after execution instead of during the fetch.
- Mixing up the MAR (an address) and the MDR (a value).
- Saying a higher clock speed always means a faster computer. Cores, cache and cycles per instruction matter too.
- Forgetting to convert GHz to cycles per second ().
- Saying pipelining makes a single instruction finish sooner.
Exam tips
- Set out a register trace as a table or list: one row per step, with PC, MAR, MDR, CIR and ACC.
- Describe the fetch stage in order: PC to MAR, memory to MDR, MDR to CIR, PC incremented.
- For a timing question, write cycles, then cycles per second, then divide, with units.
- Name the register or bus, not just "the CPU", when you explain what moves where.
- When a question mentions a jump or interrupt, say the PC changes, and say what the new value is.
What you will practise
105 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.
Example questions
- The CPU’s job is to:
- The fetch-decode-execute cycle:
- Registers are:
- The ALU performs:
- A clock cycle sets the CPU’s:
- The program counter (PC) holds:
- Which bus carries memory addresses between the CPU and memory?
- In the von Neumann architecture, where are program instructions and data stored?
…and 97 more.