The processor and its memory.
At eleven o'clock on the morning of June 21, 1948, in a laboratory in Manchester, England, a machine assembled from parts supplied by Britain's wartime radar laboratory ran a program for the first time. A young engineer named Tom Kilburn had written the program, and it lived in the machine's own memory, as a pattern of electric charges on the face of a glass tube. Its task was to find the largest number that divides evenly into 262,144, and its method was to try one candidate after another. The answer arrived after fifty-two minutes and about three and a half million small steps.
That machine understood only a handful of instructions. The processor in a phone understands several hundred, and carries them out several million times faster. Remarkably little else has changed. Both machines do one thing over and over: fetch the next instruction from memory, work out what it asks for, do it, and go back for another.
This chapter builds a processor small enough to watch while it does that. Then it looks at the unit that stands between a processor and its memory, and gives every program a world of its own.
A cook with one recipe card
Go back to the cook of the last chapter, and take away everything clever. This cook follows a recipe card one line at a time, with a finger on the line that comes next. Each line asks for something small: fetch the jar from shelf 13, add what is on shelf 14, put the result on shelf 15. The cook has room in hand for exactly one thing at a time.
A processor is that cook, and it needs only three things of its own. It needs a place to hold the number it is working on. It needs a counter that says which line of the recipe comes next, which is the finger on the card. And it needs the circuits that do the arithmetic. Everything else lives in memory, which is a long row of numbered cells with one number in each, like a wall of mailboxes.
The recipe lives in those same mailboxes. An instruction is stored as a number, exactly as the data is, and the only thing that makes it an instruction is that the counter happens to point at it. The Manchester machine was the first to run this way, and the idea is what lets a computer load a new program without anyone rewiring it.
The machine below has sixteen cells of memory and understands seven instructions. Each instruction is one number: sixteen times the number of the operation, plus the address of the cell to use. Choose a program and press one step to move the machine forward by a quarter of an instruction at a time. The small arrow beside the memory marks the cell that the counter points to.
Every instruction takes the same four beats. The processor asks, by putting the counter on one set of wires. Memory answers, and the processor fetches the number that comes back. It decodes that number into an operation and an address. Then it carries out the operation, and the whole thing starts again. The beats are counted off by the clock of the last chapter, the way a drummer counts for a band.
| number | name | what the processor does |
|---|---|---|
| 0 | HALT | Stop. |
| 1 | LOAD | Take the number in a cell in hand. |
| 2 | STORE | Write the number in hand into a cell. |
| 3 | ADD | Add the number in a cell to the number in hand. |
| 4 | SUB | Take the number in a cell away from the number in hand. |
| 5 | JUMP | Set the counter to a cell, so the next instruction comes from there. |
| 6 | JUMPZ | Do the same, but only if the number in hand is zero. |
Run the third program, which multiplies six by seven. The machine has no instruction for multiplying. It adds six to a running total, takes one away from a count that started at seven, and goes around again until the count reaches zero. That takes fifty-nine instructions, and it shows the two things that give a processor its power. The first is the instruction that jumps only when a number is zero. With it a program can make a decision, and can repeat a piece of work until the work is finished. The second is speed. A recipe that needs fifty-nine steps to do one multiplication sounds like a poor bargain, until the cook is taking four billion steps a second.
Here is the whole machine as a program, which is all that a simulator like the one above really is.
/* A processor with seven instructions and sixteen cells of memory.
An instruction is one number: 16 * operation + address. */
#include <stdint.h>
enum { HALT, LOAD, STORE, ADD, SUB, JUMP, JUMPZ };
void run(uint8_t memory[16]) {
uint8_t counter = 0, in_hand = 0;
for (;;) {
uint8_t instruction = memory[counter]; /* ask and fetch */
counter = (counter + 1) & 15;
uint8_t operation = instruction >> 4; /* decode */
uint8_t address = instruction & 15;
switch (operation) { /* carry out */
case LOAD: in_hand = memory[address]; break;
case ADD: in_hand += memory[address]; break;
case SUB: in_hand -= memory[address]; break;
case STORE: memory[address] = in_hand; break;
case JUMP: counter = address; break;
case JUMPZ: if (in_hand == 0) counter = address; break;
default: return; /* HALT */
}
}
}# A processor with seven instructions and sixteen cells of memory.
# An instruction is one number: 16 * operation + address.
HALT, LOAD, STORE, ADD, SUB, JUMP, JUMPZ = range(7)
def run(memory):
counter, in_hand = 0, 0
while True:
instruction = memory[counter] # ask and fetch
counter = (counter + 1) % 16
operation, address = divmod(instruction, 16) # decode
if operation == LOAD: # carry out
in_hand = memory[address]
elif operation == ADD:
in_hand = (in_hand + memory[address]) % 256
elif operation == SUB:
in_hand = (in_hand - memory[address]) % 256
elif operation == STORE:
memory[address] = in_hand
elif operation == JUMP:
counter = address
elif operation == JUMPZ:
if in_hand == 0:
counter = address
else:
return # HALT
# six times seven: the answer, 42, ends up in cell 14
memory = [29, 104, 79, 45, 30, 60, 46, 80, 0, 0, 0, 0, 6, 7, 0, 1]
run(memory)
print(memory[14])A real processor differs from this one in size and in a number of clever shortcuts. It can hold a few dozen numbers in hand where this one holds a single number, and each of them is 64 bits wide, a bit being a single one or zero. It has hundreds of instructions. It works on several instructions at once, each at a different beat, like the stations of an assembly line. The loop at its heart is the one you just stepped through.
Two sets of wires
Look again at what passed between the processor and its memory in that drawing. There were only two kinds of traffic. On one set of wires the processor called out an address, the number of a mailbox. On the other set a number traveled, either back to the processor or out to be written down. Engineers call each set of wires a bus, so the first set is the address bus and the second is the data bus.
The width of a bus decides what it can say. Each wire carries one binary digit, a one or a zero, so four address wires can name sixteen different cells, which is all that the small machine has. Every wire added doubles the count. Thirty-two address wires reach a little over four billion cells, and that is exactly why the personal computers of the early 2000s stopped at four gigabytes of memory, however much their owners were willing to buy. Today's processors use addresses 64 bits wide.
This is also where the map of the last chapter comes from. The front-side bus was these two sets of wires, a few inches long, running from the processor to the north bridge. Every arrow drawn between two chips is a bundle of wires like these, and every number on them is a pattern of high and low voltages. The next chapter looks at one of those wires closely.
Shelves within reach
The stretched clock of chapter 24 exposed a problem. If one tick of the processor takes a second, every trip to memory takes seven minutes, and the small machine above goes to memory at least once in every instruction. A processor that waited seven minutes between one-second steps would spend nearly all its life standing still.
The answer is the one any cook arrives at, which is to keep the things in use on the counter. A processor carries small, very fast memories of its own, called caches, and keeps copies in them of the cells it has used most recently. It bets on two habits that nearly every program has. A cell used a moment ago will probably be used again soon, and so will its neighbors. So when the processor does have to make the long trip, it brings back the whole shelf, as a cook who needs the paprika takes down the entire spice rack.
| where a number is kept | how much fits | time to reach it | stretched |
|---|---|---|---|
| in the processor's hand (a register) | a few dozen numbers | within one tick | 1 second |
| the first shelf (level 1 cache) | tens of thousands | half a billionth | 2 seconds |
| the second shelf (level 2) | about a million | about 7 billionths | half a minute |
| the third shelf (level 3) | tens of millions | about 15 billionths | 1 minute |
| main memory | billions | about 100 billionths | 7 minutes |
The bet pays off remarkably well. In ordinary programs, more than nine trips in ten end at the first shelf. Caches are the reason that the order in which a program touches its data can matter more than how much arithmetic it does, and the reason that a processor with a larger cache can beat one with a faster clock.
Every program in a world of its own
The small machine ran one program and had the whole memory to itself. A real computer runs hundreds of programs at once, and that raises an awkward problem. If every program could read and write any cell it liked, one mistake in one program could scribble over all the others, and a dishonest program could read your passwords straight out of another program's cells.
The solution is one of the best ideas in computing, and it comes from Manchester again. In 1962 Tom Kilburn's team finished a much larger machine called Atlas, and with it came the trick that every computer now depends on. Each program is handed a notebook of its own, with the pages numbered from zero. The program believes that the notebook is the whole memory. The real sheets of paper are scattered through one large shared stack, and only one unit in the machine knows which real sheet is page 5 of which notebook. That unit is called the memory management unit, or MMU.
The MMU sits inside the processor, on the way out to the address wires. Every address that a program uses passes through it and is translated on the way, every single time. Memory is dealt with in pages, most often of 4,096 cells. The MMU swaps the page number in the address for the number of the real page, called a frame, and lets the rest of the address through unchanged. It finds the frame in a table that the operating system keeps for each program, with one row for each page.
Two programs are running in the drawing below, and each has eight pages. Press a page on the left to have the running program reach for an address on that page. Follow the line to see where the answer came from and which frame of real memory it leads to. Then switch to the other program and press the same page.
Three things fall out of this one arrangement, and you can find each of them in the drawing.
Privacy. Page 0 of program A and page 0 of program B are different frames. Each program uses the same addresses and never meets the other. A program has no way even to name a cell that belongs to someone else, because every address it can form is looked up in its own table. Press a page that program A was never given, and the MMU refuses. The operating system stops the program on the spot, and the damage goes no further.
More memory than there is. A row of the table can say that a page is out on the disk. Program A's page 2 starts that way. When the program reaches for it, the MMU raises its hand, the operating system pauses the program, pushes out a page that has gone unused for a while, reads the missing one in, and lets the program carry on as though nothing had happened. All the programs together can then use more memory than the machine has. The price is time. On the stretched clock that one reach took a week, which is why a computer with too many programs open slows to a crawl.
A short list, to keep it fast. Looking in a table means a trip to memory, and a translation that cost a trip to memory for every address would be ruinous. So the MMU keeps a short list of the answers it has found most recently. It is one more cache, and engineers call it the translation lookaside buffer. Press the same page twice, and the second answer comes straight off the list. Switch programs and the list is wiped, since its answers belonged to the other program's table.
Try it: watch your own machine
- Open the system monitor of your computer: Task Manager on Windows, Activity Monitor on a Mac. Count the programs in the list. There are usually a few hundred, and each one believes it has the memory to itself.
- Find the two memory figures: how much is installed, and how much is in use. Then open twenty tabs in a web browser and watch the second figure climb.
- Keep going until memory is nearly full. Look for a figure named swap, or page file. That is the drive being used as extra pages, and you will feel the machine slow down as it grows.
- Close the tabs, and step through the count-down program above with a pencil. Write the counter and the number in hand after each instruction, and check yourself against the machine.
Everything in the last two chapters was drawn as boxes and arrows, with tidy numbers traveling between them. On the board each of those numbers is a row of voltages on copper, and copper is a far less tidy place. The next chapter is about what a one and a zero really look like there.