Computer Science › Section C · Data manipulation › Topic C.3
Program execution and the machine cycle
A CPU runs every program, from a calculator to a web browser, by repeating one short routine: fetch an instruction, decode it, carry it out. Tracing that routine by hand shows how two special registers keep a program on track.
Keeping your place in a program
Following a recipe, you keep a finger on the current step, read it, do it, and move to the next, unless the recipe says "if the dough is sticky, go back to step 3". A CPU does the same with two special-purpose registers. The program counter (PC) holds the address where the next fetch will happen. The instruction register (IR) holds the instruction currently being carried out. Instructions normally run in the order they are stored in memory; only a jump changes that order. This topic uses Sparrow, the teaching machine from "Machine language", whose instructions are two bytes long.
The machine cycle
The CPU repeats a three-step routine called the machine cycle. Fetch: the CPU asks memory for the instruction at the address in the PC. On Sparrow that means reading two consecutive cells, placing the 16-bit result in the IR, and then adding 2 to the PC so that it points at the following instruction. Decode: the CPU splits the IR into its op-code and operand fields and works out what is being asked. Execute: the CPU activates the circuits that do the job, such as reading memory, asking the ALU to add, or writing a register to memory. Then the cycle starts again with another fetch.
How a jump works
Because the PC was already advanced during the fetch, a normal instruction leaves the CPU ready for the next one in memory. A jump instruction works by overwriting the PC during its execute step. Sparrow's 0xE0XY always places XY in the PC, so the next fetch comes from address XY. The conditional instruction 0xDRXY first checks register R. If R holds 0, it places XY in the PC; if not, it does nothing and the program continues in order. A jump does not run the target instruction itself. It only changes which address the next fetch uses, and the machine cycle does the rest.
Worked example: a straight-line program
Sparrow's memory holds a program starting at 0x30: 0x2107, 0x2205, 0x5312, 0x3380, 0x0000. The PC is set to 0x30. 1. Fetch from 0x30: IR = 0x2107, PC = 0x32. Execute: R1 = 0x07. 2. Fetch from 0x32: IR = 0x2205, PC = 0x34. Execute: R2 = 0x05. 3. Fetch from 0x34: IR = 0x5312, PC = 0x36. Execute: R3 = R1 + R2 = 0x0C. 4. Fetch from 0x36: IR = 0x3380, PC = 0x38. Execute: cell 0x80 gets 0x0C. 5. Fetch from 0x38: IR = 0x0000, PC = 0x3A. Execute: halt. Check: 7 + 5 = 12 = 0x0C, and the PC rose by 2 at every fetch, so it ends at 0x3A, beyond the HALT.
Worked example: a loop
This program at 0x20 adds 5 + 4 + 3 + 2 + 1: 0x2105 (R1 = 5, a counter), 0x2200 (R2 = 0, the sum), 0x2301 (R3 = 1), then at 0x26 0xD12E (if R1 = 0 jump to 0x2E), 0x5221 (R2 = R2 + R1), 0x6113 (R1 = R1 − R3), 0xE026 (jump to 0x26), and at 0x2E 0x3250 (store R2 in cell 0x50), 0x0000. 1. Each pass adds the counter to the sum and lowers the counter by 1. 2. After five passes R1 is 0, so the test at 0x26 jumps to 0x2E. 3. Cell 0x50 receives 0x0F, which is 15. Check by counting: 3 set-up instructions, 5 passes of 4, then the final test, STORE and HALT make 26 machine cycles, and the test runs 6 times. A simulated run agrees.
Programs versus data
Memory holds programs and data side by side as plain bit patterns, and nothing marks which is which. Several programs can sit in memory at once; which one runs depends only on where the PC points. If the PC is set to the address of data by mistake, the CPU will fetch that data and obey it as instructions. Suppose cells 0x60 and 0x61 hold 0x35 and 0x10, meant as two numbers. Fetched as an instruction they read 0x3510: store R5 in cell 0x10. The CPU cannot tell that this was never intended as code. What happens next depends entirely on the data.
Why treating code as data is useful
Sharing one format is not a flaw; it is what lets programs handle other programs. A compiler writes machine instructions into memory as its output data. An operating system copies a program from storage into memory and then sets the PC to its first instruction, as the topic "Operating system architecture and booting" describes. A program could even change its own instructions while it runs. The same freedom carries a risk: an attacker who sneaks bytes into memory may try to make the CPU execute them. Modern processors and operating systems therefore let regions of memory be marked as data only, so the CPU refuses to fetch instructions from them.
The clock
The steps of the machine cycle are paced by the CPU's clock, a circuit called an oscillator that produces a steady stream of pulses. Clock rate is measured in hertz (Hz), pulses per second. A 3 GHz clock ticks 3 billion times per second, so one tick lasts a third of a nanosecond. A single instruction may need several ticks, while a modern processor can also finish more than one instruction per tick on average. So the number of instructions completed each second depends on both the clock rate and the average instructions per cycle, often shortened to IPC.
Comparing processors
Because different designs do different amounts of work per tick, clock rate alone is a poor way to compare processors. Suppose processor A runs at 3.0 GHz and averages 2 instructions per cycle, while processor B runs at 4.0 GHz and averages 1.2. A completes about \(3.0 \times 2 = 6.0\) billion instructions per second and B about \(4.0 \times 1.2 = 4.8\) billion, so the slower clock wins. Even this is a simplification, since one design's instruction may do more work than another's. The fair test is benchmarking: running the same programs, called benchmarks, on each machine and timing them, using programs that resemble the work you actually do.
Common mistakes to avoid
First, saying the PC holds the current instruction: it holds an address, and the IR holds the instruction. Second, adding 1 to the PC after a fetch on Sparrow; each instruction is two cells long, so add 2. Third, forgetting that the PC is advanced during the fetch, before the instruction executes, which is why a jump simply overwrites it. Fourth, assuming the CPU knows code from data; it obeys whatever the PC points at. Fifth, ranking processors by clock rate alone.
Key takeaways
The program counter holds the address of the next instruction; the instruction register holds the one being executed. The machine cycle is fetch, decode, execute, repeated until HALT. During the fetch, Sparrow reads two cells into the IR and adds 2 to the PC. A jump works by placing a new address in the PC during its execute step. Memory does not distinguish programs from data, which is useful but must be guarded. A clock paces the cycle; instructions per second depend on clock rate and instructions per cycle, and benchmarks give the fairest comparison.
Flashcards
Flashcard
Register holding the address of the next instruction
Test your understanding
Question 1 of 5
Sparrow fetches the instruction stored at addresses 0x4C and 0x4D. What does the program counter hold at the end of this fetch step?
Question 2 of 5
In the loop example, the first instruction 0x2105 is changed to 0x2103. What ends up in cell 0x50, and how many times does the test at 0x26 run?
Question 3 of 5
What happens during the execute step of the Sparrow instruction 0xE040?
Question 4 of 5
By mistake, the PC is set to the address of two cells holding the numbers 0x14 and 0x80. What does Sparrow do?
Question 5 of 5
Processor X runs at 2.5 GHz averaging 3 instructions per cycle; processor Y runs at 3.5 GHz averaging 2. Which completes more instructions per second?
