← Back to Computer Science

The CPU and the fetch-execute cycle

The central processing unit runs programs by repeatedly fetching, decoding, and executing instructions billions of times per second.

Components of a computer

Most computers follow the von Neumann model: a central processing unit (CPU), memory holding both programs and data, input and output devices, and buses (communication pathways) connecting them. The CPU is often called the brain of the computer.

Inside the CPU

The CPU contains the arithmetic logic unit (ALU), which performs calculations and comparisons; the control unit, which directs operations; and registers, tiny ultra-fast storage locations. Key registers include the program counter (address of the next instruction) and the instruction register (current instruction).

The fetch-decode-execute cycle

The CPU repeatedly fetches the next instruction from memory at the address in the program counter, decodes it to determine what to do, executes it (for example, adding numbers or moving data), and updates the program counter. Each cycle moves the program forward one instruction.

Machine code and instruction sets

Each CPU family understands a specific instruction set architecture (ISA), its set of basic instructions in binary machine code. Common ISAs include x86-64 (used in most PCs and servers) and ARM (used in most phones, tablets, and Apple's M-series Macs). Assembly language is a human-readable form of machine code.

Clock speed and performance

A CPU's clock coordinates its operations; clock speed is measured in gigahertz (billions of cycles per second). Performance depends not only on clock speed but on how many instructions are completed per cycle, the number of cores, cache sizes, and the efficiency of the design.

Multicore and parallelism

Since the mid-2000s, rising heat and power made increasing clock speeds difficult, so manufacturers added multiple cores, independent processing units on one chip. Programs must be designed to divide work among cores to benefit. Graphics processing units (GPUs) contain thousands of simpler cores for highly parallel tasks, such as graphics and machine learning.

Pipelining

Pipelining overlaps the stages of different instructions, like an assembly line: while one instruction is executing, the next is being decoded and the one after is being fetched. This increases throughput. Branch prediction guesses which way decisions will go to keep the pipeline full.

Worked example

A simple program adds two numbers. The CPU fetches 'LOAD R1, 100' and copies the value at memory address 100 into register R1; fetches 'LOAD R2, 101' for the second number; fetches 'ADD R3, R1, R2', and the ALU adds them into R3; then fetches 'STORE R3, 102' to save the result. Each step is one pass of the fetch-decode-execute cycle.

Key takeaways

The von Neumann model has a CPU, shared memory for programs and data, I/O devices, and buses. The CPU contains the ALU, control unit, and registers, including the program counter. The fetch-decode-execute cycle runs programs. Instruction set architectures include x86-64 and ARM; assembly is readable machine code. Performance depends on clock speed, instructions per cycle, cores, and cache. Multicore CPUs and GPUs provide parallelism; pipelining overlaps instructions.

Flashcard

CPU

Central processing unit

Executes instructions.

Check your understanding

What does the program counter hold?

Take the unit quiz

← Previous lesson Next lesson →