4.1.6 · D3Computer Architecture (Deep)

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

3,413 words16 min readBack to topic

The four flags in one breath (so no symbol is unearned)

Before the matrix, let us re-anchor the four flags in plain words + one picture so nothing below uses a symbol you haven't met.

Picture the 8 bits as 8 boxes in a row, numbered from the right: box 0, box 1, … up to box 7 on the far left. Box 7 is special — it is the top box (MSB).

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

The scenario matrix

Every ALU flag puzzle is one of these cells. The goal: hit all of them below.

# Cell (the situation) What's being tested Example
A Add, both small positives clean case, all flags calm Ex 1
B Add, unsigned carry but signed is correct C=1, V=0 (independent!) Ex 2
C Add, signed overflow but no carry C=0, V=1 (independent!) Ex 3
D Add, negatives that overflow (neg+neg→pos) C=1 and V=1 together Ex 4
E Subtract, (borrow / negative result) subtract carry = borrow logic Ex 5
F Subtract, , result positive subtract carry = "no borrow" Ex 6
G Subtract equal values → zero (degenerate) Z=1 on subtract, Ex 7
H Logic op (AND / XOR) — flags behave differently C and V forced to 0 Ex 8
I Shift left — the "carry catches the falling bit" C from shifted-out bit Ex 9
J Real-world + exam twist (sensor wraparound) apply flags to a decision Ex 10

Ten cells, ten examples. Every numeric answer at the bottom is machine-checked.


Example 1 — Cell A: the calm case


Example 2 — Cell B: unsigned carry, signed is correct


Example 3 — Cell C: signed overflow, no carry


Example 4 — Cell D: negatives that overflow (C=1 and V=1)


Example 5 — Cell E: subtract with a borrow ()

First, let us build the subtract-carry rule so no symbol is unearned.

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

Example 6 — Cell F: subtract, , positive result


Example 7 — Cell G: degenerate — subtract equal values → zero


Example 8 — Cell H: logic op forces C and V to 0


Example 9 — Cell I: shift left, carry catches the falling bit


Example 10 — Cell J: real-world + exam twist


Matrix coverage check

Recall Did we hit every cell?

A→Ex1, B→Ex2, C→Ex3, D→Ex4, E→Ex5, F→Ex6, G→Ex7, H→Ex8, I→Ex9, J→Ex10. Signs covered: pos+pos, neg+neg, pos−pos both directions, mixed. Degenerate zero covered. Logic and shift (where C/V change meaning) covered. Real-world wraparound covered. ✓

Recall When two examples have identical bits but different flags, what decides?

The interpretation you chose (signed vs unsigned) ::: The bits and all four flags are computed the same way every time; you pick for unsigned decisions and for signed decisions. The ALU never assumes.

Given , , 8-bit ADD: which flag is set and which is clear?
(signed overflow), (no unsigned carry).
On a subtract, what does tell you about and ?
A borrow happened, i.e. (unsigned).
For XOR, why are and forced to 0?
No bit crosses a box boundary in a logic op, so carry and signed-overflow are meaningless.

Connections