Turning rows into columns is the building block behind rotating small glyphs and adapting bitmap layouts.

Use it in a project
C0..C7 are eight monochrome source rows, bit seven leftmost. Return R0..R7 with row and column exchanged: source pixel (x,y) becomes destination (y,x). Preserve source rows. This is a diagonal reflection; combine with mirroring for a quarter turn.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: R0, R1, R2, R3, R4, R5, R6, R7, C0, C1, C2, C3, C4, C5, C6, C7. They occupy 16 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, EXPR_SAVE (6 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call bitmap_transpose_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 bitmap_transpose_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 | C0..C7 |
| Read after service | R0..R7 |
Each output row corresponds to one input column. Test that bit in every source row and place it at the matching output position.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Transpose an Eight-by-eight Bitmap. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
bitmap_transpose_init:
ld a,0
ld (R0),a
ld (R1),a
ld (R2),a
ld (R3),a
ld (R4),a
ld (R5),a
ld (R6),a
ld (R7),a
ld (C0),a
ld (C1),a
ld (C2),a
ld (C3),a
ld (C4),a
ld (C5),a
ld (C6),a
ld (C7),a
ld (T0),a
ld (T1),a
ld (T2),a
ld (T3),a
ld (T4),a
ld (EXPR_SAVE),a
ret
bitmap_transpose_step:
; T0 = 0
ld a,0
ld (T0),a
; while T0 < 8:
bitmap_transpose_L1:
ld a,(T0)
cp 8
jp c,bitmap_transpose_L2
jp bitmap_transpose_L3
bitmap_transpose_L2:
; R[T0] = 0
ld a,0
ld c,a
ld a,(T0)
ld e,a
ld d,0
ld hl,R0
add hl,de
ld (hl),c
; T1 = 0
ld a,0
ld (T1),a
; T2 = 128
ld a,128
ld (T2),a
; while T1 < 8:
bitmap_transpose_L4:
ld a,(T1)
cp 8
jp c,bitmap_transpose_L5
jp bitmap_transpose_L6
bitmap_transpose_L5:
; T3 = C[T1]
ld a,(T1)
ld e,a
ld d,0
ld hl,C0
add hl,de
ld a,(hl)
ld (T3),a
; T4 = T0
ld a,(T0)
ld (T4),a
; while T4 != 0:
bitmap_transpose_L7:
ld a,(T4)
cp 0
jp nz,bitmap_transpose_L8
jp bitmap_transpose_L9
bitmap_transpose_L8:
; T3 = T3 << 1
ld a,(T3)
sla a
ld (T3),a
; T4 = T4 - 1
ld a,(T4)
sub 1
ld (T4),a
jp bitmap_transpose_L7
bitmap_transpose_L9:
; T3 = T3 & 128
ld a,(T3)
and 128
ld (T3),a
; if T3 != 0:
ld a,(T3)
cp 0
jp nz,bitmap_transpose_L10
jp bitmap_transpose_L11
bitmap_transpose_L10:
; T3 = R[T0]
ld a,(T0)
ld e,a
ld d,0
ld hl,R0
add hl,de
ld a,(hl)
ld (T3),a
; T3 = T3 | T2
ld a,(T3)
ld b,a
ld a,(T2)
ld c,a
ld a,b
or c
ld (T3),a
; R[T0] = T3
ld a,(T3)
ld c,a
ld a,(T0)
ld e,a
ld d,0
ld hl,R0
add hl,de
ld (hl),c
jp bitmap_transpose_L12
bitmap_transpose_L11:
bitmap_transpose_L12:
; T2 = T2 >> 1
ld a,(T2)
srl a
ld (T2),a
; T1 = T1 + 1
ld a,(T1)
add a,1
ld (T1),a
jp bitmap_transpose_L4
bitmap_transpose_L6:
; T0 = T0 + 1
ld a,(T0)
add a,1
ld (T0),a
jp bitmap_transpose_L1
bitmap_transpose_L3:
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:
R0: defb 0
R1: defb 0
R2: defb 0
R3: defb 0
R4: defb 0
R5: defb 0
R6: defb 0
R7: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
C4: defb 0
C5: defb 0
C6: defb 0
C7: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: 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
; C0 = 128
ld a,128
ld (C0),a
; C1 = 64
ld a,64
ld (C1),a
; C2 = 32
ld a,32
ld (C2),a
; C3 = 16
ld a,16
ld (C3),a
; C4 = 8
ld a,8
ld (C4),a
; C5 = 4
ld a,4
ld (C5),a
; C6 = 2
ld a,2
ld (C6),a
; C7 = 1
ld a,1
ld (C7),a
if BROKEN
; C0 = 0
ld a,0
ld (C0),a
endif
call checked_step
call emit_state
ret
else
; C0 = 255
ld a,255
ld (C0),a
; C1 = 128
ld a,128
ld (C1),a
; C2 = 128
ld a,128
ld (C2),a
; C3 = 128
ld a,128
ld (C3),a
if BROKEN
; C0 = 0
ld a,0
ld (C0),a
endif
call checked_step
call emit_state
endif
retCustomise it
Transpose an identity diagonal, which should remain identical. 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:
R0: defb 0
R1: defb 0
R2: defb 0
R3: defb 0
R4: defb 0
R5: defb 0
R6: defb 0
R7: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
C4: defb 0
C5: defb 0
C6: defb 0
C7: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: 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 80 40 20 10 08 04 02 01. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller clears the first row before transforming it. The destination’s first column loses those pixels. Keep all eight source rows until the transform has completed.
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:
R0: defb 0
R1: defb 0
R2: defb 0
R3: defb 0
R4: defb 0
R5: defb 0
R6: defb 0
R7: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
C4: defb 0
C5: defb 0
C6: defb 0
C7: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: 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 265 code bytes; public state is 16 bytes and private scratch is 6 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.
Sixty-four pixel tests plus shifts. A butterfly bit-swap algorithm is faster but less direct; precomputed transformed art costs storage instead of runtime.
Source and destination bindings must be separate. This is a data transformation, not a machine-specific draw routine; screen attributes and colour planes need separate handling.
Execution evidence
Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 62 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.
Existing alternatives
The existing full-glyph transform covers rotation and reflection with an overlap-safe pointer interface on its documented machines. This separate-buffer helper exposes transposition directly for row/column conversion. A transpose reflects across a diagonal; it must not silently substitute for a clockwise turn. Choose the existing transform for its quarter-turn flags or in-place operation.
Where it fits
Bitmap mirroring: Transpose plus a row mirror produces a quarter-turn rotation.