A quotient without its remainder throws useful information away. Keep both when converting counters, coordinates or scores.

Use it in a project
Divide the unsigned low-byte-first 16-bit numerator by DIVISOR 1–255. Return a 16-bit quotient, a remainder below the divisor and VALID=1. Division by zero returns zero outputs and VALID=0. Inputs are preserved.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: QUOTIENT_LO, QUOTIENT_HI, REMAINDER, VALID, NUMERATOR_LO, NUMERATOR_HI, DIVISOR. They occupy 7 contiguous bytes. Also reserve the private workspace: T0, T1, T2 (3 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call word_division_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 word_division_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 | none |
| Supply before service | NUMERATOR_LO, NUMERATOR_HI and DIVISOR |
| Read after service | QUOTIENT_LO, QUOTIENT_HI, REMAINDER and VALID |
Long division consumes sixteen numerator bits. The remainder temporarily needs a ninth bit when shifted; keeping that carry separately prevents a large divisor from being compared against a wrapped remainder.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Divide a Word by a Byte. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
word_division_init:
ld a,0
ld (QUOTIENT_LO),a
ld (QUOTIENT_HI),a
ld (REMAINDER),a
ld (VALID),a
ld (NUMERATOR_LO),a
ld (NUMERATOR_HI),a
ld (DIVISOR),a
ld (T0),a
ld (T1),a
ld (T2),a
ret
word_division_step:
; QUOTIENT_LO = 0
ld a,0
ld (QUOTIENT_LO),a
; QUOTIENT_HI = 0
ld a,0
ld (QUOTIENT_HI),a
; REMAINDER = 0
ld a,0
ld (REMAINDER),a
; VALID = 0
ld a,0
ld (VALID),a
; if DIVISOR == 0:
ld a,(DIVISOR)
cp 0
jp z,word_division_L1
jp word_division_L2
word_division_L1:
; return
ret
jp word_division_L3
word_division_L2:
word_division_L3:
; VALID = 1
ld a,1
ld (VALID),a
; QUOTIENT_LO = NUMERATOR_LO
ld a,(NUMERATOR_LO)
ld (QUOTIENT_LO),a
; QUOTIENT_HI = NUMERATOR_HI
ld a,(NUMERATOR_HI)
ld (QUOTIENT_HI),a
; T0 = 16
ld a,16
ld (T0),a
; while T0 != 0:
word_division_L4:
ld a,(T0)
cp 0
jp nz,word_division_L5
jp word_division_L6
word_division_L5:
; T1 = QUOTIENT_HI >> 7
ld a,(QUOTIENT_HI)
srl a
srl a
srl a
srl a
srl a
srl a
srl a
ld (T1),a
; T2 = QUOTIENT_LO >> 7
ld a,(QUOTIENT_LO)
srl a
srl a
srl a
srl a
srl a
srl a
srl a
ld (T2),a
; QUOTIENT_HI = QUOTIENT_HI << 1
ld a,(QUOTIENT_HI)
sla a
ld (QUOTIENT_HI),a
; QUOTIENT_HI = QUOTIENT_HI | T2
ld a,(QUOTIENT_HI)
ld b,a
ld a,(T2)
ld c,a
ld a,b
or c
ld (QUOTIENT_HI),a
; QUOTIENT_LO = QUOTIENT_LO << 1
ld a,(QUOTIENT_LO)
sla a
ld (QUOTIENT_LO),a
; T2 = REMAINDER >> 7
ld a,(REMAINDER)
srl a
srl a
srl a
srl a
srl a
srl a
srl a
ld (T2),a
; REMAINDER = REMAINDER << 1
ld a,(REMAINDER)
sla a
ld (REMAINDER),a
; REMAINDER = REMAINDER | T1
ld a,(REMAINDER)
ld b,a
ld a,(T1)
ld c,a
ld a,b
or c
ld (REMAINDER),a
; if T2 != 0:
ld a,(T2)
cp 0
jp nz,word_division_L7
jp word_division_L8
word_division_L7:
; REMAINDER = REMAINDER - DIVISOR
ld a,(REMAINDER)
ld b,a
ld a,(DIVISOR)
ld c,a
ld a,b
sub c
ld (REMAINDER),a
; QUOTIENT_LO = QUOTIENT_LO | 1
ld a,(QUOTIENT_LO)
or 1
ld (QUOTIENT_LO),a
jp word_division_L9
word_division_L8:
; if REMAINDER >= DIVISOR:
ld a,(REMAINDER)
ld b,a
ld a,(DIVISOR)
ld c,a
ld a,b
cp c
jp nc,word_division_L10
jp word_division_L11
word_division_L10:
; REMAINDER = REMAINDER - DIVISOR
ld a,(REMAINDER)
ld b,a
ld a,(DIVISOR)
ld c,a
ld a,b
sub c
ld (REMAINDER),a
; QUOTIENT_LO = QUOTIENT_LO | 1
ld a,(QUOTIENT_LO)
or 1
ld (QUOTIENT_LO),a
jp word_division_L12
word_division_L11:
word_division_L12:
word_division_L9:
; T0 = T0 - 1
ld a,(T0)
sub 1
ld (T0),a
jp word_division_L4
word_division_L6:
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:
QUOTIENT_LO: defb 0
QUOTIENT_HI: defb 0
REMAINDER: defb 0
VALID: defb 0
NUMERATOR_LO: defb 0
NUMERATOR_HI: defb 0
DIVISOR: defb 0
T0: defb 0
T1: defb 0
T2: 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
; NUMERATOR_LO = 255
ld a,255
ld (NUMERATOR_LO),a
; NUMERATOR_HI = 255
ld a,255
ld (NUMERATOR_HI),a
; DIVISOR = 255
ld a,255
ld (DIVISOR),a
if BROKEN
; NUMERATOR_HI = 0
ld a,0
ld (NUMERATOR_HI),a
endif
call checked_step
call emit_state
ret
else
; NUMERATOR_LO = 232
ld a,232
ld (NUMERATOR_LO),a
; NUMERATOR_HI = 3
ld a,3
ld (NUMERATOR_HI),a
; DIVISOR = 7
ld a,7
ld (DIVISOR),a
if BROKEN
; NUMERATOR_HI = 0
ld a,0
ld (NUMERATOR_HI),a
endif
call checked_step
call emit_state
endif
retCustomise it
Divide the largest word by the largest byte. 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:
QUOTIENT_LO: defb 0
QUOTIENT_HI: defb 0
REMAINDER: defb 0
VALID: defb 0
NUMERATOR_LO: defb 0
NUMERATOR_HI: defb 0
DIVISOR: defb 0
T0: defb 0
T1: defb 0
T2: 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 00 01 FF FF FF 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller drops the numerator’s high byte. The division remains internally consistent but answers a different question. Preserve both bytes when passing the numerator.
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:
QUOTIENT_LO: defb 0
QUOTIENT_HI: defb 0
REMAINDER: defb 0
VALID: defb 0
NUMERATOR_LO: defb 0
NUMERATOR_HI: defb 0
DIVISOR: defb 0
T0: defb 0
T1: defb 0
T2: 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 298 code bytes; public state is 7 bytes and private scratch is 3 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.
Sixteen stages independent of quotient size. Repeated subtraction is shorter for a tightly bounded small quotient but can take 65,535 subtractions for division by one. A 68000 DIVU path can exploit native word arithmetic after validating its quotient range.
Unsigned division rounds down. A signed quotient needs a stated rounding rule. The helper does not turn an arbitrary random byte modulo a range into an unbiased random choice.
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
Decimal formatting: Repeated quotient/remainder extraction is the arithmetic behind base-ten digits.