Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

10.2 KB
Raw Blame
; ============================================================================
; src/demos.asm — Handwritten Micro-ASM Demo Programs (Assembled Live by uasm!)
; Zero-App Ephemeral OS (Stage 1)
;
; All demos use ONLY the bare 5-primitive ABI (fb_ptr, stride, input_state,
; ticks_ms, exit) — ZERO widgets, ZERO draw_rect/draw_pixel helpers!
; ============================================================================

bits 64
default rel

section .rodata

global demo_mouse_src
global demo_mouse_len
global demo_mouse_frame_src
global demo_mouse_frame_len
global demo_pf_src
global demo_pf_len
global demo_hang_src
global demo_hang_len
global demo_ud_src
global demo_ud_len
global demo_de_src
global demo_de_len
global demo_ro_abi_src
global demo_ro_abi_len

; ============================================================================
; Demo 1: Interactive Mouse-Controlled Pixel Square (Zero Widgets!)
;   - Clears canvas to dark indigo with subtle 64px grid (pure fb_ptr writes)
;   - Draws a 64x64 bright pixel square centered on (mouse_x, mouse_y)
;   - Color reacts to mouse_buttons (LMB=bright magenta, RMB=gold, default=cyan)
;     plus subtle animation pulse from ticks_ms
;   - Draws bright white 10px crosshair at exact mouse center
;   - Exits cleanly when user presses 'q' (ASCII 0x71)
; ============================================================================
%macro emit_mouse_box 1
    db "; Demo 1: Bare-Metal Mouse Square (5 primitives, 0 widgets)", 10
    db "mov r12, [rdi + 0x00]     ; r12 = fb_ptr", 10
    db "mov r13, [rdi + 0x08]     ; r13 = stride (4096)", 10
    db "mov r14, [rdi + 0x18]     ; r14 = input_ptr", 10
    db "mov r15, rdi              ; r15 = abi_ptr", 10
    db "frame_loop:", 10
    db "  ; Check 'q' key for clean exit", 10
    db "  mov eax, [r14 + 0x14]   ; last_key", 10
    db "  cmp eax, 'q'", 10
    db "  je done_exit", 10
    db "  ; Clear 1024x736 canvas to dark grid using raw fb_ptr + stride", 10
    db "  xor r8d, r8d            ; y = 0", 10
    db "bg_y:", 10
    db "  cmp r8d, 736", 10
    db "  jae bg_done", 10
    db "  mov rax, r8", 10
    db "  imul rax, r13           ; y * stride", 10
    db "  lea r9, [r12 + rax]     ; row_ptr = fb_ptr + y*stride", 10
    db "  mov ecx, r8d", 10
    db "  and ecx, 63", 10
    db "  xor ebx, ebx            ; x = 0", 10
    db "bg_x:", 10
    db "  cmp ebx, 1024", 10
    db "  jae bg_y_next", 10
    db "  mov edx, 0x00101828     ; deep slate bg", 10
    db "  test ecx, ecx", 10
    db "  je bg_grid", 10
    db "  mov eax, ebx", 10
    db "  and eax, 63", 10
    db "  test eax, eax", 10
    db "  jne bg_put", 10
    db "bg_grid:", 10
    db "  mov edx, 0x001D2A42     ; subtle 64px grid line", 10
    db "bg_put:", 10
    db "  mov [r9 + rbx*4], edx", 10
    db "  inc ebx", 10
    db "  jmp bg_x", 10
    db "bg_y_next:", 10
    db "  inc r8d", 10
    db "  jmp bg_y", 10
    db "bg_done:", 10
    db "  ; Read mouse_x, mouse_y, mouse_buttons from InputState", 10
    db "  movsxd r10, dword [r14 + 0x00] ; mx", 10
    db "  movsxd r11, dword [r14 + 0x04] ; my", 10
    db "  mov eax, [r14 + 0x10]          ; buttons", 10
    db "  mov edx, 0x0000E5FF            ; default: bright cyan", 10
    db "  test eax, 1", 10
    db "  je chk_rmb", 10
    db "  mov edx, 0x00FF2A85            ; LMB: neon magenta", 10
    db "  jmp color_ok", 10
    db "chk_rmb:", 10
    db "  test eax, 2", 10
    db "  je color_pulse", 10
    db "  mov edx, 0x00FFD000            ; RMB: bright gold", 10
    db "  jmp color_ok", 10
    db "color_pulse:", 10
    db "  ; Subtle green channel pulse using ticks_ms", 10
    db "  mov rax, [r15 + 0x10]          ; ticks_ms", 10
    db "  shr rax, 4", 10
    db "  and eax, 0x3F", 10
    db "  shl eax, 8", 10
    db "  xor edx, eax", 10
    db "color_ok:", 10
    db "  ; Draw 64x64 square centered at (mx, my)", 10
    db "  mov r8, r11", 10
    db "  sub r8, 32              ; y0 = my - 32", 10
    db "  mov rcx, r11", 10
    db "  add rcx, 32             ; y1 = my + 32", 10
    db "sq_y:", 10
    db "  cmp r8, rcx", 10
    db "  jge sq_done", 10
    db "  cmp r8, 0", 10
    db "  jl sq_y_next", 10
    db "  cmp r8, 736", 10
    db "  jge sq_y_next", 10
    db "  mov rax, r8", 10
    db "  imul rax, r13", 10
    db "  lea r9, [r12 + rax]", 10
    db "  mov rbx, r10", 10
    db "  sub rbx, 32             ; x = mx - 32", 10
    db "  mov rsi, r10", 10
    db "  add rsi, 32             ; x1 = mx + 32", 10
    db "sq_x:", 10
    db "  cmp rbx, rsi", 10
    db "  jge sq_y_next", 10
    db "  cmp rbx, 0", 10
    db "  jl sq_x_next", 10
    db "  cmp rbx, 1024", 10
    db "  jge sq_x_next", 10
    db "  mov [r9 + rbx*4], edx", 10
    db "sq_x_next:", 10
    db "  inc rbx", 10
    db "  jmp sq_x", 10
    db "sq_y_next:", 10
    db "  inc r8", 10
    db "  jmp sq_y", 10
    db "sq_done:", 10
    db "  ; Draw bright white 4x4 core dot at (mx, my)", 10
    db "  mov rax, r11", 10
    db "  imul rax, r13", 10
    db "  lea r9, [r12 + rax]", 10
    db "  mov dword [r9 + r10*4], 0x00FFFFFF", 10
