Answers for Control Structures


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

Identify the Error(s)


inc b

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


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.

Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.