DYSTOPIANPIXEL

A reusable clamp helper

Build and trace a helper that clamps an unsigned byte to 0–15.

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 14

A small reusable helper for Next Asset Lab

We can now combine comparison, a conditional return and a subroutine into one useful building block. A nibble has four bits and therefore fits an unsigned value from 0 to 15. Our helper, called clamp_to_15, will accept an unsigned byte in A and return the original value if it is below $10, or $0F if it is greater than 15.

A clamping routine is useful whenever an input field must be restricted to a nibble-sized range. This is not a palette engine or sprite editor yet, but it is the kind of small testable helper on which an editor can be built.

Input/output contract

PurposeClamp an unsigned byte to the range 0–15
InputA = unsigned byte $00–$FF
OutputA = the input, or $0F if the input was $10 or greater
ChangesAF; caller must not assume flags have uniform meaning after both paths
PreservesBC, DE, HL (the helper does not use them)
StackUses only normal CALL/RET mechanics; no extra PUSH/POP
Error indicationNone; all 256 unsigned inputs are accepted

RET C is not the same as JR C

This helper uses a conditional return. A conditional instruction checks a flag and chooses one of two paths; it does not invent a new return address. Compare these three instructions:

RET CPop the current return address into PC; return to the caller.Continue with the instruction after RET C.
JR C,labelJump to the label within reachable relative range; remain in the current routine.Continue with the next instruction.
RETReturn regardless of flags.Return regardless of flags.

Only RET (conditional or unconditional when taken) removes the current return address from the stack. JR C changes PC but does not remove that return address. This makes RET C useful for early exit from a subroutine only when you have already restored anything the subroutine pushed itself.

In the final helper, RET C is reached after a CALL from start. For A below $10, it returns immediately to the instruction after that CALL. For A at or above $10, the conditional return is not taken and execution simply falls through to LD A,$0F and the ordinary RET.

Final source listing — tut02.odn

; Next Asset Lab -- Tutorial 2
; Odin Dev.2k teaching programme
; Run the 'start' entry from Odin.

start:
    ld a,$13
    call clamp_to_15
    ld hl,$6000
    ret

; Clamp an unsigned byte to 0..15.
; Input:      A
; Output:     A = min(A,$0f)
; Changes:    AF
; Preserves:  BC, DE, HL
; Stack:      balanced
clamp_to_15:
    cp $10
    ret c
    ld a,$0f
    ret

Read the subroutine carefully. CP $10 compares A with decimal 16. For an unsigned A below 16, Carry is set, and RET C immediately returns from the subroutine, leaving A unchanged. Otherwise, LD A,$0F replaces the too-large value with 15, and the final RET returns normally.

Run 1. With A initially $13 (decimal 19), the input is too large, so the expected result is A = $0F. Run 2. Change only the first value to $07, save, assemble and run again. Because seven is below 16, RET C returns early and the expected result is A = $07.

Important flags warning. After CP $10, flags describe a comparison of the original A with $10. If the routine then executes LD A,$0F, the load does not refresh those flags to describe the new value. Callers must therefore use the documented A output, not infer the result from leftover flags. If a caller needs Zero or Carry to describe a separate condition, it should perform the appropriate operation explicitly.

Trace both paths of clamp_to_15

The table below is a hand-executed trace, not a measurement from Odin. Follow one input across the row before switching to the other. Notice that CP changes flags without changing A.

CALL clamp_to_15Enters helper; saves return addressEnters helper; saves return addressBoth take the CALL.
CP $10A still $07; C=1, Z=0A still $13; C=0, Z=07 is below 16; 19 is not.
RET CTaken; returns to callerNot taken; continuesChecks Carry set by the CP.
LD A,$0FSkippedA becomes $0FOnly the large input needs clamping.
RETSkippedReturns to callerThe second return is used only in the second path.
Final A$07$0FThe helper accepts any unsigned byte.

After either path, the CALL return address has been removed exactly once. The caller then executes LD HL,$6000 and its own RET, returning to Odin. The `LD A,$0F` on the large-input path does not update F, so do not interpret the leftover flags as if they describe the final clamped A value.

Two useful boundary cases: with A=$0F (15), Carry is set and the helper leaves A unchanged; with A=$10 (16), Carry is clear and the helper replaces A with $0F. These are part of the six-input test record in Part 15.

Understanding the final bytes

Using the $6000 origin previously observed in Tutorial 1, this source should produce the following machine-code sequence if Odin assembles it in the same location:

3E 13 CD 09 60 21 00 60 C9 FE 10 D8 3E 0F C9

The initial two bytes load A; the next three encode a CALL to $6009; the next three load HL; the following C9 returns to Odin. At $6009, the helper compares with $10, uses conditional RET C (D8), loads $0F if necessary, then returns. Use Odin’s T and M $6000 to check what your assembled source actually produced. If the source and machine code disagree, do not press R until you have found the discrepancy.

This is candidate project utility code, not yet integrated into the editor core or proven in an assembled NEX. Real-machine execution and later module integration are separate acceptance steps.