An arithmetic logic unit with eight operations and flags, and how it is built inside
The arithmetic logic unit (ALU) is the part of the processor that does the calculations. It receives two operands, A and B, and an operation code that says what to do with them, and delivers the result F and some flags describing that result. It is a combinational circuit: it stores nothing; the operands come from the registers and the result goes back to them on the clock edge.
This example ALU has three blocks working on A and B at the same time:
An 8-to-1 multiplexer per output bit, controlled by the operation code, chooses which of the results reaches F. That is why the ALU computes every operation at once and lets only one through, as the simulator table shows.
| Flag | Set when | What it is for |
|---|---|---|
| C (carry) | The sum leaves bit 3, the subtraction needs no borrow or the shift pushes out a 1 | Unsigned comparison, adding long numbers in parts |
| V (overflow) | The signed result does not fit between −8 and 7 | Detecting errors in signed arithmetic |
| Z (zero) | F = 0000 | Knowing whether two numbers are equal (A − B = 0) |
| N (negative) | Bit 3 of F is 1 | Knowing the sign of the result |
The conditional jumps of any processor are based on these four bits. For example, to know whether A ≥ B unsigned, A − B is computed and C = 1 is checked; signed, N = V is checked. Try A = 12 and B = 5 with SUB in the simulator: unsigned it gives 7, but signed 12 is −4 and −4 − 5 = −9 does not fit in 4 bits, so V is set.
To operate on 8, 16 or 32 bits, several 4-bit ALUs are placed in parallel: they share the operation code, and the carry out of each feeds the next. Logic operations do not need that connection because each bit is independent. The 74LS181 is a classic 4-bit ALU with 16 logic and 16 arithmetic operations, selected with S3–S0 and the mode M, and generate and propagate outputs for carry look-ahead with the 74LS182. Today ALUs live inside the microcontroller or are described in VHDL or Verilog for an FPGA.
Related: adders and subtractors, magnitude comparators, multiplexers and number system converter.