C718 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 18 · The arithmetic logic unit

One block that adds, subtracts, ANDs and ORs, and the four flags that let a program compare

Lesson 17 built adders. A processor needs more than addition from the same hardware: subtraction, comparison, the logical operations, all selected by a few control bits from the instruction. That block is the arithmetic logic unit, and alongside its result it reports four facts about it: was the result zero, was it negative, did an unsigned operation carry out, did a signed one overflow? Those four flags are how every comparison in a program is made, and getting them right is a small, exact exercise in two's complement.

  • A 2-bit ALU built from two slices. Op1 Op0 selects the operation: 00 AND, 01 OR, 10 ADD, 11 SUBTRACT. Flip the inputs and watch the result and flags.
  • Subtraction is addition with B inverted and a carry of 1 into the lowest slice: A−B=A+B¯+1.
  • Try A = 01, B = 01 with SUBTRACT: the result is 00, Z lights, and C = 1 because no borrow was needed.

Predict

A 4-bit ALU computes 5 − 7 by adding 0101, 1010 and a carry-in of 1. What are the result and the carry out?

Name it

Bit slices and function select

An n-bit ALU is n copies of a one-bit slice: a full adder, the logic gates, and a multiplexer that picks the slice's output according to the operation code. The adder's carries ripple (or look ahead) from slice to slice, and subtraction reuses them: XOR gates invert B when subtracting, and the lowest carry-in is set to 1, because −B=B¯+1 in two's complement.

The four flags

After an operation producing result R with carries cn out of the top bit and cn−1 into it:

FlagMeaningLogic
Z, zeroR=0NOR of all result bits
N, negativeR's sign bitRn−1
C, carryunsigned carry out; after a subtraction, 1 means no borrowcn
V, overflowthe signed result didn't fitcn⊕cn−1

Overflow can be detected two ways that always agree: the carries into and out of the sign bit differ, or two operands of the same sign produced a result of the other sign (lesson 17).

Comparisons from flags

Compute A−B and discard the result:

  • A=B: Z = 1.
  • Unsigned A<B: C = 0 (a borrow).
  • Signed A<B: N⊕V=1. The sign bit alone is wrong when the subtraction overflowed, and the XOR corrects it.

That's what a "compare" instruction does, and what conditional branches test. RISC-V skips the flags register and computes the comparison directly in the branch instruction, but the logic inside is the same.

Speed

The ALU's critical path is the carry chain: in a ripple design, about two gate delays per bit, plus the multiplexer, plus the zero detector, which is a tree of ⌈log2⁡n⌉ levels of 2-input gates. Lookahead adders shorten the chain to a few levels, which is why fast ALUs use them.

Worked example

A 4-bit ALU subtracts B = 0111 (7) from A = 0101 (5). Find the result and all four flags, and say what the flags conclude about A < B, signed and unsigned.

Show the solution
  1. Add 0101+1000+1 bit by bit.
BitSumCarry out
001
110
210
310
  1. Read the result. The result is 1110 = −2.
  2. Set the flags. c4=0 and c3=0, so C = 0 and V = 0; N = 1 and Z = 0.
  3. Unsigned comparison. C = 0 means a borrow, so 5<7.
  4. Signed comparison. N⊕V=1, so 5<7 again.
  5. Why they agree. Both agree here because neither number has its top bit set.

Answer the result is 1110 (−2), with C = 0, V = 0, N = 1 and Z = 0; both comparisons say A < B.

Worked example

Now subtract B = 0111 (+7) from A = 1000 (−8 signed, 8 unsigned). Find the flags and the comparisons.

Show the solution
  1. Add 1000+1000+1 bit by bit. Only bit 3 carries: 1+1=0 carry 1.
BitSumCarry out
010
100
200
301
  1. Set the flags. The result is 0001 with a carry out: c4=1 and c3=0, so C = 1, V = 1, N = 0 and Z = 0.
  2. Unsigned comparison. C = 1, no borrow, so 8≥7: right.
  3. The signed result. The result 0001 = +1 is wrong, since −8 − 7 = −15 doesn't fit in 4 bits, and V flags it.
  4. Signed comparison. N⊕V=0⊕1=1 says −8<7: right.
  5. Why N alone fails. Testing N alone would have said −8≥7.

Answer C = 1, V = 1, N = 0 and Z = 0. Unsigned, C = 1 says 8≥7; signed, N⊕V=1 says −8<7. Both are right.

Worked example

A 32-bit ripple-carry ALU uses gates of 20 ps. The carry takes two gate delays per bit, the output multiplexer two more, and the zero flag a tree of 2-input NORs and ANDs after that. How long until Z is valid? What does a 4-input gate tree save?

Show the solution
  1. Carry chain. t=32×2×20ps=1280ps
  2. Multiplexer. Two more gate delays: 40 ps.
  3. Zero detector. It combines 32 bits in log2⁡32 = 5 levels: 100 ps.
  4. Total. 1280+40+100=1420ps
  5. With 4-input gates. The tree takes ⌈log4⁡32⌉ = 3 levels, 60 ps, saving 40 ps, about 3%.
  6. Where the time goes. The carry chain is 90% of the delay, which is the case for lookahead.

Answer Z is valid after 1420 ps; a 4-input gate tree saves 40 ps, about 3%.

Check

A 4-bit ALU computes A − B for A = 0101 and B = 0001. What 4-bit result does it produce?

Check

A 4-bit ALU subtracts B = 0001 from A = 1010. Signed, A is negative and B positive. What result does it produce? (The V flag will say whether it's wrong.)

Check

After computing A − B, write the signed less-than condition from the N and V flags, using AND, OR and NOT.

Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.

Check

Write the zero flag of a 4-bit result.

Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.

Check

In the play's ALU, Op1 Op0 = 11 means subtract. Write the slice's adder input x, which is B for every other operation and B¯ when subtracting.

Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.

Check

A 16-bit ripple ALU has gates of 10 ps: two gate delays per bit of carry, two for the output multiplexer, then a zero detector built as a tree of 2-input gates. How long until Z is valid?

Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.

Check

Computing A − B on 8 bits sets C = 1, N = 1, V = 1, Z = 0. Which is true?

Check

Write alu(a, b, op) for an n-bit ALU (16 bits), with op one of 'and', 'or', 'add', 'sub', taking unsigned inputs below 2n and returning (r, z, neg, c, v): the result and the four flags as 0 or 1, computed the way hardware does (subtract as a+b¯+1; C is the carry out of the top bit; V from the carries into and out of the top bit; logic operations clear C and V). Then write signed_lt(a, b) and unsigned_lt(a, b) using only the flags from alu(a, b, 'sub').

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

A programmer tests whether a signed 8-bit temperature is below a threshold by subtracting and checking only the sign of the result. It works in testing but fails for a reading of −100 against a threshold of 50. Explain using the ALU's flags.

Practice

Keep practicing

6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.