Re: A82: Start Me Up
[Prev][Next][Index][Thread]
Re: A82: Start Me Up
>> Down to business; what I need:
>> How do you do if then's?
if/thens are done using the cp instruction. the instruction actually
performs a subtraction but does not change the accumulator or other
value (usually a register). you use it as follows:
cp r ;r is an 8 bit register. it is subtracted from the
;accumulator (a - r) and flags are set in the flag
;register (reg. f) according to the result. neither
;register's value is changed
EXAMPLES
--------
ld a,5 ;a = 5
ld d,5 ;d = 5
cp d ;a ?=? d, are the two numbers equal?
jr z,EqualTo ;a = d, subtraction (5-5) equals 0 so Zero flag is set
;so i jump to the label EqualTo, else i continue to
... ; the next statement
more
code
...
...
EqualTo: ;the label that you jump to
...
ld a,7 ;a = 7
ld d,5 ;d = 5
cp d ;a ?!=? d, are the two numbers not equal?
jr nz,NotEqual ;a != d, subtraction is NONZERO so Z flag is reset
... ;so the jump will be taken
ld a,5 ;a = 5
ld d,7 ;d = 7
cp d ;a ?<? d, is a less than d?
jr c,LessThan ;a < d, subtraction (5-7) result is negative so set
... ;Carry flag. the jump will be taken
ld a,7 ;a = 7
ld d,5 ;d = 5
cp d ;a ?>=? d, is a greater than or equal to d ?
jr nc,NotLess ;a >= d, subtraction result is positive or zero so
... ;Carry flag is reset. so the jump will be taken
that is how it works. not too difficult.
-mike pearce
mgp4007@omega.uta.edu
References: