Lesson 4. 8 bits and comparison: CMP is subtraction

In pictures

After this lesson the adder becomes 8-bit — numbers from 0 to 255 — and three lamps answer which number is bigger: A = B, A < B or A > B. Nothing new has to be invented: two ADDSUB4 in a chain and the subtraction flags.


4.1. The idea: two halves

Eight bits are two halves of four. The carry from the low half (CO) goes to the carry input of the high half (CI) — just as it runs between digits inside ADDER4. The "plus 1" of subtraction is needed only once, in the very lowest digit, so SUB goes to CI only on the low ADDSUB4.

ADDSUB4high: bits 4–7ADDSUB4low: bits 0–3A4–A7, B4–B7A0–A3, B0–B3c4CO → CISUBto CICOVS4–S7S0–S3
ADDSUB8 — two ADDSUB4 in a chain. The carry of the low half c4 goes to the CI of the high half, as between digits inside ADDER4. The "plus 1" of subtraction (SUB on CI) is only for the low half. The CO and V flags come from the high half: the sign bit is there.

Overflow V is computed from the top, sign bit — so we take it from the high half, and the V of the low half is not needed.

4.2. The idea: comparison is subtraction

To compare two numbers, subtract B from A and look at the flags:

  • A = B — the result is zero, Z is lit;
  • A < B — a borrow was needed, C = 0;
  • A > B — no borrow (C = 1), and the result is not zero.

That is how every processor compares numbers. The CMP (compare) instruction is a subtraction that keeps only the flags and throws the result away. The next instruction looks at the flags and decides where to jump.

4.3. Building

The same project. Delete the lesson 3 setup (9 switches, ADDSUB4 on the canvas, OR/OR/NOR, lamps, three displays); keep Power/Ground.

The idea. Eight bits are two halves of four. The carry from the low half (CO) goes to the carry input of the high half (CI) — just as the carry runs between digits inside ADDER4. The "plus 1" of subtraction is needed only once, in the very lowest digit, so SUB goes to CI only on the low ADDSUB4.

Comparison is subtraction. Subtract B from A and look at the flags:

  • A = B — the result is zero, Z is lit;
  • A < B — there was a borrow, C = 0;
  • A > B — no borrow (C = 1) and the result is not zero.

That is how every processor compares numbers: the CMP instruction is a subtraction that keeps only the flags, not the result.

Step 1. ADDSUB8 — two ADDSUB4 in a chain

PartPinLabel
Input portoutA0
Input portoutA1
Input portoutA2
Input portoutA3
Input portoutA4
Input portoutA5
Input portoutA6
Input portoutA7
Input portoutB0
Input portoutB1
Input portoutB2
Input portoutB3
Input portoutB4
Input portoutB5
Input portoutB6
Input portoutB7
Input portoutSUB
ADDSUB4 (low 4 bits)A0
A1
A2
A3
B0
B1
B2
B3
SUB
CI
S0
S1
S2
S3
CO
V
A0
A1
A2
A3
B0
B1
B2
B3
SUB
SUB
S0
S1
S2
S3
c4
— (free)
ADDSUB4 (high 4 bits)A0
A1
A2
A3
B0
B1
B2
B3
SUB
CI
S0
S1
S2
S3
CO
V
A4
A5
A6
A7
B4
B5
B6
B7
SUB
c4
S4
S5
S6
S7
CO
V
Output portinS0
Output portinS1
Output portinS2
Output portinS3
Output portinS4
Output portinS5
Output portinS6
Output portinS7
Output portinCO
Output portinV

Ctrl+G → name ADDSUB8. Pins: inputs A0–A7, B0–B7, SUB; outputs S0–S7, CO, V.

The V pin of the low ADDSUB4 stays free: overflow is computed from the sign bit, and that is in the high half.

Step 2. Canvas: switches, adder, the Z flag

