DYSTOPIANPIXEL

Understanding flags

Read the individual flags in F and check your foundations before typing the first source file.

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 1 / PART 08

What are flags?

The F register is a collection of status bits

The F register is eight bits wide, but unlike A we do not normally think of it as one ordinary number. Individual bits have particular meanings. They are called flags because they signal that something about the previous operation is true.

A useful analogy is a row of indicator lamps. Each lamp can be off or on. After certain instructions, the CPU updates those lamps to describe the result.

The main flags we care about

FlagBeginner meaning
S — SignSet when the top bit of an 8-bit result is 1. This is useful when interpreting the value as signed.
Z — ZeroSet when the result is zero.
H — Half CarrySet when a carry occurs between bit 3 and bit 4. It matters especially for BCD-related arithmetic.
P/V — Parity/OverflowHas different meanings depending on the instruction. For arithmetic, it can report signed overflow.
N — Add/SubtractIndicates subtraction-related operations.
C — CarrySet when arithmetic produces a carry out of the top bit, or a borrow in certain subtraction operations.

Where the flags live inside F

F is one 8-bit register. The flags are individual bit positions inside it. For our beginner-level view, we can draw the register like this:

BitFlag
7S
6Z
5—
4H
3—
2P/V
1N
0C

Bits 5 and 3 are not part of the beginner flag set we need to reason about here, so the diagram marks them with dashes. The important idea is that each named flag is a single on/off bit.

Later, if Odin shows F = $51, we can convert $51 to binary and see which flag bits are set. We should understand the individual flags first, then treat the combined hexadecimal byte as a compact display of them.

Not every instruction changes the flags

This is important. Loading a value into A with LD A,$42 does not recalculate all of the flags. Therefore, when Odin showed AF = 42A0 in our first register experiment, the A half was deliberately set by our code, but the F half was inherited state.

Later, when we used ADD, the arithmetic instruction did update the flags. That made the F value meaningful for the result we had just calculated.

Foundation checkpoint before typing the first program

Before moving on, you do not need perfect recall, but the following statements should make sense. If one does not, revisit the matching section.

  • A bit is one binary digit, 0 or 1.
  • A nibble is four bits and maps to one hexadecimal digit.
  • A byte is eight bits and is naturally written as two hexadecimal digits.
  • $FF is 255 decimal and is the largest unsigned value in one byte.
  • A 16-bit value uses two bytes and is naturally written as four hexadecimal digits.
  • An address identifies a location; the contents are the byte stored there.
  • A pointer is a value being used as an address.
  • Registers are small storage locations inside the CPU.
  • A register contains a value; it is not itself the value.
  • The assembler translates readable source into machine-code bytes.
  • The Z80 executes machine-code bytes from memory, not the assembly source text.