Flags and conditionals
So if you're not allowed to use IF statements, how do you do conditionals?
There is a special register for conditionals, it is called the flag register.
There are actually many flags, but only two are important. These are the zero
flag and carry flag. How are the flags changed? Well, basically the results
of a math operation affects the flag. Lets say after doing a SUB, the acc is
zero.
ld a,10 ;a=10
ld b,10 ;b=10
sub b ;a=a-b... a=10-10... a=0
After this command the ZERO FLAG is SET. So what good is that? Well, you can
make conditions for your jumps, calls, and returns to occur. You simply put
a flag name as a first parameter and put the label as the second parameter.
Possible first parameters (conditions) are z,nz,c,nc.
jr z,Lbl
jp z,&Lbl
call z,&Lbl
ret z
So what does that do? Well, if the ZERO FLAG is set (as discussed above), the
instruction happens. (jump,call,ret) Otherwise, nothing happens.
You can change the first parameter (condition) to change what situation the
jump, call, or return is executed.
Z = Zero flag set
NZ = Zero flag not set
C = Carry flag set
NC = Carry flag reset
So you already know what the zero flag does. But what about the Carry Flag?
Well, anytime a 'wraparound' of the affected register happens, the carry flag
is set. Otherwise it is reset. SOMETIMES OPERATIONS ON REGISTER PAIRS WILL
NOT HAVE THE SAME AFFECT. One big example is inc or dec on a reg pair.
Note that LD is not a math operation; it NEVER changes ANY FLAG.
Your first program
#include "usgard.h"
.org 0
.db "My first program",0
call CLEARLCD
ld hl,0
ld (CURSOR_ROW),hl
ld hl,&Text
call D_ZT_STR
Wait_4_key:
call GET_KEY
or a
ret nz
jr Wait_4_key
Text:
.db "Hello World!",0
.end
You may not understand this program, but it will be discussed in detail in
the next lesson.
Cut and paste this program into a new text file, and name it hello.asm.
Make sure you leave a few empty lines after the last line. (.end)
Change to the Usgard main dir, and type this in at the DOS prompt:
c hello
Then send it using whatever link program you have. It should have a
description of "My first program". When you enter the program it should
display "Hello World" then wait for a key and return.
Congratulations, you have compiled your first program!