I am trying to make a subroutine that prints a null terminated string, but it doesn't work. in my first attempt:
PRINTLN: ld a, (bc) // set bc to start of string before calling
add a, 0 // update zero flag
jp z, endprint // if the character at (bc) is a null, terminate loop
out 1, a // output to port 0 (TTY in emulator)
inc bc // point to next character
jp println // rinse and repeat
ENDPRINT: ret // end of subroutine
it just repeated the string until it stopped for some reason. My emulator won't let me view the stack.
The next attempt was to put the address of PRINTLN
to de
before the loop and then just push de
and use ret
instead of jp
:
PRINTLN: ld de, printloop // attempt to get rid of jp's effect on the stack
PRINTLOOP: ld a, (bc) // set bc to start of string before calling
add a, 0 // update zero flag
jp z, endprint // if the character at (bc) is a null, terminate loop
out 1, a // output to port 0 (TTY in emulator)
inc bc // point to next character
push de // I hope this works
ret // rinse and repeat
ENDPRINT: ret // end of subroutine
This ended in a failure too, because it kept repeating the first character of the string for some reason. What is going on and what do I do to fix it?
The emulator is inaccurate. it alters the stack when it executes a jp
instruction, even though it shouldn't. I'll look for a different emulator. Don't use the ZEMU emulator.