4.1.6Computer Architecture (Deep)

ALU — operations, flags (zero, carry, overflow, negative)

2,352 words11 min readdifficulty · medium

1. What an ALU actually is


2. Two's complement — the foundation (derive it!)

Derivation from first principles. In nn bits we have 2n2^n patterns. We want x-x to satisfy x+(x)0(mod2n)x + (-x) \equiv 0 \pmod{2^n}. So: x2nx(mod2n)-x \equiv 2^n - x \pmod{2^n} Now 2nx=(2n1)all onesx+1=( ⁣x)+12^n - x = \underbrace{(2^n - 1)}_{\text{all ones}} - x + 1 = (\sim\! x) + 1.

That's the famous rule: invert all bits and add 1.


3. Deriving each flag from first principles

3.1 Zero flag — Z

3.2 Negative flag — N

3.3 Carry flag — C (the unsigned story)

Think of an nn-bit adder as full-adders chained, each passing a carry. The carry out of the most significant full adder is coutc_{out}.

3.4 Overflow flag — V (the signed story)

Derivation. Signed overflow happens iff the carry into the sign bit differs from the carry out of the sign bit: V=cncn1V = c_{n} \oplus c_{n-1} Equivalently, in terms of sign bits (aa, bb = sign of operands, ss = sign of result), for addition: V=(abs)(abs)V = (a \wedge b \wedge \overline{s}) \vee (\overline{a} \wedge \overline{b} \wedge s) Read it: overflow only when both operands share a sign and the result's sign flips. (pos+pos→neg, or neg+neg→pos). You can never overflow adding numbers of opposite sign.

Figure — ALU — operations, flags (zero, carry, overflow, negative)

4. Worked examples (8-bit, n=8n=8, range unsigned 0..2550..255, signed 128..127-128..127)


5. Logic & shift operations (flags differ!)

Op R Z N C V
ADD A+BA+B result=0 MSB carry-out cncn1c_n\oplus c_{n-1}
SUB ABA-B result=0 MSB NOT borrow sign rule
AND/OR/XOR logic result=0 MSB 0 0
LSL (shift left) A1A\ll1 result=0 MSB bit shifted out

6. Common mistakes (Steel-man + fix)


Recall Feynman: explain to a 12-year-old

Imagine an 8-slot counter that only holds numbers 0–255. The ALU is a robot that adds and subtracts in these slots. After every sum it raises little flags: a "it's zero!" flag, a "the top slot's lit up (looks negative)" flag, a "the answer didn't fit, it spilled over the top" carry flag, and a special "if we pretend half the numbers are negative, the sign came out wrong" overflow flag. The same robot does subtraction by flipping the second number and adding one — like walking backwards by turning around and walking forward.


Connections


Flashcards

What does the ALU output besides the result?
A set of condition flags (Z, C, N, V).
Define the Zero flag and its hardware.
Z=1 iff result is all zeros; implemented as a NOR over all result bits.
What is the Negative flag equal to?
The MSB (sign bit) of the result, rn1r_{n-1}.
Carry flag answers which range question?
Whether an UNSIGNED operation exceeded the n-bit range (carry/borrow out of the MSB).
Overflow flag answers which range question?
Whether a SIGNED operation left [2n1,2n11][-2^{n-1}, 2^{n-1}-1].
Derive overflow in terms of carries.
V=cncn1V = c_n \oplus c_{n-1} (carry into sign bit XOR carry out of sign bit).
How does the ALU subtract using an adder?
AB=A+(B)+1A - B = A + (\sim B) + 1, feeding carry-in = 1.
Why is x+1=x\sim x + 1 = -x?
Because x2nx=(2n1x)+1=x+1(mod2n)-x \equiv 2^n - x = (2^n-1 - x) + 1 = \sim x + 1 \pmod{2^n}.
Give a case with carry but no overflow.
255+1 (8-bit): C=1, V=0, result 0 (correct signed -1+1).
Give a case with overflow but no carry.
127+1 (8-bit): C=0, V=1, result -128 (signed wrap).
When can signed addition NOT overflow?
When the operands have opposite signs.
On subtraction, what does C=0 mean (ARM/x86)?
A borrow occurred, i.e. A < B (unsigned).
What flags do logic ops (AND/OR/XOR) typically set?
Z and N from the result; C and V are cleared.
Where does the carry come from on a left shift?
The bit shifted out of the MSB becomes the carry flag.

Concept Map

fed into

produces

produces

includes

includes

includes

includes

all bits zero sets

MSB copied to

invert bits add 1 enables

reuses one adder in

unsigned carry-out of MSB

signed result too big

ALU combinational circuit

Operands A B plus control code

n-bit result R

Condition flags

Two's complement

Subtraction A + notB + 1

Zero flag Z

Negative flag N

Carry flag C

Overflow flag V

Hinglish (regional understanding)

Intuition Hinglish mein samjho

ALU matlab CPU ke andar ka calculator. Tum do binary numbers do aur ek operation (add, sub, AND, OR) chuno — ALU result deta hai PLUS chaar chhote flags: Zero, Carry, Negative, Overflow. Ye flags CPU ko extra info dete hain jo akela result nahi bata sakta. Jaise agar 8-bit me answer fit hi nahi hua, to Carry flag wo "spill" wala bit yaad rakhta hai.

Sabse important confusion: Carry vs Overflow. Carry tab set hota hai jab unsigned number range se bahar gaya (jaise 255+1 = 256, 8-bit me fit nahi). Overflow tab set hota hai jab signed math galat ho gaya — jaise 127+1, signed me answer -128 aa gaya, jo galat hai. Dono alag-alag sawaal puchte hain, isliye ek set ho sakta hai aur dusra nahi. Rule yaad rakho: same sign waale do numbers jodo aur result ka sign palat jaye = overflow. Opposite sign me overflow kabhi nahi hota.

Subtraction ka jugaad: ALU alag se subtract nahi karta, wo AB=A+(B)+1A - B = A + (\sim B) + 1 karta hai — yaani B ke saare bits ulto aur 1 jodo (ye two's complement me B-B hai), carry-in = 1 daal do. Negative flag bas result ka top bit (MSB) hota hai. Zero flag tab 1 jab saare bits zero hon.

Exam aur real coding dono me ye kaam aata hai: jab tum if (a >= b) likhte ho, CPU andar Carry/Overflow flags dekh kar branch leta hai — unsigned compare me Carry, signed compare me Negative aur Overflow ka combination. Isliye flags samajhna = conditional branches samajhna.

Go deeper — visual, from zero

Test yourself — Computer Architecture (Deep)

Connections