Processor Status Flags

The arithmetic-logic unit (ALU) computes brilliantly, but that alone isn't enough to make games. A game constantly needs to check conditions: are the lives gone? Did the snake hit a wall? Did the counter reach zero? And the gamepad you learned to read in the article about memory-mapped I/O is useless without checks: reading a button is not enough — you have to decide which way to move the character.

To let the processor make such decisions, the ALU is equipped with special signal wires — status flags. Think of them as signal flares the ALU fires after every arithmetic operation.

The Ershov Computer has two flags: Zero and Negative. No hidden registers — they're plain wires that you will connect yourself.

Status register 7 6 5 4 3 2 1 0 N Z N = 1 when the result is negative Z = 1 when the result is zero
The status register: one bit per fact about the last result — N (negative) and Z (zero) matter most for jumps

The Zero flag: the result is zero

Zero lights up (becomes 1) if the result of the last calculation was exactly zero. In hardware it's produced by the BusZero zero-detector: it checks all 8 bits of the result, and if they're all 0, it outputs 1.

This is the perfect way to check whether two numbers are equal: just subtract one from the other — if the Zero flag lights up, they're the same.

The Negative flag: the result went negative

Negative lights up when the most significant bit (bit 7) of the result is 1. In two's complement that means a negative number. Say we're checking a player's coordinate: subtract the left edge of the field from it, and if the result goes negative — the Negative flag is set, and the processor instantly knows the player flew off the field.

Conditional jumps: JZ and JN

Flags are useless unless something looks at them. That's why the instruction set has conditional jumps: JZ (Jump if Zero) and JN (Jump if Negative). We'll cover the full instruction set in the article about assembly — here we focus on these two.

FlagEquals 1 whenInstructionExample from the game
ZeroResult = 0JZthe lives counter hits zero
NegativeBit 7 = 1 (minus)JNa coordinate goes negative

The hardware behind a conditional jump

How is a conditional jump built from gates? Take JN — a chain of three elements. A Splitter extracts bit 7 from the ALU output (the Negative flag). An AND gate combines the decoder's JumpCond signal with that bit. An OR gate merges the result with the JumpUncond signal (unconditional jump). The output is the PC.Load signal.

If PC.Load = 1, the program counter loads the jump address from the operand instead of simply incrementing. If the condition isn't met — PC.Load = 0, and execution continues in order.

Subtraction in numbers: 5 − 10 = 251

Let's trace the test from level 2.25. 5 − 10 = −5. In two's complement, −5 is 251, and 251 = 0b11111011. The most significant bit (bit 7) is 1 — so the Negative flag is set.

The test program:

LDA 10     ; Acc = 10
STA 10     ; RAM[10] = 10
LDA 5      ; Acc = 5
SUB 10     ; Acc = 5 - RAM[10] = 5 - 10 = 251
JN neg     ; Negative = 1 (bit 7) — jump to neg
HLT        ; if JN didn't fire, Acc stays 251
neg:
LDA 99     ; Acc = 99
HLT        ; stop

Step by step: 1) ten is stored in RAM[10]; 2) the accumulator (5) subtracts RAM[10] — the result is 251; 3) bit 7 of 251 is 1, so the Negative flag is set; 4) JN moves the program counter to the neg label; 5) the accumulator becomes 99. If JN had failed, it would stay 251 — and the level checkpoint will catch that.

Lives counter: JZ and Game Over

Back to the lives example. The lives counter is 3. Each death subtracts one: 3 → 2 → 1 → 0. As soon as the result hits zero, the Zero flag is 1, and the JZ instruction sends the program to the "Game Over" screen. Until zero is reached, the program jumps back to the start of the level.

Without flags the processor couldn't compare numbers or make decisions — every program would be linear and useless.

Common mistakes

1. Thinking flags are a separate memory: in fact they're plain wires coming out of the ALU.

2. Forgetting that JZ and JN watch the latest ALU result: no other operations may sit between the calculation and the jump.

3. Treating Negative "on paper" as "less than zero": in hardware it's simply bit 7.

Summary

1. The Zero flag is 1 when the result is 0; it's produced by the BusZero detector.

2. The Negative flag is 1 when bit 7 of the result is 1 — a negative number in two's complement.

3. JZ jumps if Zero = 1; JN jumps if Negative = 1.

4. In hardware a conditional jump is AND (JumpCond and flag) followed by OR (JumpUncond) feeding PC.Load.

5. 5 − 10 = −5 = 251 = 0b11111011: bit 7 = 1, so Negative is set.

In level 2.25 you will build the JN conditional-jump circuit: extract bit 7 from the ALU output with a Splitter and combine it with the decoder signals so the program counter loads the jump address. After that your processor learns to make decisions.

Try it in the simulator →