(This is part 3 of 12 short pieces on machine code programming on the ZX80/1–AZ)
The Flags register, F, is the other half of the Accumulator pair, AF. The Accumulator (or A register) is where the results of operations (like add and subtract) are stored. The Flags register tells you something about the value in the A register. AF is just like the other register pairs HL, BC, DE, etc., except that the AF pair changes with each manipulation; all of the others change only when specifically instructed to do so.
Instead of values for each bit position, the Flags register holds information for testing. Each bit stands for a different condition and can be either 0 or 1 (0=flag not set, 1=flag set). Each bit represents the information:
FLAGS (F)
S Z - H - P/O N C
(bit) 0 0 0 0 0 0 0 0
ACCUMULATOR (A)
128 64 32 16 8 4 2 1
(bit) 0 0 0 0 0 0 0 0
Bit 7--S sign (0 if +, 1 if -)
6--Z zero (0 if result of oper-
ation WAS NOT 0, 1 if result
WAS 0)
5--not used
4--H half carry (used intern-
ally, not testable)
3--not used
2--P/O parity/overflow (parity
is testable)
1--N add or subtract (used
internally, not testable)
0--C carry (1 is a carry from
the Accumulator)
Most nonprofessional machine code programmers need thoroughly understand only the Z flag. The others can be studied when necessary. For instance, if you have an address in REM for data (<128; + or – value), you can load A with that value, test bit 7 (the Z flag) to see if it is positive or negative, then continue depending on what is in the Z bit.
Here’s what happens: when bit 7 is tested, the Z flag will show 0 if the bit is 1 (negative value) or 1 if the bit is 0 (positive value). It sounds confusing, but just remember 0 in the Z flag means negative; 1 means positive. If the next instruction is JR Z (jump relative to Z), then the computer will jump if Z=1 (similar to IF…THEN GOTO in BASIC). If Z=0, the computer just goes to the next instruction.
The Z80 offers thousands of possible bit-test/test-and-branch combinations…try a few.
Next: LOADING REGISTERS WITH DATA