%if %1
    db "  pause", 10
    db "  jmp frame_loop", 10
%endif
    db "done_exit:", 10
    db "  exit 0", 10
%endmacro

demo_mouse_src:
    emit_mouse_box 1
demo_mouse_len equ $ - demo_mouse_src

demo_mouse_frame_src:
    emit_mouse_box 0
demo_mouse_frame_len equ $ - demo_mouse_frame_src

; ============================================================================
; Demo 2: Intentional #PF Page Fault Test (Unmapped / Forbidden Write)
;   - First paints a visible cyan band on the framebuffer (proving Ring 3 ran)
;   - Then deliberately writes to 0x00000000DEADBEEF -> triggers hardware #PF!
;   - Kernel catches #PF, displays red HUD with RIP, line#, CR2, and stays alive!
; ============================================================================
demo_pf_src:
    db "; Demo 2: Intentional #PF Crash Test (Ring 3 Isolation Proof)", 10
    db "mov r12, [rdi + 0x00]     ; fb_ptr", 10
    db "mov r13, [rdi + 0x08]     ; stride", 10
    db "xor ecx, ecx", 10
    db "paint_bar:", 10
    db "  cmp ecx, 200", 10
    db "  jae trigger_pf", 10
    db "  mov dword [r12 + rcx*4], 0x0000FFCC", 10
    db "  inc ecx", 10
    db "  jmp paint_bar", 10
    db "trigger_pf:", 10
    db "  mov rax, 0xDEADBEEF     ; unmapped / supervisor address", 10
    db "  mov dword [rax], 0x12345678 ; <-- MUST TRIGGER #PF HERE (line 13)", 10
    db "  exit 0", 10
demo_pf_len equ $ - demo_pf_src