PartPinLabel
InputoutA0
InputoutA1
InputoutA2
InputoutA3
InputoutA4
InputoutA5
InputoutA6
InputoutA7
InputoutB0
InputoutB1
InputoutB2
InputoutB3
InputoutB4
InputoutB5
InputoutB6
InputoutB7
InputoutSUB
ADDSUB8A0
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
ORI0
I1
O0
S0
S1
z01
ORI0
I1
O0
S2
S3
z23
ORI0
I1
O0
S4
S5
z45
ORI0
I1
O0
S6
S7
z67
ORI0
I1
O0
z01
z23
z03
ORI0
I1
O0
z45
z67
z47
NOR (zero)I0
I1
O0
z03
z47
Z

Step 3. Comparison, lamps, displays

PartPinLabel
NOTI0
O0
C
nC
NOTI0
O0
Z
nZ
AND (equal)I0
I1
O0
SUB
Z
EQ
AND (less)I0
I1
O0
SUB
nC
LT
ANDI0
I1
O0
C
nZ
cnz
AND (greater)I0
I1
O0
SUB
cnz
GT
LED (C — carry)inC
LED (Z — zero)inZ
LED (N — sign)inS7
LED (V — overflow)inV
LED (A = B)inEQ
LED (A < B)inLT
LED (A > B)inGT
Display⚙width 8, DEC, bus A
Display⚙width 8, DEC, bus B
Display⚙width 8, DEC, bus S

The comparison lamps go through AND with SUB: they stay dark when adding — comparing only makes sense by subtracting.

4.4. Check

The display shows unsigned numbers: 0…255. Signed, 1xxxxxxx are the negative ones (−128…−1), and then N is lit.

ASwitches A7…A0BSwitches B7…B0SUBDisplay SLampsMeaning
2001100 1000550011 01110255N200 + 55 = 255
2001100 10001000110 0100044C300 does not fit in 8 bits: 256 went into the carry
1000110 01001000110 01000200N, Vsigned: 100 + 100 ≠ −56 — overflow
70000 011170000 011110Z, C, A = Bequal
50000 010190000 10011252N, A < B5 − 9 = −4; a borrow, C = 0
90000 100150000 010114C, A > B9 − 5 = 4
1000110 01002001100 10001156N, A < B100 is less than 200
2001100 10001501001 0110150C, A > B200 is more than 150

In the "Switches" columns the top switch (A7) is on the left and A0 on the right; 1 — on, 0 — off.

If only the high bits of the answer are wrong, check the CI of the high ADDSUB4: it must be on the label c4.


Lesson 4 paste (Cmd+I)

Needs NOT, AND, OR, NOR and ADDSUB4. Delete the lesson 3 setup, keep the power.

# Lesson 4 — 8 bits (ADDSUB8) and comparison by subtraction.chip ADDSUB8 {  portIn(A0)  portIn(A1)  portIn(A2)  portIn(A3)  portIn(A4)  portIn(A5)  portIn(A6)  portIn(A7)  portIn(B0)  portIn(B1)  portIn(B2)  portIn(B3)  portIn(B4)  portIn(B5)  portIn(B6)  portIn(B7)  portIn(SUB)  portOut(S0)  portOut(S1)  portOut(S2)  portOut(S3)  portOut(S4)  portOut(S5)  portOut(S6)  portOut(S7)  portOut(CO)  portOut(V)  ADDSUB4(A0=A0, A1=A1, A2=A2, A3=A3, B0=B0, B1=B1, B2=B2, B3=B3, SUB=SUB, CI=SUB, S0=S0, S1=S1, S2=S2, S3=S3, CO=c4)  ADDSUB4(A0=A4, A1=A5, A2=A6, A3=A7, B0=B4, B1=B5, B2=B6, B3=B7, SUB=SUB, CI=c4, S0=S4, S1=S5, S2=S6, S3=S7, CO=CO, V=V)} 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)OR(S0, S1, z01)OR(S2, S3, z23)OR(S4, S5, z45)OR(S6, S7, z67)OR(z01, z23, z03)OR(z45, z67, z47)NOR(z03, z47, Z)NOT(C, nC)NOT(Z, nZ)AND(SUB, Z, EQ)AND(SUB, nC, LT)AND(C, nZ, cnz)AND(SUB, cnz, GT)led(C)led(Z)led(S7)led(V)led(EQ)led(LT)led(GT)

¿No te sale? Algo no funciona →