DYSTOPIANPIXEL

Unsigned ranges

Use Carry to test whether an unsigned value is below a limit.

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 07

Using Carry for unsigned ranges

For unsigned values, CP followed promptly by JR C answers ‘is A below this limit?’ Here is another complete experiment. Replace the previous source; do not append it.

start:
    ld a,$10
    cp $20
    jr c,smaller
    ld b,$00
    jr done
smaller:
    ld b,$01
done:
    ld hl,$6000
    ret

Here $10 (decimal 16) is below $20 (decimal 32). The comparison requires a borrow, so Carry is set and execution goes to smaller. Expected result: B = $01 and A = $10. Change A to $30 (decimal 48) and the branch is not taken: B becomes $00.

A useful pattern for a proposed Next Asset Lab routine is cp limit followed by jr c,within_range. But remember the word unsigned. For signed negative numbers, Carry alone does not tell you whether one signed value is less than another. Correct signed comparisons require considering the overflow condition too; they are not simply a matter of checking S in isolation. We will revisit advanced signed comparison strategies when coordinate and movement code needs them.

What to record in your notebook

For each run, record: the A value, the comparison value, whether Z was set, whether Carry was set, which label executed, and the final B value. This is a small form of testing. Once the relationship between input and output becomes predictable, the machine is no longer mysterious.