A character can face a target in eight directions without angles, trigonometry or a lookup table.

Use it in a project
Return DIRECTION=0 when points coincide, otherwise 1 north, 2 north-east, 3 east, 4 south-east, 5 south, 6 south-west, 7 west, 8 north-west. Screen Y increases downwards. Choose a cardinal direction when its absolute displacement is at least twice the other; remaining vectors are diagonal. DX/DY return unsigned magnitudes.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: DIRECTION, DX, DY, FROM_X, FROM_Y, TO_X, TO_Y. 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 eight_way_direction_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 eight_way_direction_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 | FROM_X/Y and TO_X/Y |
| Read after service | DIRECTION and DX/DY |
Compare a magnitude with half the other to avoid overflow from doubling a byte. Two sign flags select the quadrant after choosing horizontal, vertical or diagonal.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Choose an Eight-way Direction. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
eight_way_direction_init:
ld a,0
ld (DIRECTION),a
ld (DX),a
ld (DY),a
ld (FROM_X),a
ld (FROM_Y),a
ld (TO_X),a
ld (TO_Y),a
ld (T0),a
ld (T1),a
ld (T2),a
ret
eight_way_direction_step:
; T0 = 0
ld a,0
ld (T0),a
; T1 = 0
ld a,0
ld (T1),a
; if TO_X >= FROM_X:
ld a,(TO_X)
ld b,a
ld a,(FROM_X)
ld c,a
ld a,b
cp c
jp nc,eight_way_direction_L1
jp eight_way_direction_L2
eight_way_direction_L1:
; DX = TO_X - FROM_X
ld a,(TO_X)
ld b,a
ld a,(FROM_X)
ld c,a
ld a,b
sub c
ld (DX),a
jp eight_way_direction_L3
eight_way_direction_L2:
; DX = FROM_X - TO_X
ld a,(FROM_X)
ld b,a
ld a,(TO_X)
ld c,a
ld a,b
sub c
ld (DX),a
; T0 = 1
ld a,1
ld (T0),a
eight_way_direction_L3:
; if TO_Y >= FROM_Y:
ld a,(TO_Y)
ld b,a
ld a,(FROM_Y)
ld c,a
ld a,b
cp c
jp nc,eight_way_direction_L4
jp eight_way_direction_L5
eight_way_direction_L4:
; DY = TO_Y - FROM_Y
ld a,(TO_Y)
ld b,a
ld a,(FROM_Y)
ld c,a
ld a,b
sub c
ld (DY),a
jp eight_way_direction_L6
eight_way_direction_L5:
; DY = FROM_Y - TO_Y
ld a,(FROM_Y)
ld b,a
ld a,(TO_Y)
ld c,a
ld a,b
sub c
ld (DY),a
; T1 = 1
ld a,1
ld (T1),a
eight_way_direction_L6:
; DIRECTION = 0
ld a,0
ld (DIRECTION),a
; if DX == 0:
ld a,(DX)
cp 0
jp z,eight_way_direction_L7
jp eight_way_direction_L8
eight_way_direction_L7:
; if DY == 0:
ld a,(DY)
cp 0
jp z,eight_way_direction_L10
jp eight_way_direction_L11
eight_way_direction_L10:
; return
ret
jp eight_way_direction_L12
eight_way_direction_L11:
eight_way_direction_L12:
jp eight_way_direction_L9
eight_way_direction_L8:
eight_way_direction_L9:
; T2 = DY >> 1
ld a,(DY)
srl a
ld (T2),a
; if DX <= T2:
ld a,(DX)
ld b,a
ld a,(T2)
ld c,a
ld a,b
cp c
jp c,eight_way_direction_L13
jp z,eight_way_direction_L13
jp eight_way_direction_L14
eight_way_direction_L13:
; DIRECTION = 5
ld a,5
ld (DIRECTION),a
; if T1 != 0:
ld a,(T1)
cp 0
jp nz,eight_way_direction_L16
jp eight_way_direction_L17
eight_way_direction_L16:
; DIRECTION = 1
ld a,1
ld (DIRECTION),a
jp eight_way_direction_L18
eight_way_direction_L17:
eight_way_direction_L18:
; return
ret
jp eight_way_direction_L15
eight_way_direction_L14:
eight_way_direction_L15:
; T2 = DX >> 1
ld a,(DX)
srl a
ld (T2),a
; if DY <= T2:
ld a,(DY)
ld b,a
ld a,(T2)
ld c,a
ld a,b
cp c
jp c,eight_way_direction_L19
jp z,eight_way_direction_L19
jp eight_way_direction_L20
eight_way_direction_L19:
; DIRECTION = 3
ld a,3
ld (DIRECTION),a
; if T0 != 0:
ld a,(T0)
cp 0
jp nz,eight_way_direction_L22
jp eight_way_direction_L23
eight_way_direction_L22:
; DIRECTION = 7
ld a,7
ld (DIRECTION),a
jp eight_way_direction_L24
eight_way_direction_L23:
eight_way_direction_L24:
; return
ret
jp eight_way_direction_L21
eight_way_direction_L20:
eight_way_direction_L21:
; DIRECTION = 4
ld a,4
ld (DIRECTION),a
; if T0 != 0:
ld a,(T0)
cp 0
jp nz,eight_way_direction_L25
jp eight_way_direction_L26
eight_way_direction_L25:
; DIRECTION = 6
ld a,6
ld (DIRECTION),a
jp eight_way_direction_L27
eight_way_direction_L26:
eight_way_direction_L27:
; if T1 != 0:
ld a,(T1)
cp 0
jp nz,eight_way_direction_L28
jp eight_way_direction_L29
eight_way_direction_L28:
; DIRECTION = 2
ld a,2
ld (DIRECTION),a
; if T0 != 0:
ld a,(T0)
cp 0
jp nz,eight_way_direction_L31
jp eight_way_direction_L32
eight_way_direction_L31:
; DIRECTION = 8
ld a,8
ld (DIRECTION),a
jp eight_way_direction_L33
eight_way_direction_L32:
eight_way_direction_L33:
jp eight_way_direction_L30
eight_way_direction_L29:
eight_way_direction_L30:
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:
DIRECTION: defb 0
DX: defb 0
DY: defb 0
FROM_X: defb 0
FROM_Y: defb 0
TO_X: defb 0
TO_Y: 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
; FROM_X = 100
ld a,100
ld (FROM_X),a
; FROM_Y = 100
ld a,100
ld (FROM_Y),a
; TO_X = 90
ld a,90
ld (TO_X),a
; TO_Y = 90
ld a,90
ld (TO_Y),a
if BROKEN
; TO_Y = FROM_Y
ld a,(FROM_Y)
ld (TO_Y),a
endif
call checked_step
call emit_state
ret
else
; TO_X = 10
ld a,10
ld (TO_X),a
; TO_Y = 3
ld a,3
ld (TO_Y),a
if BROKEN
; TO_Y = FROM_Y
ld a,(FROM_Y)
ld (TO_Y),a
endif
call checked_step
call emit_state
endif
retCustomise it
Face a target north-west of the origin. 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:
DIRECTION: defb 0
DX: defb 0
DY: defb 0
FROM_X: defb 0
FROM_Y: defb 0
TO_X: defb 0
TO_Y: 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 08 0A 0A 64 64 5A 5A 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller flattens the target’s vertical coordinate. DY becomes zero instead of three in the normal client; a steeper vector can also acquire the wrong facing. Preserve both coordinates.
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:
DIRECTION: defb 0
DX: defb 0
DY: defb 0
FROM_X: defb 0
FROM_Y: defb 0
TO_X: defb 0
TO_Y: 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 350 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.
Constant work with no table. A finer angular quantiser can use a ratio table; continuous steering needs a different representation.
These sectors use a 2:1 slope threshold, not exact 22.5-degree boundaries. This selects facing; it does not normalise movement speed.
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
Four-way input: Facing selection and movement constraints can use different direction sets.