DYSTOPIANPIXEL

Your first decision

Type and test both paths of a conditional decision.

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 06

First decision experiment in Odin

Create tut02.odn using the workflow from Part 1. Type this complete example:

start:
    ld a,$12
    cp $12
    jr nz,not_equal
    ld b,$01
    jr done
not_equal:
    ld b,$00
done:
    ld hl,$6000
    ret

Read it slowly before running. First, A receives $12. Next, CP $12 compares A with the same value, so Z is set. JR NZ,not_equal jumps only if Z is not set, so that jump is not taken. LD B,$01 executes. The unconditional JR done skips over the alternative LD B,$00. Finally HL is set to the familiar inspection address and RET returns to Odin.

Expected state after R: A is $12, B is $01, and HL is $6000 if using the same assembly origin as Tutorial 1. The last comparison made Z set. Odin’s other register values can reflect earlier state because the programme did not initialise them.

Now change only the comparison to cp $13, save and assemble again. A remains $12, but this time Z is clear; the jump to not_equal is taken and B ends as $00.

A = $12cp $12$01Equal, Z set; no NZ branch
A = $12cp $13$00Not equal, Z clear; NZ branch taken

Common beginner mistake: fall-through

Remove the line jr done on paper, not from your saved working source, and follow the remaining instructions. After B receives $01, execution would continue straight down into the label not_equal and run LD B,$00 as well. The result would be $00 even in the equal case. A label does not stop execution. It merely names a position. This is why the unconditional branch is present.

Inspecting the programme rather than guessing

Save with S; assemble with A; check T for start, not_equal and done; and inspect memory with M $6000. You should see different machine-code lengths for LD, CP and the jumps. The exact branch offsets are the assembler’s job, not something you need to memorise.