z80:Answers 3

From Learn @ Cemetech
Jump to navigationJump to search

1.

   loop:
    cp b
    jr c,bigger
    jr z,endLoop
    inc c
    inc a
    jr loop
   bigger:
    dec c
    dec a
    jr loop
   endLoop:
    ...


Identify the Error(s)

2.

   inc b

DJNZ uses B as the counter, so increasing B in the loop will result in an infinite loop

3.

   jp mUp		;assume mUp exists somewhere
    ...
    jp mDown		;assume mDown exists somewhere


The error is not that mUp or mDown does not exist, it's that there is no check on any flag. It will jump regardless, so in this case it will always jump to mUp.

Fixed code:


   jp z,mUp
   ...
   jp z,mDown

4. Nothing wrong with the code.

5. There's nothing wrong with the code. We want it to repeat 256 times, and since DJNZ decrements first, by setting B to zero it will first change B to 255, then repeat 255 times more. 255+1=256. Note that this is bad coding practice, though. Generally if you want a counter larger than $FF (255), you'll use BC as a 16 bit counter.