Arithmetic Logic Unit (ALU)
The ALU is the workhorse of the CPU - it's where all the actual computation happens! Every calculation your computer performs, from simple addition to complex graphics rendering, passes through the ALU.
ALU Operations
An ALU performs two main types of operations:
ALU Operations
+---------------------------------------------+
| |
| Arithmetic Operations: |
| +---------------------------------------+ |
| | ADD | SUB | MUL | DIV | INC/DEC | |
| +---------------------------------------+ |
| |
| Logic Operations: |
| +---------------------------------------+ |
| | AND | OR | XOR | NOT | SHIFT | |
| +---------------------------------------+ |
| |
| Comparison Operations: |
| +---------------------------------------+ |
| | CMP | GT | LT | EQ | NEQ | |
| +---------------------------------------+ |
+---------------------------------------------+
ALU Structure
A simplified ALU has this structure:
ALU Block Diagram
+---------------------------------------------+
| |
| Input A (from registers) |
| | |
| v |
| +-------------+ |
| | | |
| | Operand | |
| | Selection | |
| | | |
| +------+------+ |
| | |
| v |
| +-------------+ |
| | | |
| | Operation |<--- Control Signals |
| | Unit | |
| | | |
| +------+------+ |
| | |
| v |
| +-------------+ |
| | | |
| | Result |-----> Output |
| | | (to registers) |
| +-------------+ |
| | |
| v |
| +-------------+ |
| | Flags |-----> Status Register |
| | (Zero, Carry| |
| | Overflow) | |
| +-------------+ |
+---------------------------------------------+
The Importance of Flags
After each operation, the ALU sets status flags that tell us about the result:
- Zero Flag (Z): Set if result is zero
- Carry Flag (C): Set if there's a carry out of the most significant bit
- Overflow Flag (V): Set if signed result doesn't fit in the register
- Negative Flag (N): Set if result is negative
These flags are crucial for conditional branching - making decisions in code!
ALU in Modern Processors
Modern CPUs contain multiple ALUs that can operate in parallel. They also include specialized ALUs for:
- Floating-point operations (FPU)
- SIMD operations (processing multiple data elements at once)
- Cryptography operations (AES encryption)
- Vector processing (graphics and scientific computing)