Arithmetic logic unit


An Arithmetic Logic Unit (ALU) is a circuit that performs basic arithmetic and logic operations, often used in CPUs. They usually have "flags" to keep track of different things during execution, like if the result overflowed, or the result was zero. Flags are usually implemented with flip-flops. Operations that an ALU could perform are arithmetic operations such as addition and subtraction, as well as bitwise operations like XOR, AND, and OR.
The different kinds of flags an ALU could keep track of are:
- If "A" is equal to "B"
- If "A" is lesser than "B"
- If "A" is greater than "B"
- If the carry bit was set during execution.
The carry bit is usually the "Carry-out" part of an adder.
ALU Designs and operations
ALUs can have many different designs with varying specifications, such as bit width, operations, size, and speed. Below are a few operations an ALU could have:
| Operation | Description |
|---|---|
| Add | Adds A and B |
| Sub | Subtracts B from A |
| AND | Outputs true if both A and B are true, otherwise false |
| NAND | Inverse of AND. If A and B are true, output is false |
| OR | Outputs true if either A or B is true |
| NOR | Inverse of OR. If either A or B is true, output is false |
| XOR | Outputs true if exactly one of A or B is true |
If your ALU only has arithmatic functions, it would be an Arithmatic Unit (AU). If your ALU only has logic functions, it would be a Logic Unit (LU).
Multiplexer based ALU's

Multiplexer based ALU's use a Multiplexer to select outputs from their different operations.
They are fairly easy to make, and usually easy to understand, but can take up a lot of space, and are limited in operations.
Control line based ALU's
Unlike Mux Based ALU's, Control Line Based ALU's don't have a multiplexer, instead they have inputs like:
- Carry
- !A
- !B
- OR
- XOR
- AND
- NOR
They can do lots of operations. They don't have a subtract mode input, instead you can turn on "Invert B" (!B) and carry to perform subtraction.
Note that they don't have an opcode input like multiplexer based ALUs, so you have to make a ROM that turns on specific pins like:
| Operation | Pins | Description |
|---|---|---|
| Add | None | Adds A and B |
| Sub | Carry, !B | Subtracts B from A |
| AND | AND | Outputs true if both A and B are true, otherwise false |
| NAND | !A, !B, AND | Inverse of AND. If A and B are true, output is false |
| OR | OR | Outputs true if either A or B is true |
| NOR | !A, !B, OR | Inverse of OR. If either A or B is true, output is false |
| XOR | XOR | Outputs true if exactly one of A or B is true |
| XNOR | !A, !B, XOR | Inverse of XOR Outputs true if exactly one of A or B is false |
| INC | B = 1 | Increments A by 1 |
| DEC | Carry, !B, B = 1 | Decrements A by 1 |
They allow for complex operations, and are usually small, but can be complex and difficult to build.