; ============================================================================
; Demo 3: Infinite Loop Preemption Test (APIC Timer Watchdog Proof)
; ============================================================================
demo_hang_src:
    db "; Demo 3: Infinite Loop Test (APIC Timer Preemption Proof)", 10
    db "mov r12, [rdi + 0x00]", 10
    db "xor ecx, ecx", 10
    db "mark:", 10
    db "  cmp ecx, 200", 10
    db "  jae hang_forever", 10
    db "  mov dword [r12 + rcx*4], 0x00FFAA00", 10
    db "  inc ecx", 10
    db "  jmp mark", 10
    db "hang_forever:", 10
    db "  inc rax", 10
    db "  jmp hang_forever        ; infinite loop! APIC timer MUST preempt", 10
demo_hang_len equ $ - demo_hang_src

; ============================================================================
; Demo 4: Invalid Opcode (#UD) Test
; ============================================================================
demo_ud_src:
    db "; Demo 4: Intentional #UD (Invalid Opcode) Test", 10
    db "mov rax, 42", 10
    db "ud2                       ; <-- MUST TRIGGER #UD HERE (line 3)", 10
    db "exit 0", 10
demo_ud_len equ $ - demo_ud_src

; ============================================================================
; Demo 5: Divide by Zero (#DE) Test
; ============================================================================
demo_de_src:
    db "; Demo 5: Intentional #DE (Divide Error) Test", 10
    db "mov eax, 100", 10
    db "xor edx, edx", 10
    db "xor ecx, ecx", 10
    db "div ecx                   ; <-- MUST TRIGGER #DE HERE (line 5)", 10
    db "exit 0", 10
demo_de_len equ $ - demo_de_src

; ============================================================================
; Demo 6: Read-Only ABI Page Write Protection (#PF) Test
; ============================================================================
demo_ro_abi_src:
    db "; Demo 6: Write to Read-Only ABI Page (must trigger #PF)", 10
    db "mov qword [rdi + 0x00], 0 ; <-- MUST TRIGGER #PF (WP) HERE (line 2)", 10
    db "exit 0", 10
demo_ro_abi_len equ $ - demo_ro_abi_src

; ============================================================================
; Stage 2.1 Golden Apps (embedded directly from apps/*.uasm — single source!)
; ============================================================================
global demo_sand_src
global demo_sand_len
global demo_snake_src
global demo_snake_len
global demo_arkanoid_src
global demo_arkanoid_len
global demo_mandelbrot_src
global demo_mandelbrot_len

demo_sand_src:
    incbin "apps/falling_sand.uasm"
demo_sand_len equ $ - demo_sand_src

demo_snake_src:
    incbin "apps/snake.uasm"
demo_snake_len equ $ - demo_snake_src

demo_arkanoid_src:
    incbin "apps/arkanoid.uasm"
demo_arkanoid_len equ $ - demo_arkanoid_src

demo_mandelbrot_src:
    incbin "apps/mandelbrot.uasm"
demo_mandelbrot_len equ $ - demo_mandelbrot_src

; ============================================================================
; Stage 2.2 Compact 160x120 Golden Apps (incbin from apps160/*.uasm)
; ============================================================================
global demo160_sand_src
global demo160_sand_len
global demo160_snake_src
global demo160_snake_len
global demo160_arkanoid_src
global demo160_arkanoid_len
global demo160_mandelbrot_src
global demo160_mandelbrot_len
global demo160_canary_broken_src

demo160_sand_src:
    incbin "apps160/falling_sand.uasm"
demo160_sand_len equ $ - demo160_sand_src

demo160_snake_src:
    incbin "apps160/snake.uasm"
demo160_snake_len equ $ - demo160_snake_src

demo160_arkanoid_src:
    incbin "apps160/arkanoid.uasm"
demo160_arkanoid_len equ $ - demo160_arkanoid_src

demo160_mandelbrot_src:
    incbin "apps160/mandelbrot.uasm"
demo160_mandelbrot_len equ $ - demo160_mandelbrot_src

demo160_canary_broken_src:
    ; Replace the Arena initializer with a guard-page address.
    %strlen canary_arena_init_len "mov rbp,0x10100000"
    db "mov rbp,0x102FF000", 10
    incbin "apps160/flappy_bird.uasm", canary_arena_init_len + 1
    db 0