DYSTOPIANPIXEL

Routine contracts

Document inputs, outputs and register changes, then repair a helper contract.

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 13

Subroutines need an explicit contract

In a larger programme, a caller must know what a routine expects and what it changes. That information is its calling contract. Different Z80 programmes, operating systems, libraries and tools may use different conventions; there is no universal convention we may silently impose on every routine.

A helpful documentation block for a simple routine is:

; ADD_THREE
; Input:      A = unsigned 8-bit value
; Output:     A = A + 3 (modulo 256)
; Changes:    AF (A and arithmetic flags)
; Preserves:  BC, DE, HL
; Stack:      balanced; one RET for its CALL

This contract is a statement about the routine we wrote, not a claim that every Z80 routine uses the same registers. Notice the word modulo: if A is $FE, adding three produces $01 in an eight-bit register. The Carry flag indicates the unsigned overflow from $FE + $03.

A routine that borrows B but restores BC

start:
    ld bc,$1234
    ld a,$05
    call add_three_save_bc
    ld hl,$6000
    ret

add_three_save_bc:
    push bc
    ld b,$ff
    add a,$03
    pop bc
    ret

The subroutine uses B temporarily and then restores the caller’s BC. Expected: A = $08, BC = $1234, HL = $6000. Because POP BC does not change arithmetic flags, the flags left by ADD A,$03 remain available after the return. But the routine does not preserve F — it changes F deliberately during addition.

A deliberately broken caller-and-helper contract

Consider a caller that keeps a resource count in B and an identifier in C. It expects BC to survive a helper call. The following example demonstrates what happens when the helper overwrites B without telling the caller. It is safe to run in Odin because it still balances the stack.

start:
    ld bc,$1234
    call careless
    ld hl,$6000
    ret

careless:
    ld b,$00
    ret

What should BC contain after R? The caller may have expected $1234, but the actual result is $0034: only B was overwritten. Changing B also changed the combined 16-bit value BC. This kind of error is especially awkward in a large application because the visible symptom might occur long after the call.

Correct it by preserving the borrowed register pair

start:
    ld bc,$1234
    call careful
    ld hl,$6000
    ret

careful:
    push bc
    ld b,$00
    pop bc
    ret

Now the helper saves BC on entry and restores it before RET. Expected BC = $1234. The helper may freely use B between the PUSH and POP, provided it does not return early before restoring the pair. An unconditional or conditional return inserted in that gap without a matching POP would be unsafe.

There are two legitimate designs: the helper preserves BC, or its contract explicitly says it changes BC and the caller saves BC if needed. The right answer is not “always PUSH everything”; it is to choose a documented contract, then consistently honour it.

Caller-saved, callee-saved and what happens to AF

The caller is the routine that executes CALL. The callee is the routine being called. In a caller-saved arrangement, the caller protects a register it needs across a call. In a callee-saved arrangement, the helper promises to put that register back. Both designs can work. Next Asset Lab will use explicit contracts while its project-wide application calling convention is established.

AF deserves special care: PUSH AF and POP AF restore A and the entire F register together. If the helper calculates useful new arithmetic flags and then executes POP AF, the caller sees the old flags instead of the result flags. Conversely, a helper promising to preserve A but return new Carry/Zero flags cannot simply preserve both with PUSH AF/POP AF. Its contract must be designed for that purpose.

Try both downloadable demonstrations: T02BAD.ASM shows the unexpected BC change, and T02FIX.ASM shows its corrected, balanced version.

The first Next Asset Lab convention

We will document each new internal helper using the same headings: Purpose; Inputs; Outputs; Changes; Preserves; Stack behaviour; Errors; Tests. We will avoid an undocumented promise such as ‘all routines preserve everything’. When modules are integrated, a project-wide convention can be formalised and kept consistent. Calls into NextZXOS or a third-party driver must follow that interface’s documented ABI, not an invented Next Asset Lab one.