In the previous lessons, we learned how logic gates can perform simple operations.
We then built a half adder, which can add two binary bits.
After that, we introduced the full adder, which can add two bits together with an incoming carry.
But a CPU needs to do much more than add individual bits.
It must perform operations such as:
- addition
- subtraction
- AND
- OR
- XOR
- comparisons
The part of the CPU responsible for many of these operations is called the ALU.
ALU stands for:
Arithmetic Logic Unit
It is one of the most important building blocks inside a processor.
1. What Does an ALU Do?
At the simplest level, an ALU receives binary values, performs an operation, and produces a result.
Conceptually:
Input A
│
▼
┌─────────┐
│ │
│ ALU │────► Result
│ │
└─────────┘
▲
│
Input B
But the ALU also needs to know which operation to perform.
Should it add?
Should it subtract?
Should it perform AND?
Should it compare two numbers?
That information comes from control signals.
A more complete picture looks like this:
A B
│ │
▼ ▼
┌──────────────┐
│ │
│ ALU │────► Result
│ │
└──────▲───────┘
│
ALU Control
The same two input values can therefore produce different results depending on the control signal.
2. Arithmetic and Logic
The name Arithmetic Logic Unit tells us that the ALU performs two major types of operations.
Arithmetic operations
These include operations such as:
A + B
A - B
Logic operations
These include:
A AND B
A OR B
A XOR B
Some ALUs also perform comparisons and other simple operations.
So conceptually, we can imagine an ALU containing several possible functions:
A B
│ │
▼ ▼
┌─────────────────┐
│ ALU │
│ │
│ ADD │
│ SUB │
│ AND │
│ OR │
│ XOR │
│ COMPARE │
└────────┬────────┘
│
▼
Result
The processor chooses which function it needs.
3. Addition Inside the ALU
We already know the basic idea behind binary addition.
A full adder accepts:
A
B
Carry-in
and produces:
Sum
Carry-out
Real processors operate on numbers containing many bits.
For example:
32-bit
64-bit
So the addition circuitry inside an ALU must operate across all of those bits.
At this stage, however, we do not need to study the detailed internal adder design.
The important idea is simply:
the ALU contains hardware capable of performing binary addition.
For example:
A = 5
B = 3
In binary:
0101
0011
The ALU performs:
0101
+
0011
----
1000
So:
5 + 3 = 8
This operation happens in digital hardware.
The ALU is not running a software program to calculate the answer.
4. Subtraction Inside the ALU
An ALU can also subtract.
For example:
7 - 3 = 4
Modern computers usually represent negative integers using two’s complement.
This allows subtraction to be transformed into addition.
Instead of building a completely separate arithmetic system for subtraction, the processor can conceptually calculate:
A - B
as:
A + (-B)
For example:
7 - 3
can be treated as:
7 + (-3)
This is one reason two’s complement is so important in computer architecture.
The same general arithmetic hardware can support both positive and negative calculations.
5. AND Operation
The ALU also performs Boolean logic.
Consider the AND operation.
For each bit:
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1
Suppose:
A = 1100
B = 1010
Then:
1100
AND
1010
------
1000
The operation is performed independently on each pair of bits.
AND is commonly useful for operations such as masking selected bits.
6. OR Operation
The OR operation produces 1 when at least one input bit is 1.
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
For example:
A = 1100
B = 1010
Then:
1100
OR
1010
----
1110
Again, the operation happens bit by bit.
7. XOR Operation
XOR means exclusive OR.
Its output is 1 when the two input bits are different.
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0
Using the same values:
A = 1100
B = 1010
we get:
1100
XOR
1010
-----
0110
XOR appears in many areas of computer hardware.
We have already seen one important example:
the Sum output of a half adder uses XOR.
8. How Does the ALU Know What to Do?
Suppose the ALU receives:
A = 5
B = 3
What should it calculate?
It could produce:
5 + 3
or:
5 - 3
or perform a logical operation.
The input values alone do not tell the ALU what operation is required.
The processor therefore provides an additional control signal.
Conceptually:
A ───────────┐
│
B ───────────┼──► ALU ───► Result
│
ALU Control ─┘
Different control values select different operations.
For example, conceptually:
Control Operation
000 AND
001 OR
010 ADD
110 SUB
The exact encoding depends on the processor design.
The important point is:
the control signal selects the ALU operation.
9. Where Do the ALU Inputs Come From?
In many CPU operations, the ALU receives values from registers.
For example:
Register A ─────┐
│
▼
┌─────┐
│ ALU │────► Result
└─────┘
▲
│
Register B ─────┘
Suppose the CPU wants to calculate:
R1 = R2 + R3
The processor can:
- read the value stored in
R2 - read the value stored in
R3 - send both values to the ALU
- tell the ALU to perform ADD
- write the result into
R1
Conceptually:
R2 ─────┐
│
▼
┌─────┐
│ ADD │────► R1
└─────┘
▲
│
R3 ─────┘
This connects the ALU directly to something we studied earlier: the register file.
10. The ALU and the Register File
The relationship can be shown more clearly:
Register File
┌─────────────┐
│ │
│ Read A ───┼────┐
│ │ │
│ Read B ───┼──┐ │
│ │ │ │
└─────────────┘ │ │
▼ ▼
┌─────┐
│ ALU │
└──┬──┘
│
▼
Result
The registers provide values.
The ALU processes them.
The result can then be written back into a register.
This creates one of the fundamental data paths inside a CPU:
Registers
│
▼
ALU
│
▼
Registers
The CPU repeatedly moves data through structures like this while executing instructions.
11. Example: An ADD Instruction
Consider an instruction conceptually similar to:
ADD R1, R2, R3
Its meaning is:
R1 = R2 + R3
Suppose:
R2 = 5
R3 = 3
The CPU reads:
5
3
from the register file.
They enter the ALU:
5 ──────┐
▼
┌─────┐
│ ALU │────► 8
└─────┘
▲
3 ──────┘
The control circuitry tells the ALU:
ADD
The ALU produces:
8
The processor can then write:
R1 = 8
This is the basic connection between a machine instruction and actual arithmetic hardware.
12. Example: An AND Instruction
Now imagine:
AND R1, R2, R3
The register file still supplies two values.
But this time the control circuitry tells the ALU:
AND
Instead of adding the inputs, the ALU performs bitwise AND.
So the same general data path can execute many different operations:
Registers
│
▼
ALU
│
▼
Registers
What changes is the operation selected by the control circuitry.
13. Comparisons
ALUs can also help the CPU compare numbers.
For example, a processor may need to determine whether:
A == B
or:
A < B
or:
A > B
Comparisons are especially important for instructions involving branches.
Consider:
if A == B
The processor needs hardware to determine whether the condition is true.
The ALU can participate in making this decision.
This connects arithmetic hardware to program control flow.
14. ALU Status Outputs
An ALU may produce more than just the main numerical result.
It can also produce status information.
Common examples include:
Zero
Carry
Overflow
Negative
For example, a Zero output may indicate:
Result = 0
If the CPU calculates:
A - B
and the result is zero, then:
A = B
That information can help the processor make decisions for branch instructions.
Different CPU architectures handle status information differently, so we do not need to go deeply into flags yet.
The important idea is that the ALU may provide both:
Result
+
Status information
15. The ALU Is Not the Entire CPU
The ALU is extremely important, but it is only one part of the processor.
A CPU also contains structures such as:
- registers
- control logic
- instruction decoding circuitry
- caches
- buses and internal data paths
Conceptually:
CPU
┌─────────────────────┐
│ │
│ Registers │
│ │ │
│ ▼ │
│ ALU │
│ │
│ Control Logic │
│ │
│ Other Hardware │
│ │
└─────────────────────┘
The ALU performs operations.
But other parts of the CPU decide:
- where the input values come from
- which ALU operation should run
- where the result should go
- which instruction executes next
16. From Logic Gates to the ALU
We can now connect several ideas from our earlier lessons.
We started with very simple logic gates:
AND
OR
XOR
NOT
Then we saw that gates can form arithmetic circuits:
Logic Gates
│
▼
Half Adder
│
▼
Full Adder
And now we can move one level higher:
Logic Gates
│
▼
Arithmetic Circuits
│
▼
ALU
│
▼
CPU
This is an important idea.
A processor may execute billions of operations, but underneath that complexity are digital circuits built from very simple logical components.
17. Conclusion
The Arithmetic Logic Unit, or ALU, is the part of the CPU that performs many arithmetic and logical operations.
Typical ALU operations include:
ADD
SUB
AND
OR
XOR
COMPARE
The ALU receives input values, often from CPU registers.
Control signals tell the ALU which operation to perform.
The ALU then produces a result and may also generate status information such as Zero, Carry, or Overflow.
Conceptually:
Registers
│
▼
ALU
│
▼
Result
│
▼
Registers
This gives us an important bridge between the digital circuits we have already studied and the operation of a real processor.
We began with logic gates.
Those gates allowed us to build adders.
And arithmetic and logic circuits come together inside the ALU.
The next important question is:
How does the CPU tell the ALU which operation to perform?
That takes us from the ALU to another fundamental part of the processor:
CPU control and instruction decoding.
