How a processor that can only add multiplies: a jump that sends the program counter back, a conditional jump that leaves the loop in time, counting instructions and ticks, and the clock speed that turns ticks into seconds.
Step 1 of 7 · A program with a loop
Step 1 of 7
A program with a loop
The chip’s ALU has no multiply. To work out you add 3 four times, and rather than write the same instructions four times, you write them once and go back to them: a loop. This is the program now in memory:
| Address | Instruction | What it does |
|---|---|---|
| 0 | LDA 13 |
loads the total so far |
| 1 | LDB 14 |
loads the number to multiply |
| 2 | ADD |
adds it to the total |
| 3 | STA 13 |
stores the total |
| 4 | LDA 15 |
loads the rounds left |
| 5 | LDB 12 |
loads 1 |
| 6 | SUB |
takes away one round |
| 7 | STA 15 |
stores what is left |
| 8 | JZ 10 |
if no rounds are left, jumps to 10 |
| 9 | JMP 0 |
goes back to the start of the loop |
| 10 | HLT |
stops |
Cell 12 holds 1, cell 13 the total, which starts at zero, cell 14 the number to multiply, 3, and cell 15 the number of rounds, set by the Rounds Parameter.
A jump is an instruction that writes a new address into the program counter. Clock tick stands at 27, the fetch of JMP 0 at the end of the first round: the counter has already gone up to 10.
Step 2 of 7
A conditional jump
If the loop ended with only JMP 0, the program would go round for ever. The JZ 10 before it is a conditional jump: it jumps only when the zero flag is on, and otherwise does nothing, so the processor goes on to the next instruction. This is how a computer makes decisions: every “if” in a program comes down to a conditional jump.
Step 3 of 7
Multiplying by adding
One round is ten instructions, addresses 0 to 9, which is thirty ticks. Step Clock tick on in jumps of thirty, and watch cell 13 grow with each round.
Now the number to multiply is 6, Rounds is 4, the lid is closed, and the program has already halted.
Step 4 of 7
Counting instructions
How long a program runs depends not on how many instructions are written in it, but on how many the processor actually carries out. In a loop the same instructions run again every round.
Step 5 of 7
From ticks to seconds
A processor’s clock ticks at a steady rate, its frequency, measured in hertz: ticks a second. To find how long a program takes, count its ticks, , and divide by the frequency, :
The chip’s Clock speed is set to 1000 Hz. The board does not draw the clock; the rate only sets how long each tick lasts.
Step 6 of 7
A faster clock
There are two ways to cut the time: carry out fewer ticks, or speed up the clock. Here the program is fixed, which leaves speeding up.
Step 7 of 7
Real processors
The clock of a laptop’s processor ticks three or four billion times a second, 3 GHz to 4 GHz. But the clock speed alone does not say how much work gets done: you also need to know how many ticks each instruction takes.
Loading the interactive version…
Loading the interactive version…
Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.
Practice · Predict, then look
Address 9 holds JMP 0. On tick 27 it was fetched, and the program counter went up to 10. What will the counter hold after it executes, on tick 29?
Practice · Predict, then look
Address 9 holds JMP 0. On tick 27 it was fetched, and the program counter went up to 10. What will the counter hold after it executes, on tick 29?
Practice · Choose
Address 8 holds JZ 10, “jump to 10 if zero”. When does it jump?
Practice · Choose
Address 8 holds JZ 10, “jump to 10 if zero”. When does it jump?
Practice · Calculate
The number to multiply is now 6, and Rounds is 4. The lid is closed, and the program has already halted. What is in cell 13?
Practice · Calculate
The number to multiply is now 6, and Rounds is 4. The lid is closed, and the program has already halted. What is in cell 13?
Practice · Calculate
Rounds is 3. The program has eleven instructions, at addresses 0 to 10. How many instructions does the processor carry out from reset until it halts, the HLT included?
Practice · Calculate
Rounds is 3. The program has eleven instructions, at addresses 0 to 10. How many instructions does the processor carry out from reset until it halts, the HLT included?
Practice · Calculate
Rounds is 4, and Clock speed is 1000 Hz: a thousand ticks a second. How long does the whole program take, from reset until the HLT executes?
Practice · Calculate
Rounds is 4, and Clock speed is 1000 Hz: a thousand ticks a second. How long does the whole program take, from reset until the HLT executes?
Practice · Reach the target
Rounds is fixed at 4, so the program takes 120 ticks. Change Clock speed until the whole program takes 0.03 s, and press Check.
Practice · Reach the target
Rounds is fixed at 4, so the program takes 120 ticks. Change Clock speed until the whole program takes 0.03 s, and press Check.
Practice · Choose
A real processor has a 3 GHz clock: three billion ticks a second. If each of its instructions took three ticks, as on our chip, how many instructions would it carry out a second?
Practice · Choose
A real processor has a 3 GHz clock: three billion ticks a second. If each of its instructions took three ticks, as on our chip, how many instructions would it carry out a second?
Lesson complete. Every practice question here is done.