The Bresenham kernel draws from current point (2, 3) to target (27, 18), including both endpoints. DRAWTO names this algorithm here; it is not an additional X.DRAWTO instruction.
Scientific workshop board with German and English labels. Open at full size

DRAWTO: line rasterization with assembly and XCODE

The Bresenham kernel draws from current point (2, 3) to target (27, 18), including both endpoints. DRAWTO names this algorithm here; it is not an additional X.DRAWTO instruction.

Natively checked: all three variants at O0 through O3. Measured with the released V8 compiler (PBXB64 V23).

Arithmetic and memory contract

dx = |x₁ − x₀|; dy = −|y₁ − y₀|; error = dx + dy; e₂ = 2 × error

x₀/y₀ are the starting coordinates, x₁/y₁ the target. sx and sy are each +1 or -1. Each step saves e₂ once: if e₂ >= dy, apply error += dy and x += sx; if e₂ <= dx, apply error += dx and y += sy. Both conditions use the same e₂.

Natively checked pixel bufferx →y↓(2,3) → (27,18) | 26 pixels
Natively checked line from (2, 3) to (27, 18): 26 set pixels.
  1. Both endpoints are checked before the first pixel access. The XCODE variant checks them in its BASIC harness; the assembly kernel uses CMP/JAE. An invalid endpoint leaves the buffer unchanged. This example rejects the entire line; it does not clip it to the image boundary.
  2. X.ABS, X.NEG and X.IF determine distances and directions for all eight octants. I64 holds signed coordinates and the error value; the fixed 32 x 24 bounds keep intermediate values small here.
  3. Two small XCODE blocks share the work: the first stores distances and directions in QUAD slots; the second draws. Both variants prepare the fixed line once. The outer X.FOR repeats rasterization; X.DO/X.LOOP forms the pixel loop. X.BREAK ends it after the target pixel.
  4. Actual output remains ordinary assembly: MOV DWORD PTR [V(base)+V(work)*4], V(color). work first holds the pixel index and then e2. I64 PTR in X.IF specifies the comparison type; QWORD PTR in X.LOAD/X.ADD specifies memory width.
  5. Pure assembly names physical registers and jump targets itself. It saves and restores the nonvolatile Win64 registers it uses. XCODE handles the required register assignment and preservation.
  6. Full comparison covers horizontal, vertical, steep and shallow lines in both directions, a single point, all octants and invalid endpoints. The default example should produce 26 pixels.

Pure x64 assembly

The following kernel appears in full in the downloadable program. The shared BASIC harness supplies buffers and timing.

#ASM
push rbx
 push rsi
 push rdi
 push r12
 push r13
 push r14
 push r15
 mov r14, QWORD PTR [end_x]
 mov r15, QWORD PTR [end_y]
 mov r8, QWORD PTR [buffer_addr]
 mov r10d, 0xFF0080FF
 bswap r10d
 ror r10d, 8
 mov r11, QWORD PTR [rounds]
 mov r12, QWORD PTR [start_x]
 mov r13, QWORD PTR [start_y]
 cmp r12, 32
 jae draw_done
 cmp r13, 24
 jae draw_done
 cmp r14, 32
 jae draw_done
 cmp r15, 24
 jae draw_done
 mov rbx, r14
 sub rbx, r12
 mov rdi, 1
 jge draw_dx_ready
 neg rbx
 mov rdi, -1
draw_dx_ready:
 mov rsi, r15
 sub rsi, r13
 mov r9, 1
 jge draw_dy_ready
 neg rsi
 mov r9, -1
draw_dy_ready:
 neg rsi
draw_repeat:
 mov r12, QWORD PTR [start_x]
 mov r13, QWORD PTR [start_y]
 mov rdx, rbx
 add rdx, rsi
draw_pixel:
 mov rcx, r13
 imul rcx, rcx, 32
 add rcx, r12
 mov DWORD PTR [r8+rcx*4], r10d
 cmp r12, r14
 jne draw_step
 cmp r13, r15
 je draw_next
draw_step:
 mov rax, rdx
 add rax, rax
 cmp rax, rsi
 jl draw_no_x
 add rdx, rsi
 add r12, rdi
draw_no_x:
 cmp rax, rbx
 jg draw_pixel
 add rdx, rbx
 add r13, r9
 jmp draw_pixel
draw_next:
 dec r11
 jnz draw_repeat
draw_done:
 pop r15
 pop r14
 pop r13
 pop r12
 pop rdi
 pop rsi
 pop rbx
#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.

IF start_x < 0 OR start_x >= 32 OR start_y < 0 OR start_y >= 24 THEN EXIT SUB
IF end_x < 0 OR end_x >= 32 OR end_y < 0 OR end_y >= 24 THEN EXIT SUB
' Small first block prepares signed distances and directions.
#XCODE OPT=2, SPILL=AUTO, FEATURES=BASELINE
 X.VREG x:I64
 X.VREG y:I64
 X.VREG work:I64
 X.LOAD V(x), QWORD PTR [start_x]
 X.LOAD V(y), QWORD PTR [start_y]
 X.LOAD V(work), QWORD PTR [end_x]
 X.SUB V(work), V(x)
 X.ABS V(work)
 X.STORE QWORD PTR [delta_x], V(work)
 X.MOVE V(work), 1
 X.IF V(x) >= I64 PTR [end_x]
  X.MOVE V(work), -1
 X.ENDIF
 X.STORE QWORD PTR [direction_x], V(work)
 X.LOAD V(work), QWORD PTR [end_y]
 X.SUB V(work), V(y)
 X.ABS V(work)
 X.NEG V(work)
 X.STORE QWORD PTR [delta_y], V(work)
 X.MOVE V(work), 1
 X.IF V(y) >= I64 PTR [end_y]
  X.MOVE V(work), -1
 X.ENDIF
 X.STORE QWORD PTR [direction_y], V(work)
