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:
lda #0
sta QUOTIENT_LO
sta QUOTIENT_HI
sta REMAINDER
sta VALID
sta NUMERATOR_LO
sta NUMERATOR_HI
sta DIVISOR
sta T0
sta T1
sta T2
rts
word_division_step:
; QUOTIENT_LO = 0
lda #0
sta QUOTIENT_LO
; QUOTIENT_HI = 0
lda #0
sta QUOTIENT_HI
; REMAINDER = 0
lda #0
sta REMAINDER
; VALID = 0
lda #0
sta VALID
; if DIVISOR == 0:
lda DIVISOR
cmp #0
beq word_division_L4
word_division_L5:
jmp word_division_L2
word_division_L4:
jmp word_division_L1
word_division_L1:
; return
rts
jmp word_division_L3
word_division_L2:
word_division_L3:
; VALID = 1
lda #1
sta VALID
; QUOTIENT_LO = NUMERATOR_LO
lda NUMERATOR_LO
sta QUOTIENT_LO
; QUOTIENT_HI = NUMERATOR_HI
lda NUMERATOR_HI
sta QUOTIENT_HI
; T0 = 16
lda #16
sta T0
; while T0 != 0:
word_division_L6:
lda T0
cmp #0
bne word_division_L9
word_division_L10:
jmp word_division_L8
word_division_L9:
jmp word_division_L7
word_division_L7:
; T1 = QUOTIENT_HI >> 7
lda QUOTIENT_HI
lsr
lsr
lsr
lsr
lsr
lsr
lsr
sta T1
; T2 = QUOTIENT_LO >> 7
lda QUOTIENT_LO
lsr
lsr
lsr
lsr
lsr
lsr
lsr
sta T2
; QUOTIENT_HI = QUOTIENT_HI << 1
lda QUOTIENT_HI
asl
sta QUOTIENT_HI
; QUOTIENT_HI = QUOTIENT_HI | T2
lda QUOTIENT_HI
ora T2
sta QUOTIENT_HI
; QUOTIENT_LO = QUOTIENT_LO << 1
lda QUOTIENT_LO
asl
sta QUOTIENT_LO
; T2 = REMAINDER >> 7
lda REMAINDER
lsr
lsr
lsr
lsr
lsr
lsr
lsr
sta T2
; REMAINDER = REMAINDER << 1
lda REMAINDER
asl
sta REMAINDER
; REMAINDER = REMAINDER | T1
lda REMAINDER
ora T1
sta REMAINDER
; if T2 != 0:
lda T2
cmp #0
bne word_division_L14
word_division_L15:
jmp word_division_L12
word_division_L14:
jmp word_division_L11
word_division_L11:
; REMAINDER = REMAINDER - DIVISOR
lda REMAINDER
sec
sbc DIVISOR
sta REMAINDER
; QUOTIENT_LO = QUOTIENT_LO | 1
lda QUOTIENT_LO
ora #1
sta QUOTIENT_LO
jmp word_division_L13
word_division_L12:
; if REMAINDER >= DIVISOR:
lda REMAINDER
cmp DIVISOR
bcs word_division_L19
word_division_L20:
jmp word_division_L17
word_division_L19:
jmp word_division_L16
word_division_L16:
; REMAINDER = REMAINDER - DIVISOR
lda REMAINDER
sec
sbc DIVISOR
sta REMAINDER
; QUOTIENT_LO = QUOTIENT_LO | 1
lda QUOTIENT_LO
ora #1
sta QUOTIENT_LO
jmp word_division_L18
word_division_L17:
word_division_L18:
word_division_L13:
; T0 = T0 - 1
lda T0
sec
sbc #1
sta T0
jmp word_division_L6
word_division_L8:
rts
component_end:Machine contract
A/X/status may change; Y is preserved. The 2A03 has no decimal arithmetic mode. A direct JSR 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 NROM caller owns PPU setup and background display, with NMI and DMC disabled. Table and packet sizes are bounded to fit the cartridge caller and internal RAM; the helper owns no PPU registers.
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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
QUOTIENT_LO=$80
QUOTIENT_HI=$81
REMAINDER=$82
VALID=$83
NUMERATOR_LO=$84
NUMERATOR_HI=$85
DIVISOR=$86
T0=$87
T1=$88
T2=$89
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=0
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0A client retaining state between calls
preview_rules:
.if CUSTOM
; NUMERATOR_LO = 255
lda #255
sta NUMERATOR_LO
; NUMERATOR_HI = 255
lda #255
sta NUMERATOR_HI
; DIVISOR = 255
lda #255
sta DIVISOR
.if BROKEN
; NUMERATOR_HI = 0
lda #0
sta NUMERATOR_HI
.endif
jsr checked_step
jsr emit_state
rts
.else
; NUMERATOR_LO = 232
lda #232
sta NUMERATOR_LO
; NUMERATOR_HI = 3
lda #3
sta NUMERATOR_HI
; DIVISOR = 7
lda #7
sta DIVISOR
.if BROKEN
; NUMERATOR_HI = 0
lda #0
sta NUMERATOR_HI
.endif
jsr checked_step
jsr emit_state
.endif
rtsCustomise 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
QUOTIENT_LO=$80
QUOTIENT_HI=$81
REMAINDER=$82
VALID=$83
NUMERATOR_LO=$84
NUMERATOR_HI=$85
DIVISOR=$86
T0=$87
T1=$88
T2=$89
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=1
BROKEN=0
COYOTE_STEPS=2
BUFFER_STEPS=2
TEXT_CAP=4
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=49
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0The 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
QUOTIENT_LO=$80
QUOTIENT_HI=$81
REMAINDER=$82
VALID=$83
NUMERATOR_LO=$84
NUMERATOR_HI=$85
DIVISOR=$86
T0=$87
T1=$88
T2=$89
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=1
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0
Cost and alternatives
The measured helper extent is 210 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 NTSC NROM, background only, DMC/NMI disabled, 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.