Flags and Conditions
z80 » Beginner
Where would we be without If/Then statements? They're
called conditions in asm. There is no If command in asm.
Don't worry though. We have what are called
'flags'. Remember when I was telling you about the
register pairs? Do you remember that
a didn't have a
partner? It does, and it's
Af is the
registered pair. The z80 has six flags on it total, which are
represented in the
f register as being either set (1) or
reset (0). Remember that the
f register contains the
flags as bits. The most commonly used flags are the 'zero
flag' and the 'carry flag', we will learn about
the others later on.
Zero FlagThe zero flag is used to show us whether the value of the indicated operation is zero: a subtraction resulted in a zero, a decriment ended up being zero, or there was zero difference between the two values compared.
For instance, when you have
$01 stored in
b and perform
dec b, the
zero flag will be set because
b has reached zero; but if
$02 and you perform
dec b, the zero flag will
be reset because it will be at
Another example is when
$09 stored in
a and you do
cp $09. The
processor will subtract
a and set the zero flag
because the answer is zero.
Carry FlagThe carry flag tells us the resultant value was too big to be stored in the register.
When you perform a command that
results in a 9 bit answer that can not be stored in the 8 bit
register, the 9th bit is stored in the carry
flag's spot on the
f register. The carry flag is
usually used in shift and rotate instructions which we will learn
These flags are either set or reset by either a comparison or a testing instructions. The flags will also remain set or reset until a new comparative instruction is executed, this means that you could have a comparative command, some code, and then the conditional branch (jump).
Here's a chart that compares the flag states to test operands in TI-BASIC considering you used:
|Carry||arg1 >= |
|Zero||arg1 == |
|No Carry||arg1 < |
|No Zero||arg1 != |
The following chart* shows the relationships of the flags to different operations being performed.
Note: When the carry flag is set, the zero flag is also set. This is not neccessarily the other way around too.
ld a,255 inc a
* ZF stands for zero flag set, CF stands for carry flag set, NZ stands for zero flag reset, and NC stands for carry flag reset.
ConditionsConditions are used in redirection commands. If the condition is true, then the processor goes the the specified address and starts executing code.
jr z,addr1 jr nz,addr1 jr c,addr1 jr nc,add1
Check out these branch jumps in action...
ld a,69 ;load a with number cp 69 ;is a equal to 69? jr z,it_was_69 ;it was 69! jr nz,it_was_not_69 ;wouldn't even ; need this ; because either ; the zero flag is ; set or notYou can also use other registers in checking.
ld b,69 ;number to check ld a,69 ;number to check against cp b ;is a=b? jr z,it_is ;they're both the same jr nz,it_is_not ;it isn't
More from z80 » Beginner
Aliases // Convert from decimal to hexadecimal or binary // Flags and Conditions // Format and Compiling // Instructions // Math // Number Bases // Oh, No! It Crashed! // Registers // TI-BASIC to Asm Comparisons // TI86 Specifications // Two's Compliment // z80 Processor