ViewWiki

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.

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.

Open the lesson

The 5 steps and their answers
  1. Step 1 of 5

    Open this step

    Sixteen cells

    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 1616⁠

    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.

    Feedback for a right answer

    24=162^4 = 16⁠: 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

    1. Each lane carries one bit: 0 or 1.
    2. One lane has two options; each extra lane has two options for every earlier one, so the number of patterns doubles.
    3. Four lanes: 2×2×2×2=24=162 \times 2 \times 2 \times 2 = 2^4 = 16⁠ addresses, from 0000 to 1111.

    If you chose 44⁠ or 88⁠, you added instead of multiplying. If you chose 1515⁠, that is the largest address, but counting starts at 0.

  2. Step 2 of 5

    Open this step

    One byte

    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

    Feedback for a right answer

    11111111 is 128+64+32+16+8+4+2+1=255128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255⁠, which is 28−12^8 - 1⁠. A byte has 256 different values, 0 to 255.

    Hint

    Add the place values of all eight bits.

    Worked solution

    1. Turn all eight bits on: 11111111.
    2. The place values are 1,2,4,…,1281, 2, 4, \ldots, 128⁠.
    3. The sum: 1+2+4+8+16+32+64+128=2551 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255⁠, that is 28−12^8 - 1⁠.

    If you answered 256256⁠, you counted the number of values, 282^8⁠, rather than the largest of them: one of the values is zero.

  3. Step 3 of 5

    Open this step

    A byte is a number

    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

    Feedback for a right answer

    16+8+4+2=3016 + 8 + 4 + 2 = 30⁠. 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 1616⁠.

    Worked solution

    1. The place values of the eight bits, left to right: 128,64,32,16,8,4,2,1128, 64, 32, 16, 8, 4, 2, 1⁠.
    2. In 0001 1110 the bits for 1616⁠, 88⁠, 44⁠ and 22⁠ are on.
    3. The sum: 16+8+4+2=3016 + 8 + 4 + 2 = 30⁠.

    If you answered 1414⁠, you read only the right four bits, the address. If you answered 1515⁠, you added the two halves, 1+141 + 14⁠: as a number, the eight bits are one number.

  4. Step 4 of 5

    Open this step

    Opcode and address

    Practice · Choose

    Question

    Address 3 holds the byte 0101 1101. Opcode 0101 is STA, “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 14 means “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. STA goes the other way: from the register to memory.

    Feedback for a right answer

    1101 is 8+4+1=138 + 4 + 1 = 13⁠, so this is STA 13: the sum in A is written to cell 13. It is the program’s fourth instruction.

    Worked solution

    1. The left half, 0101, is the opcode: 4+1=54 + 1 = 5⁠, STA.
    2. The right half, 1101, is the address: 8+4+1=138 + 4 + 1 = 13⁠.
    3. 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.

  5. Step 5 of 5

    Open this step

    Copy, don't move

    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

    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/watched

    A 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

    1. LDA 14 sends address 14 to memory and raises the MEM read line.
    2. The memory puts what is in cell 14, 28, on the data bus, and register A latches it.
    3. 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.

Open the lesson

The 5 steps and their answers
  1. Step 1 of 5

    Open this step

    One loop

    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 15 from 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

    1. Fetch: the instruction goes into the instruction register, and the counter goes up by one.
    2. Decode: the opcode passes to the control unit, which picks control lines. No value is stored.
    3. 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.

  2. Step 2 of 5

    Open this step

    The program counter

    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 15 from 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/programCounter

    A prediction is never marked as a failure: the learner sees the outcome and the explanation either way.

    Feedback for a right answer

    1. On the fetch tick the counter sends 1 to memory and at once steps on to 2. While LDB 15 is decoded and carried out, the counter already points at the next instruction.

    Worked solution

    1. At the start of tick 3 the counter holds 1, the address of the next instruction.
    2. Fetch: address 1 goes out on the address bus, and the byte there, LDB 15, lands in the instruction register.
    3. In the same tick the counter goes up by one: 1+1=21 + 1 = 2⁠.

    If you chose 1, you took the counter to hold the instruction running now; it is always one step ahead of it.

  3. Step 3 of 5

    Open this step

    Three ticks an instruction

    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 ADD and raises ALU out and A in.

    Feedback for a wrong answer

    Not yet. Find the fetch of ADD and go one tick on.

    Hint

    The instruction at address nn⁠ starts at tick 3n3n⁠.

    Worked solution

    1. Each instruction takes three ticks, and the first starts at tick 0.
    2. ADD at address 2 is the third instruction, so it starts at tick 2×3=62 \times 3 = 6⁠.
    3. 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.

  4. Step 4 of 5

    Open this step

    Counting ticks

    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

    Feedback for a right answer

    Four instructions times three ticks: 4×3=124 \times 3 = 12⁠. 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

    1. STA 13 is the fourth instruction: LDA, LDB, ADD, STA.
    2. Each instruction takes three ticks: fetch, decode, execute.
    3. 4×3=124 \times 3 = 12⁠ ticks: ticks 0 to 11.

    If you answered 1111⁠, you read the number of the last tick; starting from 0, there is one more. If you answered 44⁠, you counted one tick per instruction.

  5. Step 5 of 5

    Open this step

    How many instructions so far

    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

    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

    1. By the end of tick 9, 9+1=109 + 1 = 10⁠ ticks have run.
    2. Each instruction takes three: 10=3×3+110 = 3 \times 3 + 1⁠.
    3. Three instructions are done, and the tick left over is the fourth one’s fetch.

    If you answered 1010⁠, you counted one tick per instruction. If you answered 44⁠, 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.

