8 Bit ALU Arithmetic Logic Unit

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

instructables

8-bit ALU (Arithmetic Logic Unit)

by Rehaan33

In this Instructable, I will be going through the process of building and understanding the architecture behind an ALU
(Arithmetic Logic Unit). The ALU is part of a computer's CPU (central processing unit) and it handles arithmetic and logic
operations as the name suggests.
The ALU that we will be building is a far simpler version than what may exist on your computer. More complicated ALUs
may handle 64-bit numbers and have many more functions than just addition and subtraction. The ALU that will be
discussed here will only be able to perform addition and subtraction on 8-bit numbers and will be built from 74HC logic
using XOR, AND, and OR gates. All of which will be explained in detail in this instructable.
Supplies:

Supplies
26 AWG Multicore wire (4 meters)
4x 4-way DIP SPST switches
1x PTM Switch (through hole)
24x 470 Ohm resistors
24x 5V LEDs
5V Adapter
30 cm x 10 cm perf board
6x CD4070 (XOR gate)
4x LS7408 (AND gate)
4x LS7432 (OR gate)
5V Adapter

Tools
Soldering Iron
Solder
Wire Cutter/Stripper
Multimeter

8-bit ALU (Arithmetic Logic Unit): Page 1


Step 1: Binary

As with most digital circuits, data is transmitted in a binary format. Binary is a numerical system in base 2, meaning
binary numbers are composed of either 0s or 1s. 0s can be represented as LOW (GND) signals whereas 1s can be
associated with HIGH (for our purposes, 5V) signals. The decimal value of a binary number can be determined by
positions of 0s and 1s. Each 1 or 0 value expresses a certain power of 2. Each 1 or 0 is referred to as a bit of data.
Take the 4-bit examples:
[1 0 0 1] = 2^3 + 0 + 0 + 2^0 = 8 + 1 = 9
[1 1 1 1] = 2^3 + 2^2 + 2^1 + 2^0 = 8 + 4 + 2 + 1 = 15

8-bit ALU (Arithmetic Logic Unit): Page 2


Step 2: Logic

