Lesson 5. The ± calculator: the minus on a lamp, the magnitude on the display

In pictures

After 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 N flag, the top bit of the answer S7. 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.

A = 3B = 5SUB = 1ADDSUB8A ± BS = 1111 1110(254 = −2)0A = GNDS7 = 1 → SUBADDSUB80 − SM−
The ± calculator for 3 − 5. The first ADDSUB8 computes S = 254 — that is −2 in two's complement, the top bit S7 = 1. S7 lights the "−" lamp and makes the second ADDSUB8 subtract: 0 − S = 2. The display shows the magnitude, the lamp shows 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

PartPinLabel
InputoutA0
InputoutA1
InputoutA2
InputoutA3
InputoutA4
InputoutA5
InputoutA6
InputoutA7
InputoutB0
InputoutB1
InputoutB2
InputoutB3
InputoutB4
InputoutB5
InputoutB6
InputoutB7
InputoutSUB
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

PartPinLabel
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)inS7
LED (V — does not fit)inV
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

ASwitches A7…A0BSwitches B7…B0SUBDisplay MLampsMeaning
30000 001150000 010112−3 − 5 = −2
1000110 0100270001 10110127—100 + 27 = 127
100000 10101000110 0100190−10 − 100 = −90
1270111 11111270111 111110—127 − 127 = 0
420010 101000000 0000142—42 − 0 = 42
1000110 0100500011 00100—V150 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)

Stuck? Something doesn't work →