Open the lesson

The 6 steps and their answers
  1. Step 1 of 6

    Open this step

    Adding

    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 ADD executes, 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/registerA

    A prediction is never marked as a failure: the learner sees the outcome and the explanation either way.

    Feedback for a right answer

    1. 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

    1. After tick 5: A holds 100, and B holds 50.
    2. On the execute of ADD the ALU works out 100+50=150100 + 50 = 150⁠.
    3. 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.

  2. Step 2 of 6

    Open this step

    Eight bits wrap

    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, 300−256=44300 - 256 = 44⁠.

    • 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 260−256=4260 - 256 = 4⁠. 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/registerA

    A 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. 200+100=300200 + 100 = 300⁠, which is 1 0010 1100: nine bits. A keeps only the lower eight, 300−256=44300 - 256 = 44⁠.

    Worked solution

    1. The true sum: 200+100=300200 + 100 = 300⁠.
    2. Eight bits only go up to 255; 300 needs a ninth bit, worth 256.
    3. The ninth bit goes out to the carry flag, and the register keeps 300−256=44300 - 256 = 44⁠.

    The sum does not stick at 255: it wraps round past zero.

  3. Step 3 of 6

    Open this step

    Working out the wrap

    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

    Feedback for a right answer

    250+10=260250 + 10 = 260⁠, and in eight bits 260−256=4260 - 256 = 4⁠. The carry flag lights, marking the 256 that did not fit.

    Hint

    How far over 255 is the sum?

    Worked solution

    1. The true sum: 250+10=260250 + 10 = 260⁠.
    2. 260260⁠ is more than 255, so the ninth bit, worth 256, drops off.
    3. Register A keeps 260−256=4260 - 256 = 4⁠.

    If you answered 260260⁠, you forgot that the register has only eight bits. If you answered 255255⁠, you stopped at the ceiling; the adder wraps past it.

  4. Step 4 of 6

    Open this step

    Subtracting

    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/registerA

    A prediction is never marked as a failure: the learner sees the outcome and the explanation either way.

    Feedback for a right answer

    1. The ALU subtracts with the same adder: it flips B’s bits, adds 1, and adds. So 90+(256−25)=32190 + (256 - 25) = 321⁠, and in eight bits 321−256=65321 - 256 = 65⁠.

    Worked solution

    1. A holds 90, and B holds 25.
    2. SUB works out A−B=90−25=65A - B = 90 - 25 = 65⁠.
    3. The answer goes back into A.

    Inside the ALU it is an addition: 90+(256−25)=32190 + (256 - 25) = 321⁠, and the ninth bit drops off. That is why the carry flag lights here: it means no borrow was needed.

  5. Step 5 of 6

    Open this step

    Below zero

    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

    Feedback for a right answer

    253, 1111 1101. 5−85 - 8⁠ goes three steps below zero and wraps backwards: 256−3=253256 - 3 = 253⁠.

    Hint

    One step below zero, in eight bits, is 255.

    Worked solution

    1. Ordinary arithmetic: 5−8=−35 - 8 = -3⁠.
    2. A register has no minus sign, and eight bits wrap like an odometer: one step below 0 is 255.
    3. Three steps below zero: 256−3=253256 - 3 = 253⁠.

    If you answered −3-3⁠, you expected a minus sign in the register. If you answered 33⁠, you took the smaller from the larger; SUB always works out A−BA - B⁠.

  6. Step 6 of 6

    Open this step

    The zero flag

    Practice · Predict, then look

    Question

    In the subtracting program, First number and Second number are both 37. What will happen when SUB executes?

    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

      37+37=7437 + 37 = 74⁠ 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/zeroFlag

    A prediction is never marked as a failure: the learner sees the outcome and the explanation either way.

    Feedback for a right answer

    37−37=037 - 37 = 0⁠: 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

    1. SUB works out A−B=37−37=0A - B = 37 - 37 = 0⁠.
    2. A latches 0.
    3. Every ADD or SUB also 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.

Open the lesson

