DYSTOPIANPIXEL

Flags and operations

Distinguish Carry, Zero and Overflow and learn when flags change.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 2 / PART 03

Flags describe an operation, not a permanent property

The flags live in the eight-bit F register. Tutorial 1 introduced them in the Odin AF display. We now need to understand the common ones more precisely.

How to read the eight bits of F

The flags register is one byte, so its bits are numbered 7 down to 0 just like any other byte. Each named flag has a fixed position. A bit that is 1 contributes its place value to the hex number printed for F; a bit that is 0 contributes nothing.

BitFlagHex mask
7S$80
6Z$40
5—$20
4H$10
3—$08
2P/V$04
1N$02
0C$01
7 — $80S (Sign)Copies bit 7 of an arithmetic result: 1 means its high bit is set.
6 — $40Z (Zero)Set when the relevant operation gives zero; CP sets it for equality.
5 — $20UndocumentedHas observable behaviour, but is not a documented condition for normal branches.
4 — $10H (Half-carry)Indicates a carry or borrow between the lower and upper nibbles.
3 — $08UndocumentedDo not use it as an ordinary decision flag in this course.
2 — $04P/VParity for certain operations, signed overflow for arithmetic / CP.
1 — $02NSet for subtract-style operations such as CP; cleared for ADD.
0 — $01C (Carry)An unsigned carry on addition or a borrow on subtraction / CP.

The P/V bit is one physical flag with two documented meanings, selected by the kind of instruction. It does not independently store both parity and overflow. For example, after CP it indicates signed arithmetic overflow, not the parity of A.

Build a flags byte by hand

Recall the example from Tutorial 1. If A starts at $FF, then ADD A,$01 wraps the eight-bit result to $00. Zero is set ($40), Half-carry is set ($10), and Carry is set ($01). N is cleared because we added rather than subtracted. The other named result flags are clear.

    ld a,$ff
    add a,$01

Result: A = $00
F = $40 + $10 + $01 = $51
AF = $0051 (expected on the Z80)

You can check the arithmetic yourself: binary F = 0101 0001. Split that into nibbles, 0101 and 0001, and you get hexadecimal $51. Later, the Odin inspection screen displays AF as a pair of bytes: the first two hex digits are A and the last two are F.

A note about bits 5 and 3: these are sometimes called undocumented X/Y flags. They can change and make F appear more complicated than the named flags alone. Their precise behaviour varies by instruction. For these beginner exercises, use the documented flags to make decisions rather than trying to interpret every bit of F.

Z — ZeroAn operation produced zero; for CP, the values compared were equal.$12 compared with $12
C — CarryAddition produced a carry beyond bit 7, or subtraction/comparison required a borrow.$10 compared with $20 sets C
S — SignReflects bit 7 of an arithmetic or comparison result.Result $80 has S set
P/V — Parity/OverflowDepending on the instruction, reports parity or signed overflow.$7F + $01 overflows signed range
N — Add/SubtractSet for subtraction-type operations, including CP.CP $10 sets N
H — Half-carryReports carry/borrow involving the low nibble; useful for certain arithmetic.Important to correct CPU flag behaviour, not our first branches

Technical reference: For the complete flag rules, consult Zilog’s Z80 CPU User Manual (UM0080): https://www.zilog.com/docs/z80/um0080.pdf. All Odin outputs in this chapter remain to be checked on the target Next.

Two undocumented flag bits can also appear in the numerical value of F. Do not assume every hex digit in AF is a meaningful result you deliberately arranged. At this stage, look at the named flags that the instruction is documented to change.

Carry is not signed overflow

Take the addition $7F + $01. As unsigned values, that is 127 + 1 = 128, which fits in eight bits: no carry beyond bit 7. As signed values, +127 + 1 should give +128, which is outside the signed eight-bit range. The stored byte becomes $80, interpreted as −128; the overflow flag indicates the signed problem.

Therefore, C and P/V tell us different stories. Later, we must not use a simple unsigned Carry test as if it were a complete signed comparison.

Flags can be overwritten

One operation may set the flags and a later operation may replace them before a jump has checked them. For example, CP $10 updates flags; an INC A inserted before a conditional jump will update several of them again. By contrast, ordinary LD instructions do not update the arithmetic flags. Always ask: which instruction most recently established the flag I am testing?