ALU
Definition
The Arithmetic Logic Unit (ALU) is a digital circuit within the CPU that performs arithmetic operations such as addition, subtraction, multiplication, and division, as well as logical operations such as AND, OR, NOT, XOR, comparisons, and bit shifting.
It receives operands from registers, performs the operation selected by control signals, and outputs the result back to registers or memory. The ALU does not decide what operation to perform on its own; it works according to instructions from the control unit.
Main Content
1. First Concept: Arithmetic Operations
The arithmetic function of the ALU deals with numerical calculations. These are the operations that manipulate numbers directly and are fundamental to almost every program.
Addition and subtraction
- The ALU can add two binary numbers using circuits such as half adders and full adders.
- Subtraction is often performed by converting the problem into addition using two’s complement representation.
- Example: If the CPU needs to calculate
5 + 3, the ALU converts the values into binary and produces8as the result.
Multiplication and division
- In many processors, multiplication and division may be handled by the ALU or by a specialized unit closely related to it.
- These operations are generally more complex than addition and subtraction because they may require repeated addition, shifting, or iterative algorithms.
- Example: A processor computing
12 × 4may use shift-and-add techniques internally.
Increment and decrement
- The ALU can increase or decrease values by 1.
- These operations are heavily used in loops, counters, and address calculations.
- Example: A loop counter in a program often needs to be incremented after each iteration.
2. Second Concept: Logical Operations
Logical operations allow the ALU to work with binary bits according to Boolean algebra. These operations are essential for decision-making and digital control.
AND, OR, NOT, XOR
- AND returns 1 only when both input bits are 1.
- OR returns 1 when at least one input bit is 1.
- NOT inverts a bit from 0 to 1 or 1 to 0.
- XOR returns 1 when the input bits are different.
- These operations are used in masking, setting, clearing, and toggling bits.
Bit comparison
- The ALU compares binary values to determine equality or order.
- Comparisons such as equal to, greater than, and less than are used in conditional statements and branching.
- Example: In the expression
if A > B, the ALU compares two values and helps the control unit decide which instruction to execute next.
Bit manipulation
- Logical operations help isolate specific bits, check flags, and modify data efficiently.
- Example: A program may use AND with a mask like
00001111to keep only the lower four bits of a byte.
3. Third Concept: Structure and Internal Components
The ALU is not a single simple block; it is made of several parts working together to carry out operations correctly and quickly.
Inputs and outputs
- The ALU receives operands from registers, typically two input values.
- It produces an output result and may also generate status flags.
- The output is often stored back into a register for later use.
Control lines
- Control signals tell the ALU what operation to perform.
- For example, one set of control bits may indicate addition, another may indicate subtraction, and another may indicate AND or OR.
- These signals come from the control unit of the CPU.
Status flags
- The ALU often sets flags that describe the result of an operation.
- Common flags include:
- Zero flag: result is zero
- Carry flag: overflow occurred in unsigned arithmetic
- Sign flag: result is negative
- Overflow flag: signed result is too large for the available bits
- These flags are important in branching and decision-making.
A simple functional view of the ALU:
Control Unit
|
v
+-----------------------+
A-> | | -> Result
B-> | ALU | -> Flags
| |
+-----------------------+
This shows that the ALU takes input values, follows control signals, and produces both the computed result and condition flags.
Working / Process
1. Operands are fetched
- The CPU first obtains the data values that need to be processed.
- These values usually come from registers, which are very fast storage locations inside the processor.
- Example: If a program needs to add two numbers, those numbers are loaded into ALU input registers.
2. Control unit selects the operation
- The control unit decodes the instruction and sends appropriate control signals to the ALU.
- These signals determine whether the ALU should add, subtract, compare, perform AND, shift bits, or carry out another operation.
- This ensures the ALU performs exactly the required function.
3. ALU performs the operation and stores the result
- The ALU processes the inputs based on the selected operation.
- It outputs the result and may update status flags.
- The result is then sent back to a register or memory location for future use.
Advantages / Applications
Fast data processing
- The ALU allows the CPU to perform calculations and logical operations at extremely high speed.
- This speed is essential for running modern software efficiently.
Supports decision-making in programs
- Comparisons and logical operations performed by the ALU help the CPU make decisions in loops, conditions, and branching instructions.
- Without the ALU, a processor could not evaluate whether one value is greater than another or whether a condition is true.
Used in almost every digital system
- ALUs are found not only in computers but also in smartphones, embedded systems, calculators, routers, and many other devices.
- Any device that processes binary data depends on ALU-like operations.
Summary
- The ALU is the part of the CPU that performs arithmetic and logic operations.
- It works with binary data using control signals from the control unit.
- It is essential for calculations, comparisons, and decision-making in computers.
- Important terms to remember: ALU, arithmetic operations, logic operations, control unit, flags