by Mwwhited
30. January 2011 13:22
The ALU (Arithmetic Logic Unit) in your computer is a major component of you CPU (Central Processing Unit). The ALU is where most Logic and Mathematical decisions are made inside of your computer. Here is an example of an ALU based on a simple 4 bit design. (This is the minimal design for a ALU. All math and logic functions could be processed in one or more steps based on this design.)

Logic gates and detection circuits
This ALU is composed of several logic functions as well as a few detections. Two of the detections are Zero Detect and Sign. Zero detect is used to check if all of the result bits are set low. This will be helpful by enabling branch on true/false features. Sign detect is used to check the highest order bit. When values are “signed” in a CPU they are relying on the idea of Two’s Compliment. One of the fundamental features from two’s compliment is the ability to subtract two numbers by adding.
Logic features of an ALU are simple bitwise operations. The basic operations are NOT, AND, OR, and XOR. XOR and Zero Detect to can quickly used to test if two values are equal. If the values are passed in as A and B then the and processed with the XOR function the Zero Detect will be true if both values are equal. If any of the bits do not match between the two values then the Zero Detect will be false.
Arithmetic in the ALU
One of the most important features of the ALU is the ability to do math. Most ALU designs are based around binary addition. Reason being if the with two’s compliment, looping, and lookup tables pretty much any math operation may be performed.
The core component of the ALU that allowed for addition if the Half Adder. Two Half Adders may be tied together to form a Full Adder and with the full adder multi-bit addition is possible.
Half Adder (Basic Unit of a Full Adder)

Full Adder

4Bit Adder
By chaining full adders is it possible to add larger and larger values. The example below is 4 full adders chained to allow for binary addition. If you look at the diagram below you will see that it is displaying an example of the equation 5+5=10 (A is Hexadecimal for 10)

4Bit Adder/Subtractor
By using a nice ability of an XOR to work as a controlled buffer/inverter you could extent the above 4bit adder into an Adder/Subtractor as show below. When you select the subtract function the circuit will automatically enable the Carry In as well as Invert the B input value. This affect applies two’s compliment to the B input allowing for the adder to subtract the values.

Summary
By tying an ALU into some registers and a state machine you can start to build you own CPU. Each of these functions allows for more powerful calculations and branching in your process. Understanding how a computer works are this level if very helpful in understanding hardware and software.