DYSTOPIANPIXEL

Practice exercises

Predict results and compare your reasoning with worked answers.

Next Asset Lab concept artwork showing sprite patterns, palette colours and animation frames
Next Asset Lab concept artwork · Begin with Odin Dev.2k and the foundations of Z80 assembly.
TUTORIAL 2 / PART 17

Practice exercises

Try these in your notebook first, then verify the ones requiring a programme in Odin. There is no prize for finishing quickly; the skill is being able to predict the result before looking at the register screen.

  • Explain how $FE can mean 254 unsigned or −2 signed.
  • What do Z and C mean immediately after comparing A = $12 with $12? What happens to A?
  • If A = $10 and the next instruction is CP $20, which of JR C,target and JR NC,target would branch?
  • In a two-path decision, why may an unconditional jump be needed after the first path?
  • Which instruction changes the stack automatically: JP or CALL? Why?
  • Predict final A and B after the three-iteration DJNZ example.
  • What happens if B initially contains zero before that loop’s DJNZ?
  • What is wrong with PUSH BC, PUSH DE, POP BC, POP DE when the intention is to preserve both pairs?
  • Why does CALL add_three need two different RET instructions in our complete programme?
  • Predict the clamp helper’s output for inputs $00, $0F, $10, $13 and $FF.
  • A routine modifies B but claims it preserves BC. What would you inspect first?
  • Why must a caller not assume that Zero describes the new A value after clamp_to_15 returns?

Worked answers

1. The stored byte is 11111110 either way. Under unsigned interpretation it is 254. In signed two’s-complement it is 254 − 256 = −2. 2. Z is set (equal), Carry is clear (no borrow), and A remains $12. 3. Carry is set because $10 is below $20 in unsigned comparison, so JR C is taken. 4. A label does not stop fall-through. Without a jump around the alternate block, both paths might execute. 5. CALL saves a return address on the stack; JP just changes PC to a target. 6. A becomes $03, B becomes $00. 7. B first wraps to $FF, producing 256 iterations rather than zero.

8. The values must be popped in reverse order: POP DE, then POP BC. 9. The subroutine RET returns to the calling instruction’s successor within the main routine; the main routine’s RET returns to Odin. 10. $00, $0F, $0F, $0F, $0F. 11. The code and stack balance: if B is used temporarily, it must be restored appropriately, or the contract must admit that BC changes. 12. The flags come from CP $10, and LD A,$0F does not update them; they cannot be interpreted as fresh arithmetic flags for the output A.