Lesson 5. The ± calculator: the minus on a lamp, the magnitude on the display
In picturesAfter this lesson you will have a calculator: set two numbers, choose "+" or "−" — and see a signed answer, 3 − 5 → "−" and 2. This lesson completes the kit: it already has everything a processor counts with. Lesson 6 is a bonus.
5.1. The idea: the magnitude is one more subtraction
The display can show only unsigned numbers. If we show 3 − 5 as it is, we see 254 — that is −2 in two's complement, but people do not read it that way. We need to show the sign and the magnitude separately.
- The sign is the
Nflag, the top bit of the answerS7. The "−" lamp shows it. - The magnitude of a negative number is the same number with the opposite sign:
0 − S. And we can already subtract.
The second ADDSUB8 computes 0 + S or 0 − S, and the answer itself decides which: its S7 goes to SUB. A positive answer — the second adder adds it to zero and passes it through. A negative one — it subtracts it from zero and flips the sign.
Set numbers from 0 to 127 (switches A7 and B7 off): in 8 signed bits there is nothing above 127. If the answer does not fit, V is lit and the display shows a wrong number.
5.2. Building
The same project. Delete the lesson 4 setup (17 switches, ADDSUB8 on the canvas, OR/NOR, NOT/AND, lamps, displays); keep Power/Ground.
The idea. The display can show only unsigned numbers. So a negative answer has to be shown as a "minus" on a lamp and the magnitude on the display. The magnitude of a negative number is the same number with the opposite sign, that is 0 − S. And we can already subtract: the second ADDSUB8 computes 0 + S or 0 − S, and the answer chooses by itself — its top bit S7 (the N flag) goes to SUB.
Set numbers from 0 to 127 (switches A7 and B7 off): in 8 signed bits there is nothing above 127. If the answer does not fit, V is lit — the display is then wrong.
Step 1. Switches and the adder A ± B
| Part | Pin | Label |
|---|---|---|
| Input | out | A0 |
| Input | out | A1 |
| Input | out | A2 |
| Input | out | A3 |
| Input | out | A4 |
| Input | out | A5 |
| Input | out | A6 |
| Input | out | A7 |
| Input | out | B0 |
| Input | out | B1 |
| Input | out | B2 |
| Input | out | B3 |
| Input | out | B4 |
| Input | out | B5 |
| Input | out | B6 |
| Input | out | B7 |
| Input | out | SUB |
| ADDSUB8 (A ± B) | A0 A1 A2 A3 A4 A5 A6 A7 B0 B1 B2 B3 B4 B5 B6 B7 SUB S0 S1 S2 S3 S4 S5 S6 S7 CO V | A0 A1 A2 A3 A4 A5 A6 A7 B0 B1 B2 B3 B4 B5 B6 B7 SUB S0 S1 S2 S3 S4 S5 S6 S7 C V |
Step 2. The magnitude on the display, the sign on a lamp
| Part | Pin | Label |
|---|---|---|
| ADDSUB8 (magnitude) | A0 A1 A2 A3 A4 A5 A6 A7 B0 B1 B2 B3 B4 B5 B6 B7 SUB S0 S1 S2 S3 S4 S5 S6 S7 CO V | GND GND GND GND GND GND GND GND S0 S1 S2 S3 S4 S5 S6 S7 S7 M0 M1 M2 M3 M4 M5 M6 M7 — (free) — (free) |
| LED ("−" — sign) | in | S7 |
| LED (V — does not fit) | in | V |
| Display | ⚙ | width 8, DEC, bus A |
| Display | ⚙ | width 8, DEC, bus B |
| Display | ⚙ | width 8, DEC, bus M |
A0–A7 of the second adder all go to GND: that is the zero we subtract from. Its CO and V are not needed.
5.3. Check
| A | Switches A7…A0 | B | Switches B7…B0 | SUB | Display M | Lamps | Meaning |
|---|---|---|---|---|---|---|---|
| 3 | 0000 0011 | 5 | 0000 0101 | 1 | 2 | − | 3 − 5 = −2 |
| 100 | 0110 0100 | 27 | 0001 1011 | 0 | 127 | — | 100 + 27 = 127 |
| 10 | 0000 1010 | 100 | 0110 0100 | 1 | 90 | − | 10 − 100 = −90 |
| 127 | 0111 1111 | 127 | 0111 1111 | 1 | 0 | — | 127 − 127 = 0 |
| 42 | 0010 1010 | 0 | 0000 0000 | 1 | 42 | — | 42 − 0 = 42 |
| 100 | 0110 0100 | 50 | 0011 0010 | 0 | — | V | 150 is more than 127 — does not fit |
In the "Switches" columns the top switch (A7) is on the left and A0 on the right; 1 — on, 0 — off.
If you see a big number instead of the magnitude (say 254 instead of 2), the second ADDSUB8 is not subtracting: its SUB is not on S7.
Lesson 5 paste (Cmd+I)
Needs ADDSUB8 from lesson 4. Delete the lesson 4 setup, keep the power.
# Lesson 5 — the ± calculator: magnitude on the display, sign on a lamp.input(A0)input(A1)input(A2)input(A3)input(A4)input(A5)input(A6)input(A7)input(B0)input(B1)input(B2)input(B3)input(B4)input(B5)input(B6)input(B7)input(SUB)ADDSUB8(A0=A0, A1=A1, A2=A2, A3=A3, A4=A4, A5=A5, A6=A6, A7=A7, B0=B0, B1=B1, B2=B2, B3=B3, B4=B4, B5=B5, B6=B6, B7=B7, SUB=SUB, S0=S0, S1=S1, S2=S2, S3=S3, S4=S4, S5=S5, S6=S6, S7=S7, CO=C, V=V)ADDSUB8(A0=GND, A1=GND, A2=GND, A3=GND, A4=GND, A5=GND, A6=GND, A7=GND, B0=S0, B1=S1, B2=S2, B3=S3, B4=S4, B5=S5, B6=S6, B7=S7, SUB=S7, S0=M0, S1=M1, S2=M2, S3=M3, S4=M4, S5=M5, S6=M6, S7=M7)led(S7)led(V)¿No te sale? Algo no funciona →