The ALU: the CPU's calculator
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
The arithmetic logic unit (ALU) does a CPU's maths and logic. Inside, an adder, an AND unit and an OR unit all work on the same two inputs at once, and a multiplexer picks one answer using a 2-bit operation code. Step a 4-bit ALU through ADD, SUB, AND and OR on A = 0101 and B = 0011, see how one adder also subtracts using two's complement, and read the four status flags: zero, negative, carry and overflow.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01In this ALU, what decides which unit's answer reaches the output?
All three units work at the same time. The operation code (00 ADD, 01 SUB, 10 AND, 11 OR) tells the mux which one result to pass on.
02How does the ALU work out 5 − 3 with its adder?
In two's complement −3 is NOT 3 plus 1, so 5 − 3 = 5 + (NOT 3) + 1 = 0101 + 1100 + 1 = 1 0010. Keeping four bits gives 0010 = 2.
03What is 0101 AND 0011?
AND gives a 1 only where both inputs have a 1. Only the rightmost column has two 1s, so the answer is 0001.
04After subtracting 5 − 5, which flag is set?
0101 − 0101 = 0000. Z is 1 when every result bit is 0, which is how a CPU checks that two numbers are equal.
05A 4-bit ALU adds 0101 (5) and 0011 (3) and gets 1000. Why does the overflow flag V light?
Signed 4-bit numbers run from −8 to 7. 5 + 3 = 8 does not fit, so the top bit flips and 1000 reads as −8. V flags this signed overflow; the carry flag stays 0.
المسرد
- ALU
- Arithmetic logic unit: the part of a CPU that does arithmetic, such as adding and subtracting, and logic, such as AND and OR, on binary numbers.
- Register
- A small, fast store inside the CPU that holds one number, a row of bits.
- Multiplexer (mux)
- A switch made of gates that passes exactly one of its inputs to its output, chosen by select bits.
- Operation code
- The bits that tell the ALU which operation to do. Here two bits: 00 ADD, 01 SUB, 10 AND, 11 OR.
- Two's complement
- The usual way computers store negative numbers: to negate, invert every bit and add 1. Four bits then hold −8 to 7.
- Status flags
- Single bits the ALU sets about its result: N (negative), Z (zero), C (carry) and V (overflow).
- Overflow
- When the true signed result does not fit in the available bits, so the stored bits have the wrong sign.
الملاحظات والمصادر
- The textbook ALU (Harris and Harris, Digital Design and Computer Architecture: ARM Edition, section 5.2.4) has an adder, an AND unit and an OR unit working in parallel, and a 4-input multiplexer that picks one result with a 2-bit control code: 00 ADD, 01 SUB, 10 AND, 11 OR. Real ALUs add more operations, such as XOR, shifts and set-less-than.
- In two's complement an N-bit number holds −2^(N−1) to 2^(N−1) − 1, so 4 bits hold −8 to 7, and you negate a number by inverting its bits and adding 1. That is why A − B = A + (NOT B) + 1, and one adder can both add and subtract (Harris and Harris, section 1.4.6).
- N is the top bit of the result and Z is 1 when every result bit is 0. C (the adder's carry-out) and V (signed overflow: both inputs have the same sign and the sum has the other) are only set by ADD and SUB; AND and OR clear them in this ALU (Harris and Harris, figure 5.17).
- After a subtraction, Arm sets C to 1 when no borrow happened and to 0 when one did, which is the adder's carry-out. Some other processors, such as x86, store the opposite (a borrow flag), so check the convention of the CPU you use.
- A real 4-bit ALU on one chip is the 74181 (TI SN74LS181): four select inputs and a mode input choose among 16 logic and 16 arithmetic functions of two 4-bit words.
- In Python, ~b & 0b1111 flips the four low bits of b, and &, | work bit by bit on whole numbers. Python's own integers never overflow; the 4-bit limit here is the width of the circuit.
- In a chip each bit is a high or low voltage on a wire; here a glowing lamp shows a 1. A lit wire with moving dashes is carrying the value the step is about.
- Harris and Harris, Digital Design and Computer Architecture: ARM Edition (sections 1.4.6 and 5.2.4)
- Nisan and Schocken, Nand2Tetris: Boolean arithmetic and the ALU
- Texas Instruments SN54LS181 / SN74LS181 datasheet: 4-bit arithmetic logic unit
- Arm Compiler armasm User Guide: condition flags
- MIT OpenCourseWare 6.004 Computation Structures
- Python reference: binary bitwise operations
اختبار يتاح بعد الخطوة الأخيرة.