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.
Step 1 of 6 · Adding
Step 1 of 6
Adding
A register is a small store inside the processor, close to the part that computes. This chip has two, A and B. The ALU, the arithmetic logic unit, sits between them and is always working out the sum of what they hold. The control unit decides whether the answer goes anywhere: on the execute of ADD it raises ALU out and A in, and the sum goes back into A.
First number is 100 and Second number is 50. Clock tick stands at 5: both numbers are already in the registers.
Step 2 of 6
Eight bits wrap
A register has eight bits, so it holds 0 to 255. What happens when the sum is bigger? The ALU adds bit by bit, as in column addition, with a carry passed on to the next bit. The top bit has no next bit in the register: its carry goes out to the carry flag, a small light on the ALU.
Step 3 of 6
Working out the wrap
When the ninth bit drops off, the sum loses exactly 256. So this is how to find what is left in the register: work out the true sum, and if it is 256 or more, take away 256. The lid is closed; work it out yourself.
Step 4 of 6
Subtracting
Now memory holds a different program, with SUB in place of ADD at address 2. The ALU subtracts with the same adder: it flips every bit of B, adds 1, and adds. Adding and then dropping 256 is exactly taking away B.
Step 5 of 6
Below zero
What if B is bigger than A? A register has no minus sign, and eight bits wrap like an odometer: one step below 0 is 255, two steps are 254. Computers that need negative numbers agree to read the numbers 128 to 255 as negative; the scheme is called two’s complement. The chip itself only wraps.
Step 6 of 6
The zero flag
Beside the carry the ALU has a second flag, zero, which lights when the last answer of an ADD or SUB is 0. A flag changes no number; it remembers something about the answer, so that a later instruction can ask about it. The two numbers here are equal.
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
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?
Practice · Predict, then look
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?
Practice · Predict, then look
Now First number is 200 and Second number is 100. What will register A hold after the ADD?
Practice · Predict, then look
Now First number is 200 and Second number is 100. What will register A hold after the ADD?
Practice · Calculate
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?
Practice · Calculate
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?
Practice · Predict, then look
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?
Practice · Predict, then look
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?
Practice · Calculate
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?
Practice · Calculate
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?
Practice · Predict, then look
In the subtracting program, First number and Second number are both 37. What will happen when SUB executes?
Practice · Predict, then look
In the subtracting program, First number and Second number are both 37. What will happen when SUB executes?
Lesson complete. Every practice question here is done.