#ENDX
' Second block rasterizes the prepared line, using ordinary memory operands.
#XCODE OPT=2, SPILL=AUTO, FEATURES=BASELINE
 X.VREG x:I64
 X.VREG y:I64
 X.VREG error:I64
 X.VREG work:I64
 X.VREG base:PTR
 X.VREG pass:U64
 X.VREG limit:U64
 X.VREG color:U32
 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.FOR V(pass), 0, V(limit), 1
  X.LOAD V(x), QWORD PTR [start_x]
  X.LOAD V(y), QWORD PTR [start_y]
  X.LOAD V(error), QWORD PTR [delta_x]
  X.ADD V(error), QWORD PTR [delta_y]
  X.DO
   X.MOVE V(work), V(y)
   X.MUL V(work), 32
   X.ADD V(work), V(x)
   X.RAW
    mov DWORD PTR [V(base)+V(work)*4], V(color)
   X.ENDRAW
   X.IF V(x) == I64 PTR [end_x]
    X.IF V(y) == I64 PTR [end_y]
     X.BREAK
    X.ENDIF
   X.ENDIF
   X.MOVE V(work), V(error)
   X.ADD V(work), V(work)
   X.IF V(work) >= I64 PTR [delta_y]
    X.ADD V(error), QWORD PTR [delta_y]
    X.ADD V(x), QWORD PTR [direction_x]
   X.ENDIF
   X.IF V(work) <= I64 PTR [delta_x]
    X.ADD V(error), QWORD PTR [delta_x]
    X.ADD V(y), QWORD PTR [direction_y]
   X.ENDIF
  X.LOOP
 X.NEXT
#ENDX

Why 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 assembly

Third 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
push rbx
 push rsi
 push rdi
 push r12
 push r13
 push r14
 push r15
 mov r14, QWORD PTR [end_x]
 mov r15, QWORD PTR [end_y]
 mov r8, QWORD PTR [buffer_addr]
 mov r11, QWORD PTR [rounds]
 mov r12, QWORD PTR [start_x]
 mov r13, QWORD PTR [start_y]
 cmp r12, 32
 jae hybrid_draw_done
 cmp r13, 24
 jae hybrid_draw_done
 cmp r14, 32
 jae hybrid_draw_done
 cmp r15, 24
 jae hybrid_draw_done
 mov rbx, r14
 sub rbx, r12
 mov rdi, 1
 jge hybrid_draw_dx_ready
 neg rbx
 mov rdi, -1
hybrid_draw_dx_ready:
 mov rsi, r15
 sub rsi, r13
 mov r9, 1
 jge hybrid_draw_dy_ready
 neg rsi
 mov r9, -1
hybrid_draw_dy_ready:
 neg rsi
hybrid_draw_repeat:
 mov r12, QWORD PTR [start_x]
 mov r13, QWORD PTR [start_y]
 mov rdx, rbx
 add rdx, rsi
hybrid_draw_pixel:
 mov rcx, r13
 imul rcx, rcx, 32
 add rcx, r12
 mov DWORD PTR [r8+rcx*4], V(color)
 cmp r12, r14
 jne hybrid_draw_step
 cmp r13, r15
 je hybrid_draw_next
hybrid_draw_step:
 mov rax, rdx
 add rax, rax
 cmp rax, rsi
 jl hybrid_draw_no_x
 add rdx, rsi
 add r12, rdi
hybrid_draw_no_x:
 cmp rax, rbx
 jg hybrid_draw_pixel
 add rdx, rbx
 add r13, r9
 jmp hybrid_draw_pixel
hybrid_draw_next:
 dec r11
 jnz hybrid_draw_repeat
hybrid_draw_done:
 pop r15
 pop r14
 pop r13
 pop r12
 pop rdi
 pop rsi
 pop rbx
 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: 100,000 × 26 line pixel.

VariantMedian (ms)Min–Max (ms)ns / line pixel
ASM3.2333.167–3.7491.244
Structured XCODE12.86212.601–13.8964.947
HYBRID: ASM + XCODE3.1783.172–3.6471.222

HYBRID/ASM: 0.983×; XCODE/ASM: 3.978×. 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.500 ms, XCODE 13.545 ms, HYBRID 3.439 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.

All raw samples, cases and program hashes

Run the complete program yourself

PBXB64.exe xcode-drawto.pb -O2 -o graphics.exe
.\graphics.exe ASM 100000 2 3 27 18 0
.\graphics.exe XCODE 100000 2 3 27 18 0
.\graphics.exe HYBRID 100000 2 3 27 18 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 ZIP

Re-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.