DYSTOPIANPIXEL

Debugging safely

Recognise common mistakes and use the instruction reference.

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 16

Debugging and recovering safely

The label exists, but the wrong line executes

First look for an accidental fall-through. If both the ‘yes’ path and ‘no’ path of a decision are executed, a missing JR done may be the explanation. Also check whether the comparison was performed before the conditional jump and whether another instruction changed the tested flag in between.

The loop repeats too many times

Check B before DJNZ. An initial B of zero means 256 iterations for an ordinary loop body, not zero. Also ensure the loop body does not unexpectedly modify B, because that would interfere with the counter.

The subroutine does not return

Check the exact route into it. A JR to a routine is not a CALL, and its RET will not have the expected saved return address. Check every CALL path has an appropriate RET, and do not delete or bypass POP instructions after a PUSH. If the computer becomes unresponsive, reboot if necessary, reload the last saved source, correct one issue and reassemble before running again.

A register unexpectedly changes

Find every instruction in the called routine which writes that register, and verify its documentation block. The routine may need to preserve it with a matching PUSH/POP pair, or the caller’s assumptions may be wrong. Documenting ‘Changes: AF’ is more useful than relying on a convention you never stated.

The flags appear inconsistent

Identify the last flag-modifying instruction. A register load may change A without updating F. A comparison may update F without changing A. DJNZ does not alter F. A conditional branch consults the flags but does not update them. These rules often explain apparently contradictory register displays.

Odin reports an error

Read the first error rather than guessing. Check label names on both sides of a branch, the instruction’s operand syntax, commas, hexadecimal $ signs, and missing or extra lines. Confirm you are editing the file you intended. Fix one issue, use A again, and only run after a successful assembly.

Instruction quick-reference — keep beside Odin

The same small instruction family has appeared repeatedly. This table is designed for looking things up while working, not for memorisation. “Does not change F” means the instruction does not recompute the arithmetic flags; an exceptional operation such as POP AF can restore an earlier F value from memory.

CP nCompare A with n, without changing A.Updates S, Z, H, P/V, N, C (and other flag bits).No
JR condition,labelShort relative branch if condition true.No change to F.No
JP condition,labelBranch to an absolute address if true.No change to F.No
DJNZ labelDecrement B and loop if B is not zero.No change to F.No
CALL labelSave return address and enter a routine.No change to F.Yes: pushes return address.
RET / RET CReturn always / return if Carry is set.No change to F.Yes, only when return occurs.
PUSH BC/DE/HL/AFSave a 16-bit pair.No change to F.Yes: SP decreases by 2.
POP BC/DE/HL/AFRestore a 16-bit pair.No change unless POP AF restores F.Yes: SP increases by 2.

Conditions you will see often: Z = Zero set; NZ = Zero clear; C = Carry set; NC = Carry clear. A condition belongs to the immediately preceding meaningful flag-setting operation, not necessarily to the instruction immediately before it. Z80N adds instructions later, but everything in this table works on the original Z80 too.