An arithmetic logic unit is the part of a processor that does the calculating. It takes two words and an operation code and returns a word, plus a few flags that describe the result. This lesson pairs with ALU Design by Ben Eater and Exploring How Computers Work by Sebastian Lague, linked under Watch & read. The first covers an ALU built around an adder, with subtraction by two's complement, and the second gives a wider tour from gates to a calculating unit. You build an ALU for eight operations, a barrel shifter, and the comparisons a program makes from the flags.
Words here are lists of bits with the least significant bit first, as in the adders lesson of this course. The unit holds three circuits side by side. The adder handles ADD and SUB, the logic unit handles AND, OR, XOR and NOT, and the shifter handles SHL and SHR. All of them see the same inputs, and the 3-bit op code selects which result comes out.
Subtraction needs no new circuit. Inverting every bit of b gives -b - 1 in two's complement, and a carry-in of 1 supplies the missing 1, so a + (not b) + 1 equals a - b. The adder returns two facts besides the sum. The carry out of the top bit says the true result needed a ninth bit. The overflow flag says the signed result left its range, and it is the carry into the top bit XOR the carry out of it.
Those two flags answer different questions, and the same bits can give different answers to each. With eight-bit words, 100 + 50 gives 10010110 with carry 0 and overflow 1: as unsigned numbers 150 fits, as signed numbers it does not. 200 + 100 gives 00101100 with carry 1 and overflow 0: the unsigned sum 300 lost its ninth bit, while the signed sum -56 + 100 = 44 is fine.
def add_flags(a, b, cin=0, width=8):
total = a + b + cin
result = total & (2 ** width - 1)
sign = lambda n: n >> (width - 1)
overflow = int(sign(a) == sign(b) != sign(result))
return result, total >> width, overflow
print(add_flags(100, 50)) # (result, carry, overflow)
print(add_flags(200, 100))The other two flags describe the stored result: zero is 1 when every bit is 0, and negative is the top bit. The shifter sets carry to the bit that fell out of the word, and the logic unit leaves carry and overflow at 0.
After SUB, a program reads the flags to compare. The carry from a + (not b) + 1 is 1 exactly when a >= b as unsigned numbers, so carry 1 means no borrow. Unsigned less-than is therefore a clear carry. Signed less-than is negative XOR overflow, because an overflowing subtraction stores the wrong sign. EQ is the zero flag.
a, b = 128, 1 # -128 - 1
diff = (a - b) & 255
carry = int(a >= b) # 1: no borrow
negative = diff >> 7
overflow = int((a >> 7) != (b >> 7) and (diff >> 7) != (a >> 7))
print(diff, negative, carry, overflow)
print("LT", negative ^ overflow, "LTU", 1 - carry)A barrel shifter moves a word by any amount in a fixed number of steps. Write the amount in binary: shifting by 5 is a shift by 4 plus a shift by 1. Each stage is a row of 2-to-1 multiplexers that picks, bit by bit, the shifted word or the unshifted one, according to one bit of the amount. Eight bits need three stages, and an amount of 8 or more clears the word.
a = 0b00010110
for k in range(3):
if (5 >> k) & 1:
a = (a << (1 << k)) & 255
print(k, format(a, "08b"))Two mistakes are common. The first is deriving signed less-than from the negative flag alone, which is right until a subtraction overflows and then gives the wrong answer for pairs like the one above. The second is mixing up the carry convention after SUB: some processors invert the carry so that it means a borrow, and this unit does not, so reading carry 1 as a borrow answers the opposite question.
The tests check the unit against Python integers. For width 4 every pair of words is tried with every operation, and for width 8 the edge values and a seeded sample are tried. The comparisons are checked against ordinary signed and unsigned integer comparison, and the barrel shifter against integer shifts. That shows the flags and results match the arithmetic definitions for those inputs. It does not show how fast a real circuit settles, and the carry convention is a choice that real instruction sets make differently, so a program written for one machine needs its own flag rules.