Re: LZ: zshell programming


[Prev][Next][Index][Thread]

Re: LZ: zshell programming



On Mon, 29 Jul 1996, Will Stokes (3D Diamond productions) wrote:


> 	my second problem has to do with those "if-like" thingies in
> assembly. lets say depending on what register a is I want to load
> different strings to de, say if a=1 string p1, if a=2 string p2, if a=3
> string p3 and so on for values of a 1, 2, 3, 4, and so on up to and
> including 9. how exactly would i test a and then load the correct string
> to de? this is what I have come up with so far:
> 
> 	ld b, 1
> 	cp b
> 	ld de, p1
> 
>
> 	ld b, 2
> 	cp b
> 	ld de, p2
 
That won't work because your not making the load depend
on the compare.  Try it like this:


	cp	1
	jr	nz,L1
	ld	de,p1
	jr	FIN


L1:	cp	2
	jr	nz,L2
	ld	de,p2
	jr	FIN


L2:	...
	...
	...


FIN:	...


This does a compare of the accumulator with 1 and
sets the Zero flag.  A compare is really a subtract,
so if they're the value in the accumulator is 1 and
you subtract 1 you get 0 and when the result is 0,
the Zero flag is set.  A compare isn't exactly like
a subtract because it doesn't change the accumulator,
but it sets the flags just like it was a subtract.


The next instruction jumps if the Zero flag is not
set, but it doesn't jump if the Zero flag is set.  So
if the accumulator contains 1, control passes to the
next instruction, which loads de with the address of
the string.  Then you have to jump on out of there
or it'll fall right through the next compare.  You're
done comparing at that point.


There is a trap here, though.  JR is Jump Relative and
it can only jump about 127 bytes. (A little farther
forward and a little less backward, but I'm not a Z80
programmer, really so I don't know the details that
well).  If the first compare is more than 127 bytes
from FIN, it won't assemble, or if you're coding this
by hand, it won't work.  In that case you need to use
JP instead.  Use JR if you can, though, cause it's a
lot shorter and faster.  The assembler will tell you if
you need JP instead.


I hope that helps.  Again, I'm an assembly programmer
but not particularly a Z80 programmer so I hope someone
will jump in and correct any mistakes I made.


Barry


References: