Answer key · Unit 3
How a computer runs a program.
Each lesson’s practice questions, their correct answers and the mistakes they spot. Answers read off the Scene use the same setting the learner sees.
Answer key 4
Lesson 1 of 4 · About 20 minutes
Memory and addresses
What is inside a computer before it does anything: numbered memory cells, a byte of eight bits, the addresses a four-wire bus can give, and a program that is nothing but numbers in memory.
The 5 steps and their answers
Practice · Choose
Question
The chip’s address bus is four lanes, and each lane carries one bit. How many different memory cells can it give an address to?
Correct answer
Correct
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
Choices
Shown in a random order.
Correct
Wrong , a cell for each lane
Misconception: Thinks each extra address line adds a fixed number of cells (four lines name 4 or 8 cells), instead of doubling them: 2⁴ = 16.
Feedback from the catalogue
Each extra line on the address bus doubles the number of addresses; it does not add to them. One line gives two addresses, 0 and 1; two lines give four,
00to11; and four lines give addresses, from0000to1111. One line per cell would take sixteen wires.Wrong , two for each lane
Misconception: Thinks each extra address line adds a fixed number of cells (four lines name 4 or 8 cells), instead of doubling them: 2⁴ = 16.
Feedback from the catalogue
Each extra line on the address bus doubles the number of addresses; it does not add to them. One line gives two addresses, 0 and 1; two lines give four,
00to11; and four lines give addresses, from0000to1111. One line per cell would take sixteen wires.Wrong
Misconception: Forgets that counting starts at 0: takes the largest address, 15, as the number of cells, or tick 11 as the eleventh tick.
Feedback from the catalogue
Computers count from zero. The addresses are 0 to 15, so there are sixteen cells, not fifteen. In the same way the first tick is tick 0, so by the end of tick 11 twelve ticks have run. When numbering starts at zero, the count is the last number plus one.
Feedback for a right answer
: each lane can be 0 or 1, and every extra lane doubles the number of patterns. Those are exactly the sixteen cells in the memory, addresses 0 to 15.
Hint
How many different patterns do two bits have? And three?
Worked solution
- Each lane carries one bit: 0 or 1.
- One lane has two options; each extra lane has two options for every earlier one, so the number of patterns doubles.
- Four lanes: addresses, from
0000to1111.
If you chose or , you added instead of multiplying. If you chose , that is the largest address, but counting starts at 0.
Practice · Calculate
Question
A memory cell, like each register on the chip, is one byte: eight bits. What is the largest number one byte can hold?
Correct answer
255 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
- Accepted within
- ± 0.01
- A fixed value in the question
Wrong answers it recognises
256 ± 0.01
Misconception: Takes the largest number n bits can hold to be 2ⁿ (256 for a byte) rather than 2ⁿ − 1 (255).
Feedback from the catalogue
Eight bits give different patterns, but one of them is zero,
00000000. So the numbers run from 0 to 255, and the largest is ,11111111. Writing 256 takes a ninth bit.
Feedback for a right answer
11111111is , which is . A byte has 256 different values, 0 to 255.Hint
Add the place values of all eight bits.
Worked solution
- Turn all eight bits on:
11111111. - The place values are .
- The sum: , that is .
If you answered , you counted the number of values, , rather than the largest of them: one of the values is zero.
Practice · Calculate
Question
The byte at address 0 is
0001 1110. The lid is closed. Read as a number rather than an instruction, what is this byte?Correct answer
30 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0fixed
- First number
- 28
- Second number
- 14
- Chip / Transistors
- Chipfixed
- Accepted within
- ± 0.01
- Computed from the Scene at this step’s setting
- The byte in the instruction register after this tick (opcode in the high four bits)
/nodes/cpu/outputs/instructionRegister
Wrong answers it recognises
14 ± 0.01
Misconception: Reads a byte as two separate numbers, its high and low four bits, instead of one eight-bit number.
Feedback from the catalogue
Read as a number, a byte is all eight bits together. The place values, right to left, are 1, 2, 4, 8, 16, 32, 64, 128. In
0001 1110the bits for 16, 8, 4 and 2 are on, so it is . Splitting it into two halves, an opcode and an address, is right only when the byte is read as an instruction.15 ± 0.01
Misconception: Reads a byte as two separate numbers, its high and low four bits, instead of one eight-bit number.
Feedback from the catalogue
Read as a number, a byte is all eight bits together. The place values, right to left, are 1, 2, 4, 8, 16, 32, 64, 128. In
0001 1110the bits for 16, 8, 4 and 2 are on, so it is . Splitting it into two halves, an opcode and an address, is right only when the byte is read as an instruction.
Feedback for a right answer
. The same byte, landing in the instruction register, is
LDA 14: the chip does not know which is which, only how the byte is used.Hint
The fifth bit from the right has place value .
Worked solution
- The place values of the eight bits, left to right: .
- In
0001 1110the bits for , , and are on. - The sum: .
If you answered , you read only the right four bits, the address. If you answered , you added the two halves, : as a number, the eight bits are one number.
Practice · Choose
Question
Address 3 holds the byte
0101 1101. Opcode0101isSTA, “store A”. What does the instruction do?Correct answer
Correct Writes what is in register A to cell 13
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
Choices
Shown in a random order.
Correct Writes what is in register A to cell 13
Wrong Writes what is in register A to cell 5
Feedback
0101, the left half, is the opcode. The address is the right half,1101.Wrong Writes the number 13 into register A
Misconception: Confuses an address, the number that names a memory cell, with the value stored in that cell.
Feedback from the catalogue
An address is a cell’s name, like a house number on a street; the contents are what lives there. The cell at address 14 holds the number 28.
LDA 14means “load A with what is in cell 14”, that is 28, not the number 14 itself.Wrong Loads register A with what is in cell 13
Feedback
Loading register A is opcode
0001,LDA.STAgoes the other way: from the register to memory.
Feedback for a right answer
1101is , so this isSTA 13: the sum in A is written to cell 13. It is the program’s fourth instruction.Worked solution
- The left half,
0101, is the opcode: ,STA. - The right half,
1101, is the address: . - Together:
STA 13, write A to cell 13.
The address in an instruction names a cell; it is not a number that goes into the register.
Practice · Predict, then look
Question
Cell 14 holds 28. Tick 2 carries out
LDA 14. What will be left in cell 14 after the tick?Correct answer
Correct Still 28
Setup, choices and feedback
The Scene at this step
- Clock tick
- 1fixed
- First number
- 28
- Second number
- 14
- Chip / Transistors
- Blocks
- Other settings
- Watch address
/nodes/cpu/props/watchAddress= 14
Choices
Correct Still 28
Wrong 0: the number moved into register A
Misconception: Thinks loading a number into a register takes it out of memory, leaving the cell empty.
Feedback from the catalogue
Loading copies. The memory puts the voltages of the cell’s bits on the data bus and the register latches them, but the cell itself does not change. After
LDA 14the number 28 is in register A and still in cell 14. Only a store,STA, changes a memory cell.Wrong 14
Misconception: Confuses an address, the number that names a memory cell, with the value stored in that cell.
Feedback from the catalogue
An address is a cell’s name, like a house number on a street; the contents are what lives there. The cell at address 14 holds the number 28.
LDA 14means “load A with what is in cell 14”, that is 28, not the number 14 itself.
Once the prediction is locked in, the Scene changes
- Clock tick
- 2fixed
- Chip / Transistors
- Blocks
The Scene then shows: 28 · The byte in memory cell watchAddress after this tick
/nodes/cpu/outputs/watchedA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
Still 28, and now register A holds 28 too. A load copies: the memory only sends its bits out on the data bus.
Worked solution
LDA 14sends address 14 to memory and raises the MEM read line.- The memory puts what is in cell 14, 28, on the data bus, and register A latches it.
- Reading does not change the cell, so cell 14 still holds 28.
Only
STA, with the MEM write line, changes a cell.
Lesson 2 of 4 · About 25 minutes
Fetch, decode, execute
The loop every processor repeats: fetch an instruction from memory, decode its opcode and carry it out, one clock tick for each step. The program counter, the instruction register, and the control unit that decides what moves where.
The 5 steps and their answers
Practice · Choose
Question
What happens on a decode tick?
Correct answer
Correct The control unit reads the opcode and picks which control lines to raise; no register changes
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
Choices
Shown in a random order.
Correct The control unit reads the opcode and picks which control lines to raise; no register changes
Wrong The ALU works out the answer, and register A latches it
Feedback
That happens on the execute tick. Decode only decides what execute will do.
Wrong The instruction comes from memory into the instruction register
Feedback
That is fetch, the tick before decode.
Wrong The program counter goes up by one
Misconception: Thinks the program counter holds the address of the instruction being carried out, rather than the next one to fetch.
Feedback from the catalogue
The program counter steps on during the fetch tick: in the same tick that the instruction lands in the instruction register, the counter goes up by one. So during decode and execute it already points at the next instruction. While
LDB 15from address 1 runs, the counter holds 2.
Feedback for a right answer
Decoding is wiring: the opcode’s four bits go into the control unit, and it raises a fixed set of lines, such as ALU out and A in for
ADD. Nothing in the registers changes.Worked solution
- Fetch: the instruction goes into the instruction register, and the counter goes up by one.
- Decode: the opcode passes to the control unit, which picks control lines. No value is stored.
- Execute: the lines open one path, and one block latches a value.
Step Clock tick from 6 to 7 and check: nothing in the registers changes.
Practice · Predict, then look
Question
Tick 2 carried out
LDA 14, which came from address 0. At tick 3 the processor fetches the instruction at address 1. What will the program counter hold at the end of tick 3?Correct answer
Correct 2
Setup, choices and feedback
The Scene at this step
- Clock tick
- 2fixed
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
Choices
Correct 2
Wrong 1, the address of the instruction fetched
Misconception: Thinks the program counter holds the address of the instruction being carried out, rather than the next one to fetch.
Feedback from the catalogue
The program counter steps on during the fetch tick: in the same tick that the instruction lands in the instruction register, the counter goes up by one. So during decode and execute it already points at the next instruction. While
LDB 15from address 1 runs, the counter holds 2.Wrong 3, the tick number
Feedback
The counter counts places in memory, not ticks. It goes up by one only on a fetch tick, once every three ticks.
Once the prediction is locked in, the Scene changes
- Clock tick
- 3fixed
- Chip / Transistors
- Blocks
The Scene then shows: 2 · The program counter after this tick: the address of the next instruction to fetch
/nodes/cpu/outputs/programCounterA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
- On the fetch tick the counter sends 1 to memory and at once steps on to 2. While
LDB 15is decoded and carried out, the counter already points at the next instruction.
Worked solution
- At the start of tick 3 the counter holds 1, the address of the next instruction.
- Fetch: address 1 goes out on the address bus, and the byte there,
LDB 15, lands in the instruction register. - In the same tick the counter goes up by one: .
If you chose 1, you took the counter to hold the instruction running now; it is always one step ahead of it.
Practice · Reach the target
Question
Move Clock tick to the tick on which the control unit decodes
ADD, the instruction at address 2, and press Check.Goal
3 ± 0.01
At the start of the step: 1
1 ± 0.01
At the start of the step: 0
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
- Read from the Scene at
- The high four bits of the instruction register: 1 LDA, 2 LDB, 3 ADD, 4 SUB, 5 STA, 6 JMP, 7 JZ, 15 HLT
/nodes/cpu/outputs/opcode - Read from the Scene at
- What this tick does: 0 fetch, 1 decode, 2 execute, 3 halted
/nodes/cpu/outputs/phase
One setting that reaches it
- Clock tick
- 7
- First number
- 0
- Second number
- 0
Feedback for a right answer
Tick 7: the third instruction starts at tick 6 with its fetch, and 7 is its decode. The control unit shows
ADDand raises ALU out and A in.Feedback for a wrong answer
Not yet. Find the fetch of
ADDand go one tick on.Hint
The instruction at address starts at tick .
Worked solution
- Each instruction takes three ticks, and the first starts at tick 0.
ADDat address 2 is the third instruction, so it starts at tick .- Fetch at tick 6, decode at tick 7, execute at tick 8.
At tick 6 the instruction is already in the instruction register, but that is still the fetch.
Practice · Calculate
Question
The processor starts from reset at tick 0. The fourth instruction,
STA 13, writes the sum to cell 13. How many clock ticks run, from the first up to the one on which the sum is written, both included?Correct answer
12 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Blocks
- Accepted within
- ± 0.01
- A fixed value in the question
Wrong answers it recognises
11 ± 0.01
Misconception: Forgets that counting starts at 0: takes the largest address, 15, as the number of cells, or tick 11 as the eleventh tick.
Feedback from the catalogue
Computers count from zero. The addresses are 0 to 15, so there are sixteen cells, not fifteen. In the same way the first tick is tick 0, so by the end of tick 11 twelve ticks have run. When numbering starts at zero, the count is the last number plus one.
4 ± 0.01
Misconception: Takes one clock tick to run one whole instruction, forgetting the separate fetch, decode and execute ticks.
Feedback from the catalogue
On this chip every instruction takes three ticks: fetch, decode and execute. Four instructions are twelve ticks, and a clock of 1000 ticks a second runs only about 333 instructions a second. In real processors too one instruction usually takes several ticks, though they overlap instructions with each other.
Feedback for a right answer
Four instructions times three ticks: . The write happens on tick 11, the twelfth tick, since counting starts at 0.
Hint
How many instructions run up to the write, and how many ticks does each take?
Worked solution
STA 13is the fourth instruction:LDA,LDB,ADD,STA.- Each instruction takes three ticks: fetch, decode, execute.
- ticks: ticks 0 to 11.
If you answered , you read the number of the last tick; starting from 0, there is one more. If you answered , you counted one tick per instruction.
Practice · Calculate
Question
Clock tick stands at 9, and the lid is closed. How many instructions have finished completely, their execute included, by the end of this tick?
Correct answer
3 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 9fixed
- First number
- 28default
- Second number
- 14default
- Chip / Transistors
- Chipfixed
- Accepted within
- ± 0.01
- Computed from the Scene at this step’s setting
- Instructions executed once this tick ends (three ticks each; none after HLT)
/nodes/cpu/outputs/instructions
Wrong answers it recognises
10 ± 0.01
Misconception: Takes one clock tick to run one whole instruction, forgetting the separate fetch, decode and execute ticks.
Feedback from the catalogue
On this chip every instruction takes three ticks: fetch, decode and execute. Four instructions are twelve ticks, and a clock of 1000 ticks a second runs only about 333 instructions a second. In real processors too one instruction usually takes several ticks, though they overlap instructions with each other.
4 ± 0.01
Misconception: Takes one clock tick to run one whole instruction, forgetting the separate fetch, decode and execute ticks.
Feedback
The fourth instruction was only fetched on tick 9; its decode and execute are still to come.
Feedback for a right answer
Three. Ticks 0 to 8 are three whole instructions, and tick 9 is only the fetch of the fourth,
STA 13.Worked solution
- By the end of tick 9, ticks have run.
- Each instruction takes three: .
- Three instructions are done, and the tick left over is the fourth one’s fetch.
If you answered , you counted one tick per instruction. If you answered , you also counted the instruction that has only been fetched.
Lesson 3 of 4 · About 25 minutes
Registers and the ALU
Where the processor actually computes: two eight-bit registers, an ALU that adds and subtracts, a sum over 255 that wraps round past zero, a subtraction that goes below zero, and flags that remember what happened.
The 6 steps and their answers
Practice · Predict, then look
Question
First number is 100 and Second number is 50, and both are already loaded into the registers. What will register A hold after
ADDexecutes, on tick 8?Correct answer
150 ± 0.01
A number prediction counts as right within ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 5fixed
- First number
- 100fixed
- Second number
- 50fixed
- Chip / Transistors
- Blocks
Once the prediction is locked in, the Scene changes
- Clock tick
- 8fixed
- Chip / Transistors
- Blocks
The Scene then shows: 150 · Register A after this tick
/nodes/cpu/outputs/registerAA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
- The ALU adds A and B, and the sum comes back along the data bus into A, in place of the first number.
Worked solution
- After tick 5: A holds 100, and B holds 50.
- On the execute of
ADDthe ALU works out . - The control lines ALU out and A in send the answer to A, so A holds 150.
150 is less than 255, so it fits in eight bits.
Practice · Predict, then look
Question
Now First number is 200 and Second number is 100. What will register A hold after the
ADD?Correct answer
Correct 44
Setup, choices and feedback
The Scene at this step
- Clock tick
- 5fixed
- First number
- 200fixed
- Second number
- 100fixed
- Chip / Transistors
- Blocks
Choices
Correct 44
Wrong 300
Misconception: Thinks an eight-bit register can hold any whole number, so 200 + 100 gives 300.
Feedback from the catalogue
A register has exactly eight bits, so it holds only 0 to 255. 300 needs nine bits,
1 0010 1100. The ninth bit does not fit in A: it lights the carry flag, and the register keeps the lower eight bits, .Wrong 255, the largest that fits
Misconception: Thinks a sum too big for eight bits stops at the largest value, 255, instead of wrapping round past zero.
Feedback from the catalogue
The adder does not know there is a ceiling: it adds bit by bit, and the carry out of the top bit simply does not go into the register. So the sum wraps round past zero, like the odometer of an old car: 255 + 1 gives 0, and 250 + 10 gives . The carry flag marks that it happened.
Wrong 0, because the sum fails
Feedback
Nothing fails: the adder always puts out eight bits. It just loses the ninth.
Once the prediction is locked in, the Scene changes
- Clock tick
- 8fixed
- Chip / Transistors
- Blocks
The Scene then shows: 44 · Register A after this tick
/nodes/cpu/outputs/registerAA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
44, and the carry flag lights. , which is
1 0010 1100: nine bits. A keeps only the lower eight, .Worked solution
- The true sum: .
- Eight bits only go up to 255; 300 needs a ninth bit, worth 256.
- The ninth bit goes out to the carry flag, and the register keeps .
The sum does not stick at 255: it wraps round past zero.
Practice · Calculate
Question
First number is 250 and Second number is 10. The lid is closed, and Clock tick stands at 8, the execute of
ADD. What is in register A?Correct answer
4 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 8fixed
- First number
- 250fixed
- Second number
- 10fixed
- Chip / Transistors
- Chipfixed
- Accepted within
- ± 0.01
- Computed from the Scene at this step’s setting
- Register A after this tick
/nodes/cpu/outputs/registerA
Wrong answers it recognises
260 ± 0.01
Misconception: Thinks an eight-bit register can hold any whole number, so 200 + 100 gives 300.
Feedback from the catalogue
A register has exactly eight bits, so it holds only 0 to 255. 300 needs nine bits,
1 0010 1100. The ninth bit does not fit in A: it lights the carry flag, and the register keeps the lower eight bits, .255 ± 0.01
Misconception: Thinks a sum too big for eight bits stops at the largest value, 255, instead of wrapping round past zero.
Feedback from the catalogue
The adder does not know there is a ceiling: it adds bit by bit, and the carry out of the top bit simply does not go into the register. So the sum wraps round past zero, like the odometer of an old car: 255 + 1 gives 0, and 250 + 10 gives . The carry flag marks that it happened.
Feedback for a right answer
, and in eight bits . The carry flag lights, marking the 256 that did not fit.
Hint
How far over 255 is the sum?
Worked solution
- The true sum: .
- is more than 255, so the ninth bit, worth 256, drops off.
- Register A keeps .
If you answered , you forgot that the register has only eight bits. If you answered , you stopped at the ceiling; the adder wraps past it.
Practice · Predict, then look
Question
A different program is in memory: address 2 now holds
SUB, which takes B from A. First number is 90 and Second number is 25. What will A hold after tick 8?Correct answer
65 ± 0.01
A number prediction counts as right within ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 5fixed
- First number
- 90fixed
- Second number
- 25fixed
- Chip / Transistors
- Blocks
- Other settings
- Memory
/nodes/cpu/props/memory= [30,47,64,93,100,0,0,0,0,0,0,0,0,0,9,4]
Once the prediction is locked in, the Scene changes
- Clock tick
- 8fixed
- Chip / Transistors
- Blocks
The Scene then shows: 65 · Register A after this tick
/nodes/cpu/outputs/registerAA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
- The ALU subtracts with the same adder: it flips B’s bits, adds 1, and adds. So , and in eight bits .
Worked solution
- A holds 90, and B holds 25.
SUBworks out .- The answer goes back into A.
Inside the ALU it is an addition: , and the ninth bit drops off. That is why the carry flag lights here: it means no borrow was needed.
Practice · Calculate
Question
The same subtracting program. First number is 5 and Second number is 8. The lid is closed, and Clock tick stands at 8. What is in register A?
Correct answer
253 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 8fixed
- First number
- 5fixed
- Second number
- 8fixed
- Chip / Transistors
- Chipfixed
- Other settings
- Memory
/nodes/cpu/props/memory= [30,47,64,93,100,0,0,0,0,0,0,0,0,0,9,4]
- Accepted within
- ± 0.01
- Computed from the Scene at this step’s setting
- Register A after this tick
/nodes/cpu/outputs/registerA
Wrong answers it recognises
-3 ± 0.01
Misconception: Expects a register to store a negative result with a minus sign, as −3.
Feedback from the catalogue
A register has only eight bits and no room for a minus sign. Taking a bigger number from a smaller one wraps backwards past zero: gives ,
1111 1101. Computers read such a pattern as −3 when they agree that the top bit is a sign (two’s complement), but what the chip holds is 253.3 ± 0.01
Misconception: Expects a register to store a negative result with a minus sign, as −3.
Feedback
SUBalways works out , here , not the positive difference.
Feedback for a right answer
253,
1111 1101. goes three steps below zero and wraps backwards: .Hint
One step below zero, in eight bits, is 255.
Worked solution
- Ordinary arithmetic: .
- A register has no minus sign, and eight bits wrap like an odometer: one step below 0 is 255.
- Three steps below zero: .
If you answered , you expected a minus sign in the register. If you answered , you took the smaller from the larger;
SUBalways works out .Practice · Predict, then look
Question
In the subtracting program, First number and Second number are both 37. What will happen when
SUBexecutes?Correct answer
Correct A will hold 0, and the zero flag will light
Setup, choices and feedback
The Scene at this step
- Clock tick
- 5fixed
- First number
- 37fixed
- Second number
- 37fixed
- Chip / Transistors
- Blocks
- Other settings
- Memory
/nodes/cpu/props/memory= [30,47,64,93,100,0,0,0,0,0,0,0,0,0,9,4]
Choices
Correct A will hold 0, and the zero flag will light
Wrong A will hold 0, and no flag will light
Feedback
The zero flag lights exactly when the ALU’s answer is 0. That is what it is for: so that another instruction can ask about it.
Wrong A will hold 74
Feedback
is addition. Here address 2 holds
SUB.
Once the prediction is locked in, the Scene changes
- Clock tick
- 8fixed
- Chip / Transistors
- Blocks
The Scene then shows: 1 · 1 when the last ADD or SUB gave 0, else 0
/nodes/cpu/outputs/zeroFlagA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
: A holds 0, and the zero flag lights. In the next Lesson a jump instruction uses this flag to decide whether to leave a loop.
Worked solution
SUBworks out .- A latches 0.
- Every
ADDorSUBalso sets the flags: the answer is 0, so the zero flag lights.
Lesson 4 of 4 · About 30 minutes
Jumps, loops and clock speed
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.
The 7 steps and their answers
Practice · Predict, then look
Question
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?Correct answer
0 ± 0.01
A number prediction counts as right within ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 27fixed
- Rounds
- 4
- Clock speed
- 1000 Hz
- Chip / Transistors
- Blocks
Once the prediction is locked in, the Scene changes
- Clock tick
- 29fixed
- Chip / Transistors
- Blocks
The Scene then shows: 0 · The program counter after this tick: the address of the next instruction to fetch
/nodes/cpu/outputs/programCounterA prediction is never marked as a failure: the learner sees the outcome and the explanation either way.
Feedback for a right answer
- A jump does no arithmetic: it writes the address in the instruction straight into the program counter. The next fetch, on tick 30, brings
LDA 13from the top of the program again.
Worked solution
- After the fetch on tick 27 the counter holds 10.
- On the execute of
JMP 0the address 0 passes from the instruction register into the counter, in place of 10. - So after tick 29 the counter holds 0, and the next round begins.
Practice · Choose
Question
Address 8 holds
JZ 10, “jump to 10 if zero”. When does it jump?Correct answer
Correct When the zero flag is on: the last
SUBbrought the rounds left down to zero
Setup, choices and feedback
The Scene at this step
- Clock tick
- 26
- Rounds
- 4
- Clock speed
- 1000 Hz
- Chip / Transistors
- Blocks
Choices
Shown in a random order.
Correct When the zero flag is on: the last
SUBbrought the rounds left down to zeroWrong Always, like
JMPFeedback
Then the program would skip to
HLTin the first round.JZis a conditional jump.Wrong When cell 10 holds 0
Misconception: Confuses an address, the number that names a memory cell, with the value stored in that cell.
Feedback from the catalogue
An address is a cell’s name, like a house number on a street; the contents are what lives there. The cell at address 14 holds the number 28.
LDA 14means “load A with what is in cell 14”, that is 28, not the number 14 itself.Wrong Never: the
JMP 0after it always goes back upMisconception: Reads a program top to bottom once, as if a jump back did not make the same instructions run again.
Feedback from the catalogue
A jump puts an earlier address back in the program counter, and the processor simply carries on fetching from there: the same instructions run again, round after round, until a conditional jump leaves the loop. In the multiply program each round adds the number to the total once, so four rounds of 6 give 24, not 6.
Feedback for a right answer
JZchecks the zero flag, which the round’sSUBset. While rounds are left the flag is off, and the processor goes on toJMP 0. When the count reaches 0 the flag lights, and the jump to 10 leaves the loop forHLT.Worked solution
- At addresses 4 to 7 the program loads the rounds left, takes away 1, and stores it.
- The
SUBlights the zero flag only when the answer is 0. JZ 10jumps only when the flag is on; otherwise the counter goes on as usual to 9.
The address 10 in the instruction is where to jump, not a cell to test.
Practice · Calculate
Question
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?
Correct answer
24 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 239fixed
- Rounds
- 4fixed
- Clock speed
- 1000 Hz
- Chip / Transistors
- Chipfixed
- Other settings
- Cell14
/nodes/cpu/props/cell14= 6
- Accepted within
- ± 0.01
- Computed from the Scene at this step’s setting
- The byte in memory cell watchAddress after this tick
/nodes/cpu/outputs/watched
Wrong answers it recognises
6 ± 0.01
Misconception: Reads a program top to bottom once, as if a jump back did not make the same instructions run again.
Feedback from the catalogue
A jump puts an earlier address back in the program counter, and the processor simply carries on fetching from there: the same instructions run again, round after round, until a conditional jump leaves the loop. In the multiply program each round adds the number to the total once, so four rounds of 6 give 24, not 6.
10 ± 0.01
Misconception: Reads a program top to bottom once, as if a jump back did not make the same instructions run again.
Feedback
adds the two numbers. The loop adds 6 again and again, once per round.
Feedback for a right answer
- Each round the program adds 6 to cell 13: 6, 12, 18, 24. That is how a processor that can only add multiplies: is 6 four times.
Hint
How much does each round add to cell 13, and how many rounds are there?
Worked solution
- At the start cell 13 holds 0.
- Each round, at addresses 0 to 3, loads the cell, adds 6, and stores it back.
- The loop runs four rounds, so .
If you answered , you read the program once from top to bottom;
JMP 0sends it back to the start three more times.Practice · Calculate
Question
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
HLTincluded?Correct answer
30 ± 0.01
Setup, choices and feedback
The Scene at this step
- Clock tick
- 239fixed
- Rounds
- 3fixed
- Clock speed
- 1000 Hz
- Chip / Transistors
- Chipfixed
- Accepted within
- ± 0.01
- Computed from the Scene at this step’s setting
- Instructions executed once this tick ends (three ticks each; none after HLT)
/nodes/cpu/outputs/instructions
Wrong answers it recognises
11 ± 0.01
Misconception: Reads a program top to bottom once, as if a jump back did not make the same instructions run again.
Feedback from the catalogue
A jump puts an earlier address back in the program counter, and the processor simply carries on fetching from there: the same instructions run again, round after round, until a conditional jump leaves the loop. In the multiply program each round adds the number to the total once, so four rounds of 6 give 24, not 6.
33 ± 0.01
Misconception: Reads a program top to bottom once, as if a jump back did not make the same instructions run again.
Feedback
HLTruns only once, at the end, and every round but the last runs ten instructions.
Feedback for a right answer
- Each round carries out the ten instructions at addresses 0 to 9, except the last, which jumps from
JZstraight toHLT: .
Hint
How many instructions run in one round, and what is different in the last?
Worked solution
- An ordinary round: ten instructions, addresses 0 to 9,
JMP 0included. - In the last round
JZ 10jumps, soJMP 0does not run: nine instructions, thenHLT. - Three rounds: , that is ten instructions a round.
If you answered , you counted the instructions in memory, as if each ran once. If you answered , you counted all eleven instructions in every round.
Practice · Calculate
Question
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
HLTexecutes?Correct answer
0.12 s ± 1%
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- Rounds
- 4fixed
- Clock speed
- 1000 Hzfixed
- Chip / Transistors
- Blocks
- Accepted within
- ± 1%
- Computed from the Scene at this step’s setting
- How long the whole program takes at clockHz: ticksToHalt / clockHz
/nodes/cpu/outputs/timeToHalt
Any unit of the same quantity is accepted; it is converted before checking.
Wrong answers it recognises
0.04 s ± 1%
Misconception: Takes the clock rate as the number of instructions run per second, forgetting that an instruction takes several ticks.
Feedback from the catalogue
The clock rate counts ticks a second, not instructions. Here each instruction takes three ticks, so the time is the number of ticks divided by the clock rate: 40 instructions are 120 ticks, and on a 1000 Hz clock they take 0.12 s, not 0.04 s.
Feedback for a right answer
instructions times ticks are ticks, and : that is 0.12 s, or 120 ms.
Hint
How many ticks does the program take? Divide by the ticks per second.
Worked solution
- Four rounds of ten instructions: instructions, the
HLTincluded. - Each instruction is three ticks: ticks.
- The time is the ticks divided by the clock speed: .
If you answered 0.04 s, you divided instructions, not ticks, by the clock speed.
Practice · Reach the target
Question
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.
Goal
0.03 s ± 0.0005 s
At the start of the step: 0.12 s
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0fixed
- Rounds
- 4fixed
- Clock speed
- 1000 Hz
- Chip / Transistors
- Blocks
- Read from the Scene at
- How long the whole program takes at clockHz: ticksToHalt / clockHz
/nodes/cpu/outputs/timeToHalt
One setting that reaches it
- Clock speed
- 4000 Hz
Feedback for a right answer
4000 Hz: . A clock four times as fast runs the same 120 ticks in a quarter of the time. The program has not changed, only the pace.
Feedback for a wrong answer
Not yet. Divide the 120 ticks by 0.03 s.
Hint
By how many times must the time shrink?
Worked solution
- The time: , with the number of ticks, so .
- Substitute: .
- Set Clock speed to 4000 Hz.
At 1000 Hz the program takes 0.12 s: four times as long.
Practice · Choose
Question
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?
Correct answer
Correct A billion
Setup, choices and feedback
The Scene at this step
- Clock tick
- 0
- Rounds
- 4
- Clock speed
- 1000 Hz
- Chip / Transistors
- Blocks
Choices
Shown in a random order.
Correct A billion
Wrong Three billion
Misconception: Takes the clock rate as the number of instructions run per second, forgetting that an instruction takes several ticks.
Feedback from the catalogue
The clock rate counts ticks a second, not instructions. Here each instruction takes three ticks, so the time is the number of ticks divided by the clock rate: 40 instructions are 120 ticks, and on a 1000 Hz clock they take 0.12 s, not 0.04 s.
Wrong Nine billion
Feedback
Three ticks per instruction means fewer instructions than ticks, not more: divide by three, do not multiply.
Feedback for a right answer
: a billion instructions a second. Real processors manage more, because they overlap instructions and run several at once, but the clock still counts ticks, not instructions.
Worked solution
- 3 GHz is ticks a second.
- Each instruction takes three ticks.
- Instructions a second: , a billion.
If you chose three billion, you assumed that every tick finishes an instruction.
Across the unit · 13 misconceptions
Misconception catalogue
Each common mistake this unit recognises, in a few words. Open one for what the learner reads when an answer matches it, and where it is asked.
Thinks each extra address line adds a fixed number of cells (four lines name 4 or 8 cells), instead of doubling them: 2⁴ = 16.
- The learner reads
Each extra line on the address bus doubles the number of addresses; it does not add to them. One line gives two addresses, 0 and 1; two lines give four,
00to11; and four lines give addresses, from0000to1111. One line per cell would take sixteen wires.- Recognised in
Forgets that counting starts at 0: takes the largest address, 15, as the number of cells, or tick 11 as the eleventh tick.
- The learner reads
Computers count from zero. The addresses are 0 to 15, so there are sixteen cells, not fifteen. In the same way the first tick is tick 0, so by the end of tick 11 twelve ticks have run. When numbering starts at zero, the count is the last number plus one.
Takes the largest number n bits can hold to be 2ⁿ (256 for a byte) rather than 2ⁿ − 1 (255).
- The learner reads
Eight bits give different patterns, but one of them is zero,
00000000. So the numbers run from 0 to 255, and the largest is ,11111111. Writing 256 takes a ninth bit.- Recognised in
Reads a byte as two separate numbers, its high and low four bits, instead of one eight-bit number.
- The learner reads
Read as a number, a byte is all eight bits together. The place values, right to left, are 1, 2, 4, 8, 16, 32, 64, 128. In
0001 1110the bits for 16, 8, 4 and 2 are on, so it is . Splitting it into two halves, an opcode and an address, is right only when the byte is read as an instruction.- Recognised in
Confuses an address, the number that names a memory cell, with the value stored in that cell.
- The learner reads
An address is a cell’s name, like a house number on a street; the contents are what lives there. The cell at address 14 holds the number 28.
LDA 14means “load A with what is in cell 14”, that is 28, not the number 14 itself.
Thinks loading a number into a register takes it out of memory, leaving the cell empty.
- The learner reads
Loading copies. The memory puts the voltages of the cell’s bits on the data bus and the register latches them, but the cell itself does not change. After
LDA 14the number 28 is in register A and still in cell 14. Only a store,STA, changes a memory cell.- Recognised in
Thinks the program counter holds the address of the instruction being carried out, rather than the next one to fetch.
- The learner reads
The program counter steps on during the fetch tick: in the same tick that the instruction lands in the instruction register, the counter goes up by one. So during decode and execute it already points at the next instruction. While
LDB 15from address 1 runs, the counter holds 2.
Takes one clock tick to run one whole instruction, forgetting the separate fetch, decode and execute ticks.
- The learner reads
On this chip every instruction takes three ticks: fetch, decode and execute. Four instructions are twelve ticks, and a clock of 1000 ticks a second runs only about 333 instructions a second. In real processors too one instruction usually takes several ticks, though they overlap instructions with each other.
Thinks an eight-bit register can hold any whole number, so 200 + 100 gives 300.
- The learner reads
A register has exactly eight bits, so it holds only 0 to 255. 300 needs nine bits,
1 0010 1100. The ninth bit does not fit in A: it lights the carry flag, and the register keeps the lower eight bits, .
Thinks a sum too big for eight bits stops at the largest value, 255, instead of wrapping round past zero.
- The learner reads
The adder does not know there is a ceiling: it adds bit by bit, and the carry out of the top bit simply does not go into the register. So the sum wraps round past zero, like the odometer of an old car: 255 + 1 gives 0, and 250 + 10 gives . The carry flag marks that it happened.
Expects a register to store a negative result with a minus sign, as −3.
- The learner reads
A register has only eight bits and no room for a minus sign. Taking a bigger number from a smaller one wraps backwards past zero: gives ,
1111 1101. Computers read such a pattern as −3 when they agree that the top bit is a sign (two’s complement), but what the chip holds is 253.- Recognised in
Reads a program top to bottom once, as if a jump back did not make the same instructions run again.
- The learner reads
A jump puts an earlier address back in the program counter, and the processor simply carries on fetching from there: the same instructions run again, round after round, until a conditional jump leaves the loop. In the multiply program each round adds the number to the total once, so four rounds of 6 give 24, not 6.
Takes the clock rate as the number of instructions run per second, forgetting that an instruction takes several ticks.
- The learner reads
The clock rate counts ticks a second, not instructions. Here each instruction takes three ticks, so the time is the number of ticks divided by the clock rate: 40 instructions are 120 ticks, and on a 1000 Hz clock they take 0.12 s, not 0.04 s.