Digital logic refers to the manipulation of binary values using integrated circuits (ICs). These circuits are known as
logic gates. There are many types of 'gates', all of which are composed of a speci c arrangement of transistors (if you
would like to learn more about transistors, I'd recommend the following video from the channel Real Engineering). The
three logic gates we will be focused on are AND, OR, and XOR logic gates.
Logic gates usually take 2 inputs and produce an output dependent on the inputs. An AND gate will only produce a
HIGH output if both inputs are HIGH. OR gates will only produce a HIGH output if at least one input is HIGH. XOR logic
gates will produce a HIGH output if and only if one input is HIGH and the other is LOW.

AND Gate Logic

INPUT 1 INPUT 2 OUTPUT

0 0 0

0 1 0

1 0 0

1 1 1

OR Gate Logic

INPUT 1 INPUT 2 OUTPUT

0 0 0

0 1 1

1 0 1

1 1 1

XOR Gate Logic

INPUT 1 INPUT 2 OUTPUT

0 0 0

0 1 1

1 0 1

1 1 0

8-bit ALU (Arithmetic Logic Unit): Page 3


Step 3: Full Adder

Using 3 digital logic gates (AND, OR, and XOR), we can create what is known as a Full Adder circuit. A full adder takes in 3
inputs. A carry and two binary inputs. An adder is meant to 'add' two binary inputs. If A and B are both 0 (LOW signals),
the output will be 0, assuming there is no carry. Likewise, If A or B is HIGH and the other is LOW, the output will result in a
HIGH signal. If both are HIGH, and the carry-in is LOW, the carry-out will trigger HIGH while the sum will be LOW.
The carry output can then be routed into the carry-in of an additional adder circuit to increase the capacity of the
Adder. Our 8-bit ALU will require an 8-bit adder which requires 8 of these circuits to be wired in series as demonstrated
in the next step.

8-bit ALU (Arithmetic Logic Unit): Page 4


Step 4: ALU Design

As mentioned in the step above, our 8-bit ALU requires an 8-bit adder for each of the 8 binary values. As such, the
design above demonstrates the con guration of 8 full adder circuits where the carry is passed on from each full adder
and into the next.
What about subtraction?
Our ALU should be able to add and subtract 8-bit numbers. However, so far we have only discussed addition. Luckily,
there is a relatively straight-forwarded solution to this. Subtracting numbers, say A - B, is the same as adding A + (-B).
Using two's-complement, a number system that allows us to represent negative numbers in binary, we can change B to -
B.
1. The method involves rst inverting all the numbers in B. Let B = 00011100 (28 in decimal):
2. With all digits inverted, we get: 11100011
3. Next, add 1:
4. adding one gives us: 11100100

As such, -B = 11100100 which is (-28 in decimal - two's complement)

Implementation
We need a way of inverting all B inputs on command to signal subtraction. An XOR gate allows us to do just that. If we
have 8 XOR gates and the rst input into all of them is the corresponding B digit and the second input was a wire that we
controlled to activate subtraction/addition, running the wire HIGH would invert the output of all gates (check the logic
table for XOR gates earlier). As for adding one, we can run this same subtraction/addition line into the very rst carry-in
input to the rst full adder circuit. When the line goes HIGH (indicating subtraction), it will add one through the carry-in.
This implementation can be seen in the diagram on the left.
The images on the right detail the di erent operations of addition vs subtraction. If you would like to play around with
this circuit yourself, the link to the circuit in Logisim can be found here.

8-bit ALU (Arithmetic Logic Unit): Page 5


8-bit ALU (Arithmetic Logic Unit): Page 6
Step 5: Scaled Down Prototype

Now that the design is complete, it's time to start testing the circuit components!
I would recommend creating a smaller 2-bit scaled-down version of the adder circuit. Keep in mind that each IC
(LS7408, LS7432, and CD4070) has 4 respective logic gates. Since one full adder circuit requires 2 AND gates, 2 XOR
gates, and 1 OR gate, one of each IC can be used to build at most a 2-bit adder circuit as shown on the breadboard
above. This setup will allow you to ensure that your understanding of the circuit is correct and that all your IC
components are working properly.

8-bit ALU (Arithmetic Logic Unit): Page 7


2 Bit Adder (output examples)

NUMBER 1 0 0 NUMBER 1 0 1 NUMBER 1 1 1

NUMBER 2 0 0 NUMBER 2 0 0 NUMBER 2 0 1

SUM 0 0 SUM 0 1 SUM 0 0

NUMBER 1 1 0 NUMBER 1 1 0 NUMBER 1 1 1

NUMBER 2 0 0 NUMBER 2 0 1 NUMBER 2 1 0

SUM 1 0 SUM 1 1 SUM 0 1

Step 6: Building the ALU

Building the full ALU can be a very tedious process, but it is incredibly important to keep things organized!
1. Start by getting familiar with the spacing on your perf board and position the ICs in the order to that
suits it best. I went with 4 duplicates of the test adder we will above. The other two ICS on the left is the
XOR gates that the B inputs run through.
8-bit ALU (Arithmetic Logic Unit): Page 8
2. Solder the ICs in place, ensuring correct orientation. If you have access to IC holders, use those! They
allow you to insert the ICs into the board after soldering is done. This protects them from the heat which
could potentially damage internal circuits. Be quick but precise with the soldering to avoid this.
3. Insert the 4x 4-way DIP switches along with the corresponding LEDs and solder the connections. Use
multicore wire to connect the switches to the LEDs. The other end of the DIP switches can all be soldered
together as a common VSS (5V source).
4. Proceed by making sure all ICs are grounded and supplied with 5V power. Use a distinct multicore wire
color to di erentiate these wires. It'll be very helpful during the debugging process.
5. Continue by con guring the 'subtraction line' connection 8 inputs of the XOR gates along with the carry
input of the rst full adder. Connect this to a PTM switch and a 10K resistor to GND (the other side
should be connected 5V).
6. Now start with all the adder connections. Everything from the inputs to the carry lines between the
adder circuits.
7. Finally, nish o by connecting the output from each adder circuit to the 8 nal LEDs (use 470-ohm
resistors to connect the other end to GND).

*Note about power: I supplied 5V and GND to the circuit from a 5V 1A adapter I found lying around. I cut o the end of
the wire and stripped it to nd the separate 5V and GND lines. I then attached these to the relevant locations on the
perf board.

8-bit ALU (Arithmetic Logic Unit): Page 9


8-bit ALU (Arithmetic Logic Unit): Page 10
Step 7: Debugging

When you nally nish the circuit, it is bound to have bugs and errors. You could have used a wire that isn't conductive
anymore, an LED that is burned out, or in the mess of all the solder, there could be a short between two pins. At this
8-bit ALU (Arithmetic Logic Unit): Page 11
stage of the process, it is incredibly helpful to have a multimeter. The primary functions that you will be using are the DC
voltage read and connectivity.
Problems that I encountered.
1. LEDs randomly turning on and o - make sure the circuit is suspended in the air or properly secured on
the table. Any angle or bend to the perf board can cause unintended shorts and grounding issues.
2. No connectivity between wires/pins - sometimes, especially without the use of ux, dry joints between
soldered joints can form due to the oxide layer that forms during soldering. Fix this by re-soldering the
joint. The issue may also be caused by a bad wire, be sure to check connectivity before soldering these
jumper cables.
3. Inconsistent outputs - ensure that all ICs are receiving 5V and GND at the necessary pins.
4. Floating voltage - these issues can generally be solved by securing a 10K resistor (pull-down resistor)
from the pin to GND. But this is a bandage x and often has other causes.

Debugging is often painful and tedious. If all else fails, follow the route of the current pin by pin until you discover the
short/issue. A good quality and performing multimeter is your best friend in these circumstances.

8-bit ALU (Arithmetic Logic Unit): Page 12


Step 8: Next Steps...

The ALU is just one component of a computer. If you enjoyed this project and wanted to take it further, you might want
to start making other parts of an 8-bit computer (RAM, registers, etc.). I'd recommend checking out this channel (Ben
Eater) for more on designing the rest of an 8-bit computer from scratch. Such a project cements the basics of computer
architecture which is both incredibly interesting and useful to learn about.
I hope you enjoyed the process of designing the building for this ALU :)

8-bit ALU (Arithmetic Logic Unit): Page 13


8-bit ALU (Arithmetic Logic Unit): Page 14

You might also like