A score table needs a clear answer for ties, a full table and the player who just misses the cut.

Use it in a project
Maintain up to four descending unsigned byte scores with associated byte tags. Insert after existing equal scores. Return RANK 0–3 and INSERTED=1 when retained; otherwise RANK=255. COUNT>4 is invalid. Entries beyond COUNT are unused; the existing prefix must be sorted.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: RANK, INSERTED, VALID, SCORE, TAG, COUNT, S0, S1, S2, S3, I0, I1, I2, I3. They occupy 14 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, EXPR_SAVE (5 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call high_score_insertion_init once at the intended reset boundary. It clears inputs as well as outputs and workspace. Set your starting state and configuration afterwards, then call high_score_insertion_step whenever the described event occurs. The helper reads no device, waits for no frame and installs no interrupt handler.
| Ownership | Fields |
|---|---|
| Retain between calls | COUNT, S0..S3 and I0..I3 |
| Supply before service | SCORE and TAG |
| Read after service | RANK, INSERTED and VALID |
Find the first strictly smaller score, then move the tail backwards so no source entry is overwritten before it has been copied.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Insert into a High-score Table. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
high_score_insertion_init:
ld a,0
ld (RANK),a
ld (INSERTED),a
ld (VALID),a
ld (SCORE),a
ld (TAG),a
ld (COUNT),a
ld (S0),a
ld (S1),a
ld (S2),a
ld (S3),a
ld (I0),a
ld (I1),a
ld (I2),a
ld (I3),a
ld (T0),a
ld (T1),a
ld (T2),a
ld (T3),a
ld (EXPR_SAVE),a
ret
high_score_insertion_step:
; RANK = 255
ld a,255
ld (RANK),a
; INSERTED = 0
ld a,0
ld (INSERTED),a
; VALID = 0
ld a,0
ld (VALID),a
; if COUNT > 4:
ld a,(COUNT)
cp 4
jp z,high_score_insertion_L2
jp nc,high_score_insertion_L1
jp high_score_insertion_L2
high_score_insertion_L1:
; return
ret
jp high_score_insertion_L3
high_score_insertion_L2:
high_score_insertion_L3:
; VALID = 1
ld a,1
ld (VALID),a
; T0 = 0
ld a,0
ld (T0),a
; while T0 < COUNT:
high_score_insertion_L4:
ld a,(T0)
ld b,a
ld a,(COUNT)
ld c,a
ld a,b
cp c
jp c,high_score_insertion_L5
jp high_score_insertion_L6
high_score_insertion_L5:
; T1 = S[T0]
ld a,(T0)
ld e,a
ld d,0
ld hl,S0
add hl,de
ld a,(hl)
ld (T1),a
; if SCORE > T1:
ld a,(SCORE)
ld b,a
ld a,(T1)
ld c,a
ld a,b
cp c
jp z,high_score_insertion_L8
jp nc,high_score_insertion_L7
jp high_score_insertion_L8
high_score_insertion_L7:
; break
jp high_score_insertion_L6
jp high_score_insertion_L9
high_score_insertion_L8:
high_score_insertion_L9:
; T0 = T0 + 1
ld a,(T0)
add a,1
ld (T0),a
jp high_score_insertion_L4
high_score_insertion_L6:
; if T0 >= 4:
ld a,(T0)
cp 4
jp nc,high_score_insertion_L10
jp high_score_insertion_L11
high_score_insertion_L10:
; return
ret
jp high_score_insertion_L12
high_score_insertion_L11:
high_score_insertion_L12:
; RANK = T0
ld a,(T0)
ld (RANK),a
; if COUNT < 4:
ld a,(COUNT)
cp 4
jp c,high_score_insertion_L13
jp high_score_insertion_L14
high_score_insertion_L13:
; COUNT = COUNT + 1
ld a,(COUNT)
add a,1
ld (COUNT),a
jp high_score_insertion_L15
high_score_insertion_L14:
high_score_insertion_L15:
; T1 = COUNT - 1
ld a,(COUNT)
sub 1
ld (T1),a
; while T1 > T0:
high_score_insertion_L16:
ld a,(T1)
ld b,a
ld a,(T0)
ld c,a
ld a,b
cp c
jp z,high_score_insertion_L18
jp nc,high_score_insertion_L17
jp high_score_insertion_L18
high_score_insertion_L17:
; T2 = T1 - 1
ld a,(T1)
sub 1
ld (T2),a
; T3 = S[T2]
ld a,(T2)
ld e,a
ld d,0
ld hl,S0
add hl,de
ld a,(hl)
ld (T3),a
; S[T1] = T3
ld a,(T3)
ld c,a
ld a,(T1)
ld e,a
ld d,0
ld hl,S0
add hl,de
ld (hl),c
; T3 = I[T2]
ld a,(T2)
ld e,a
ld d,0
ld hl,I0
add hl,de
ld a,(hl)
ld (T3),a
; I[T1] = T3
ld a,(T3)
ld c,a
ld a,(T1)
ld e,a
ld d,0
ld hl,I0
add hl,de
ld (hl),c
; T1 = T1 - 1
ld a,(T1)
sub 1
ld (T1),a
jp high_score_insertion_L16
high_score_insertion_L18:
; S[T0] = SCORE
ld a,(SCORE)
ld c,a
ld a,(T0)
ld e,a
ld d,0
ld hl,S0
add hl,de
ld (hl),c
; I[T0] = TAG
ld a,(TAG)
ld c,a
ld a,(T0)
ld e,a
ld d,0
ld hl,I0
add hl,de
ld (hl),c
; INSERTED = 1
ld a,1
ld (INSERTED),a
ret
component_end:Machine contract
AF/BC/DE/HL may change; IX/IY are preserved. A direct CALL needs two return-address stack bytes. The helper makes no nested calls and adds no other stack use. Its named scratch is private workspace, not a pointer, a ROM variable or shared zero-page space that the game may borrow concurrently. The samples use fixed symbol bindings. Scope or rename every label and binding, including the component_end measurement marker, and allocate a separate block for each independent instance. Different helpers can also use common field names, so combining them needs separate namespaces or explicitly renamed bindings. Give each block one mutation owner.
The 48K diagnostic boots through ROM tape loading and USR, disables interrupts and owns the screen. The helper performs no port or screen writes.
Adapt the caller
demo.asm supplies the bindings and include order. exercise.inc first runs table cases, then a separate client retaining state between calls. A game can call the public step entry directly and remove the diagnostic packet and display. Do not copy the diagnostic’s hardware takeover into an existing game loop.
Build with make ASM198X=/path/to/asm198x. The Makefile produces normal, custom, deliberately wrong and repaired programs. Tools must already be installed; the sample downloads nothing.
Bindings and include order
org $8000
CUSTOM equ 0
BROKEN equ 0
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
RANK: defb 0
INSERTED: defb 0
VALID: defb 0
SCORE: defb 0
TAG: defb 0
COUNT: defb 0
S0: defb 0
S1: defb 0
S2: defb 0
S3: defb 0
I0: defb 0
I1: defb 0
I2: defb 0
I3: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
EXPR_SAVE: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end startA client retaining state between calls
preview_rules:
if CUSTOM
; SCORE = 255
ld a,255
ld (SCORE),a
; TAG = 4
ld a,4
ld (TAG),a
if BROKEN
; SCORE = 0
ld a,0
ld (SCORE),a
endif
call checked_step
call emit_state
; SCORE = 0
ld a,0
ld (SCORE),a
; TAG = 5
ld a,5
ld (TAG),a
if BROKEN
; SCORE = 0
ld a,0
ld (SCORE),a
endif
call checked_step
call emit_state
ret
else
; SCORE = 20
ld a,20
ld (SCORE),a
; TAG = 1
ld a,1
ld (TAG),a
if BROKEN
; SCORE = 0
ld a,0
ld (SCORE),a
endif
call checked_step
call emit_state
; SCORE = 40
ld a,40
ld (SCORE),a
; TAG = 2
ld a,2
ld (TAG),a
if BROKEN
; SCORE = 0
ld a,0
ld (SCORE),a
endif
call checked_step
call emit_state
; SCORE = 40
ld a,40
ld (SCORE),a
; TAG = 3
ld a,3
ld (TAG),a
if BROKEN
; SCORE = 0
ld a,0
ld (SCORE),a
endif
call checked_step
call emit_state
endif
retCustomise it
Retain the highest possible byte score and a zero score. The maintained custom caller changes its inputs or client sequence rather than hiding a different implementation behind the same label. Compare every state of the two clients, not only their final display.
Custom client selection
org $8000
CUSTOM equ 1
BROKEN equ 0
COYOTE_STEPS equ 2
BUFFER_STEPS equ 2
TEXT_CAP equ 4
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 49
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
RANK: defb 0
INSERTED: defb 0
VALID: defb 0
SCORE: defb 0
TAG: defb 0
COUNT: defb 0
S0: defb 0
S1: defb 0
S2: defb 0
S3: defb 0
I0: defb 0
I1: defb 0
I2: defb 0
I3: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
EXPR_SAVE: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end startThe custom diagnostic row is 01 01 01 00 05 02 FF 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller clears the achieved score before inserting it. The ordering routine cannot recover that lost value. Pass the completed score before resetting gameplay state.
The BROKEN branch lives in the sequential client, not the reusable helper. wrong.asm selects it; repaired.asm restores the normal call sequence. The repaired executable matches the normal program (for Spectrum, compare loaded CODE rather than the tape filename).
Select the deliberately wrong client
org $8000
CUSTOM equ 0
BROKEN equ 1
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
RANK: defb 0
INSERTED: defb 0
VALID: defb 0
SCORE: defb 0
TAG: defb 0
COUNT: defb 0
S0: defb 0
S1: defb 0
S2: defb 0
S3: defb 0
I0: defb 0
I1: defb 0
I2: defb 0
I3: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
EXPR_SAVE: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end start
Cost and alternatives
The measured helper extent is 352 code bytes; public state is 14 bytes and private scratch is 5 bytes. There are no immutable helper tables. Caller data, the test cases, return-address stack, alignment and display buffers cost additional space. These are assembled sizes, not cycle measurements.
Four comparisons and at most three pair moves. Insertion is easier to audit than sorting the whole table after every result.
Scores are one byte and tags are identifiers, not name strings. Wider scores need a multi-byte comparison; permanent storage is a separate concern.
Execution evidence
Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 64 boundary/varied/error table cases, dirty initialisation and the separate retained-state client. An independent mathematical/container model checks every public byte after every call. This is bounded example coverage, not exhaustive correctness for every possible state.
The records retain loaded immutable code, completion, register sentinels, full packets, source/tool/firmware hashes and captures. The public gate validates these retained observations and rejects altered evidence; it does not run restricted firmware. No firmware is distributed. The eight displayed bytes are the first eight public fields, zero-padded when shorter; the packet includes every field.
Where it fits
Score counters: Store the final score before starting a new run.