The 7 steps and their answers
  1. Step 1 of 7

    Open this step

    A program with a loop

    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/programCounter

    A prediction is never marked as a failure: the learner sees the outcome and the explanation either way.

    Feedback for a right answer

    1. 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 13 from the top of the program again.

    Worked solution

    1. After the fetch on tick 27 the counter holds 10.
    2. On the execute of JMP 0 the address 0 passes from the instruction register into the counter, in place of 10.
    3. So after tick 29 the counter holds 0, and the next round begins.
  2. Step 2 of 7

    Open this step

    A conditional jump

    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 SUB brought 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 SUB brought the rounds left down to zero

    • Wrong Always, like JMP

      Feedback

      Then the program would skip to HLT in the first round. JZ is 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 14 means “load A with what is in cell 14”, that is 28, not the number 14 itself.

    • Wrong Never: the JMP 0 after it always goes back up

      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.

    Feedback for a right answer

    JZ checks the zero flag, which the round’s SUB set. While rounds are left the flag is off, and the processor goes on to JMP 0. When the count reaches 0 the flag lights, and the jump to 10 leaves the loop for HLT.

    Worked solution

    1. At addresses 4 to 7 the program loads the rounds left, takes away 1, and stores it.
    2. The SUB lights the zero flag only when the answer is 0.
    3. JZ 10 jumps 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.

  3. Step 3 of 7

    Open this step

    Multiplying by adding

    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

    Feedback for a right answer

    1. Each round the program adds 6 to cell 13: 6, 12, 18, 24. That is how a processor that can only add multiplies: 6×46 \times 4⁠ is 6 four times.

    Hint

    How much does each round add to cell 13, and how many rounds are there?

    Worked solution

    1. At the start cell 13 holds 0.
    2. Each round, at addresses 0 to 3, loads the cell, adds 6, and stores it back.
    3. The loop runs four rounds, so 0+6+6+6+6=240 + 6 + 6 + 6 + 6 = 24⁠.

    If you answered 66⁠, you read the program once from top to bottom; JMP 0 sends it back to the start three more times.

  4. Step 4 of 7

    Open this step

    Counting instructions

    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 HLT included?

    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

    Feedback for a right answer

    1. Each round carries out the ten instructions at addresses 0 to 9, except the last, which jumps from JZ straight to HLT: 10+10+9+1=3010 + 10 + 9 + 1 = 30⁠.

    Hint

    How many instructions run in one round, and what is different in the last?

    Worked solution

    1. An ordinary round: ten instructions, addresses 0 to 9, JMP 0 included.
    2. In the last round JZ 10 jumps, so JMP 0 does not run: nine instructions, then HLT.
    3. Three rounds: 10+10+9+1=3010 + 10 + 9 + 1 = 30⁠, that is ten instructions a round.

    If you answered 1111⁠, you counted the instructions in memory, as if each ran once. If you answered 3333⁠, you counted all eleven instructions in every round.

  5. Step 5 of 7

    Open this step

    From ticks to seconds

    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 HLT executes?

    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

    Feedback for a right answer

    4040⁠ instructions times 33⁠ ticks are 120120⁠ ticks, and 1201000=0.12\dfrac{120}{1000} = 0.12⁠: that is 0.12 s, or 120 ms.

    Hint

    How many ticks does the program take? Divide by the ticks per second.

    Worked solution

    1. Four rounds of ten instructions: 4040⁠ instructions, the HLT included.
    2. Each instruction is three ticks: 40×3=12040 \times 3 = 120⁠ ticks.
    3. The time is the ticks divided by the clock speed: t=1201000 Hz=0.12 st = \dfrac{120}{1000\ \mathrm{Hz}} = 0.12\ \mathrm{s}⁠.

    If you answered 0.04 s, you divided instructions, not ticks, by the clock speed.

  6. Step 6 of 7

    Open this step

    A faster clock

    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: 1204000=0.03\dfrac{120}{4000} = 0.03⁠. 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

    1. The time: t=Nft = \dfrac{N}{f}⁠, with NN⁠ the number of ticks, so f=Ntf = \dfrac{N}{t}⁠.
    2. Substitute: f=1200.03=4000 Hzf = \dfrac{120}{0.03} = 4000\ \mathrm{Hz}⁠.
    3. Set Clock speed to 4000 Hz.

    At 1000 Hz the program takes 0.12 s: four times as long.

  7. Step 7 of 7

    Open this step

    Real processors

    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.

    Feedback for a right answer

    3×1093=109\dfrac{3 \times 10^9}{3} = 10^9⁠: 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

    1. 3 GHz is 3×1093 \times 10^9⁠ ticks a second.
    2. Each instruction takes three ticks.
    3. Instructions a second: 3×1093=109\dfrac{3 \times 10^9}{3} = 10^9⁠, 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, 00 to 11; and four lines give 24=162^4 = 16⁠ addresses, from 0000 to 1111. One line per cell would take sixteen wires.

  • 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 28=2562^8 = 256⁠ different patterns, but one of them is zero, 00000000. So the numbers run from 0 to 255, and the largest is 28−1=2552^8 - 1 = 255⁠, 11111111. Writing 256 takes a ninth bit.

  • 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 1110 the bits for 16, 8, 4 and 2 are on, so it is 16+8+4+2=3016 + 8 + 4 + 2 = 30⁠. Splitting it into two halves, an opcode and an address, is right only when the byte is read as an instruction.

  • 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 14 means “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 14 the number 28 is in register A and still in cell 14. Only a store, STA, changes a memory cell.

  • 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 15 from 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, 300−256=44300 - 256 = 44⁠.

  • 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 260−256=4260 - 256 = 4⁠. 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: 5−85 - 8⁠ gives 256−3=253256 - 3 = 253⁠, 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.

  • 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.

Index