
Set a pixel: assembly and XCODE
A 32 x 24 BGRA32 pixel buffer receives an orange point at (5, 3). XCODE calculates the index with typed registers; an ordinary MOV instruction writes the pixel directly to memory.
Natively checked: all three variants at O0 through O3. Measured with the released V8 compiler (PBXB64 V23).
Arithmetic and memory contract
base is the address of the first pixel, x its column and y its row. Each BGRA32 pixel occupies 4 bytes. The byte offset for (5, 3) is 404. Rows have no additional padding.
- The BASIC harness provides a BYTE buffer. VARPTR supplies its address; a graphics handle would not be a valid pixel address.
- Both kernels reject coordinates outside 0 <= x < 32 and 0 <= y < 24. Pure assembly uses unsigned JAE; XCODE loads coordinates as U64. Negative inputs are therefore rejected too.
- RGB32 denotes the R, G, B, A byte sequence. On x64, 0xFF0080FF contains FF 80 00 FF. X.COLOR.CONVERT produces BGRA32: 00 80 FF FF, or DWORD value 0xFFFF8000. Pure assembly performs the same rearrangement using BSWAP and ROR.
- X.MUL and X.ADD calculate the pixel index. Inside X.RAW, V(base) becomes its assigned physical register; MOV with DWORD PTR writes exactly four bytes. X.ENDRAW returns to XCODE instructions.
- Verification compares the entire buffer: precisely this pixel is set and all other pixels are zero. Two guard bytes surrounding the payload remain unchanged.
Pure x64 assembly
The following kernel appears in full in the downloadable program. The shared BASIC harness supplies buffers and timing.
#ASM mov rcx, QWORD PTR [start_x] mov rdx, QWORD PTR [start_y] cmp rcx, 32 jae point_done cmp rdx, 24 jae point_done mov r8, QWORD PTR [buffer_addr] mov r10d, 0xFF0080FF bswap r10d ror r10d, 8 mov r11, QWORD PTR [rounds] point_repeat: mov rax, rdx imul rax, rax, 32 add rax, rcx mov DWORD PTR [r8+rax*4], r10d dec r11 jnz point_repeat point_done: #ENDASM
Structured XCODE with an assembly pixel store
The following kernel appears in full in the downloadable program. The shared BASIC harness supplies buffers and timing.
#XCODE OPT=2, SPILL=AUTO, FEATURES=BASELINE
X.VREG x:U64
X.VREG y:U64
X.VREG offset:U64
X.VREG base:PTR
X.VREG color:U32
X.VREG pass:U64
X.VREG limit:U64
X.LOAD V(x), QWORD PTR [start_x]
X.LOAD V(y), QWORD PTR [start_y]
X.LOAD V(base), QWORD PTR [buffer_addr]
X.LOAD V(limit), QWORD PTR [rounds]
X.MOVE V(color), 0xFF0080FF
X.COLOR.CONVERT V(color), V(color), RGB32, BGRA32
X.IF V(x) < 32
X.IF V(y) < 24
X.FOR V(pass), 0, V(limit), 1
X.MOVE V(offset), V(y)
X.MUL V(offset), 32
X.ADD V(offset), V(x)
X.RAW
mov DWORD PTR [V(base)+V(offset)*4], V(color)
X.ENDRAW
X.NEXT
X.ENDIF
X.ENDIF
#ENDXWhy does this remain native assembly?
XCODE is lowered to concrete x64 instructions during compilation. X.RAW contains regular assembly; V(name) and V8(name) refer to assigned physical registers or their byte view. There is no XCODE runtime interpreter here. Types, loops and X.CLAMP simplify expressing intent but do not guarantee the speed of a hand-tuned kernel.
Open the actual expanded XCODE assemblyThird variant: assembly with selective XCODE macros
This is the macro approach for assembly programmers: X.COLOR.CONVERT simplifies the one-time color rearrangement; X.BIND explicitly binds color to R10D. Coordinates, Bresenham steps and loops inside X.RAW remain ordinary register instructions. SPILL=ERROR disallows automatically spilled virtual values.
' The XCODE color macro runs once; the hot loop is ordinary register assembly. #XCODE OPT=2, SPILL=ERROR, FEATURES=BASELINE X.VREG color:U32 X.BIND V(color), R10D, INOUT X.MOVE V(color), 0xFF0080FF X.COLOR.CONVERT V(color), V(color), RGB32, BGRA32 X.RAW mov rcx, QWORD PTR [start_x] mov rdx, QWORD PTR [start_y] cmp rcx, 32 jae hybrid_point_done cmp rdx, 24 jae hybrid_point_done mov r8, QWORD PTR [buffer_addr] mov r11, QWORD PTR [rounds] hybrid_point_repeat: mov rax, rdx imul rax, rax, 32 add rax, rcx mov DWORD PTR [r8+rax*4], V(color) dec r11 jnz hybrid_point_repeat hybrid_point_done: X.ENDRAW X.UNBIND V(color) #ENDX
The color-rearrangement expression is shorter: one X.COLOR.CONVERT instead of BSWAP plus ROR. Declaration, binding and X.UNBIND add setup lines; the complete routine does not automatically have fewer source lines or instructions. This work remains outside the repeated loop.
The actual emitted inner pixel loop has the same instructions as the handwritten ASM version. It contains no virtual-register stack accesses. Prologue, setup and ordinary memory accesses remain visible in the complete listing.
X.COLOR.CONVERT can prepare the color; X.PIXELS.CONVERT can convert an entire packed buffer. Neither replaces DRAWTO/Bresenham. This 32-bit buffer needs no additional buffer conversion. An RGB24 pixel must not be written with a DWORD store: it occupies exactly three bytes. 24-/32-bit formats and conversions
Measured runtime: three variants
Per sample: 10,000,000 × 1 pixel.
| Variant | Median (ms) | Min–Max (ms) | ns / pixel |
|---|---|---|---|
| ASM | 3.744 | 3.556–4.054 | 0.374 |
| Structured XCODE | 21.251 | 20.818–21.989 | 2.125 |
| HYBRID: ASM + XCODE | 3.728 | 3.651–4.014 | 0.373 |
HYBRID/ASM: 0.996×; XCODE/ASM: 5.676×. HYBRID and ASM have comparable speed here; their min/max ranges overlap. This does not establish a HYBRID speed advantage.
First measurement with the experimental V22 build (2026-10-02, medians): ASM 3.783 ms, XCODE 22.588 ms, HYBRID 4.032 ms. The V8 compiler generates the same machine instructions for these kernels; the differences of a few percent are within measurement scatter and are not a compiler speed gain.
Measured on 2026-10-05 using Intel(R) Core(TM) i9-7980XE CPU @ 2.60GHz, Windows build 26300. QPC includes dispatch and the kernel call; process startup, buffer initialization and output are excluded. All three variants use identical inputs in the same program. Two warm-up triplets are discarded, followed by nine measured triplets with rotating order. All result bytes are checked against the same reference.
These scalar tests repeat the same point or line in small hot buffers. Other system work was not disabled; min/max and raw samples are provided. This is not a general SIMD, image-processing or graphics-device benchmark.
PBXB64 version 23.0.0-V23 from the released PBX-Compiler V8 package (PBX-Basic_08.zip, digitally signed); SHA-256 465c6d67dae523e4c3c75ae8aa3975c602a5839b3e6e82e8a7cad9a5c67fe3e9. The checks cover these tutorial examples; they are not a general compiler qualification.
Run the complete program yourself
PBXB64.exe xcode-set-pixel.pb -O2 -o graphics.exe .\graphics.exe ASM 10000000 5 3 0 0 0 .\graphics.exe XCODE 10000000 5 3 0 0 0 .\graphics.exe HYBRID 10000000 5 3 0 0 0
Arguments: variant, repetitions, start-x, start-y, target-x, target-y, brightness/factor. The harness requires 1 through 100000000 repetitions and validates the brightness/factor range. Exit0 checks guard bytes; TIMING contains QPC ticks and frequency, BYTE contains result bytes. Compare the whole buffer as well as the exit code.
Download all three variants and timing harnessAll four graphics examples as ZIPRe-checked with the V8 compiler on 2026-10-05: 24 O0–O3 program variants of the four graphics examples and all 276 published output and boundary cases produce byte-identical results.
More graphics examples
PBXB64 is part of the PBX-Compiler package. Free for private use, cleanly licensed for business.