Your place is saved automatically on this browser.
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
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:
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
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.
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 C9The 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.
