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 For instance, when you have Another example is when
you have 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 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:
a
didn't have a
partner? It does, and it's f
. 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 Flag
The 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.
$01
stored in
b
and perform dec b
, the
zero flag will be set because b
has reached zero; but if b
contains $02
and you perform dec b
, the zero flag will
be reset because it will be at $01
still.
$09
stored in a
and you do
a cp $09
. The
processor will subtract $09
from a
and set the zero flag
because the answer is zero.
Carry Flag
The carry flag tells us the resultant value was too big
to be stored in the register.
f
register. The carry flag is
usually used in shift and rotate instructions which we will learn
about later.
cp arg1
Assembler | High Level |
Carry | arg1 >= a
|
Zero | arg1 == a
|
No Carry | arg1 < a
|
No Zero | arg1 != a
|
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.
ZF | CF | ||||||||||||||||||||||||||||||
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.
Syntax | Variations | Explaination | |||||||||
jr addr1 | 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