How a computer runs a program
A program sits on the disk as a file of numbers called machine code. To run it, the computer copies it into memory, and the CPU works through it with one simple loop, again and again: fetch the next instruction, decode what it means, execute it, move on. Follow a four-instruction program that adds 2 and 3, one cycle at a time.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Why is a program copied into memory (RAM) before it runs?
The disk keeps files even with the power off, but it is slow. The CPU reads instructions from memory, which is far quicker, so the operating system copies the program there first.
02What does the program counter (PC) hold?
The program counter is a register that holds the address of the next instruction. Each cycle the CPU fetches from that address, then the PC moves on.
03What are the three steps of the cycle, in order?
Fetch the instruction from memory, decode what it means, execute it. Then the program counter moves on and the cycle repeats.
04With this lesson's instruction set, what does 123 do?
The first digit, 1, means store a number. The next digit picks the register, r2, and the last digit is the number to store, 3.
05A CPU runs at 2.4 GHz. What does that mean?
Hertz means times per second, and giga means a billion. A 2.4 GHz clock ticks 2,400,000,000 times every second, and each tick moves the CPU's work along.
Glossary
- Disk (SSD)
- Storage that keeps files even when the power is off. An SSD (solid-state drive) is a fast kind of disk, but still much slower than memory.
- Memory (RAM)
- A long row of numbered boxes that holds the programs and values the computer is using right now. It forgets everything when the power goes off.
- Address
- The number of a box in memory. The CPU asks for box 0, box 1, and so on.
- Machine code
- A program written as the numbers a CPU understands directly. Every program ends up as machine code before it runs.
- Instruction set
- The fixed list of instructions one kind of CPU understands, each with its own number code. x86-64 and Arm are two real ones.
- Register
- A tiny, very fast box inside the CPU that holds one number it is working with.
- Program counter (PC)
- The register that holds the address of the next instruction to run.
- Clock speed
- How many times a second the CPU's clock ticks, in hertz. 2.4 GHz is 2.4 billion ticks a second.
Notes and sources
- The instruction set here is made up for teaching, with instructions written as decimal digits. Real CPUs use binary codes. On an x86-64 PC, for example, the bytes B8 02 00 00 00 mean "put 2 in register eax" and 01 D8 means "add ebx to eax" (Intel Software Developer’s Manual, Vol. 2).
- Many real CPUs move the program counter on during the fetch, before executing. The result is the same: the next cycle reads the next address.
- One instruction does not always take exactly one tick. Modern CPUs overlap several instructions at once (pipelining) and have several cores, so they can finish more than one instruction per tick, or need several ticks for a slow one.
- Operating systems often load a program lazily, copying each piece from the disk only when it is first needed. The figure copies all four instructions at once.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 2 Introduction (fetch, decode, execute)
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 4 The Process (loading a program from disk)
- Raspberry Pi 5 product brief (Broadcom BCM2712, 2.4 GHz quad-core Arm Cortex-A76)
- Intel 64 and IA-32 Architectures Software Developer's Manuals (Vol. 2: MOV and ADD encodings)
- N. Nisan and S. Schocken, Nand2Tetris: The Elements of Computing Systems (the Hack CPU's fetch-execute loop)
- Python Language Reference: the while statement
Quiz Unlocks after the last step.