((code (label main ; Open screen ; use load immediate because it is a pointer to a string, not a value (load-immediate $0 &screen-namespace) (load-immediate $11 0) (syscall OPEN $0 $18 $11) ; open(out Plex screen, in namespace, in flags) (load-offset-32 $20 $0 8) ; load width (load-offset-32 $22 $0 12) ; load size (load-immediate $1 16) ; offset for screen buffer (add-nat $21 $0 $1) ; open mouse (load-immediate $16 &mouse-namespace) (syscall OPEN $15 $16 $11) ; open(out Plex mouse, in namespace, in flags) ; outline_swatch(screen, BLACK, 1, 1); (load-absolute-32 $1 &BLACK) (load-immediate $12 1) (load-immediate $13 1) (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) ; outline_swatch(screen, WHITE, 1, 1); (load-absolute-32 $1 &WHITE) (load-immediate $12 21) (load-immediate $13 1) (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) ; screen.draw(); (syscall WRITE $0 $21 $22) (label draw-loop ; load mouse click data (syscall REFRESH $15) (load-offset-8 $9 $15 16) ; load btn1 pressed (jump-eq-nat &draw-loop $9 $11) (load-offset-32 $7 $15 8) ; load x (load-offset-32 $8 $15 12) ; load y (load-immediate $14 20) ; box size ; first row (load-absolute-32 $1 &BLACK) (load-immediate $12 1) (load-immediate $13 1) (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) (load-absolute-32 $1 &WHITE) (load-immediate $12 21) (load-immediate $13 1) (call &draw-outlined-swatch ($21 $1 $12 $13 $20) nil) (call &set-color-if-clicked ($7 $8 $12 $13 $1 $14) nil) (syscall WRITE $0 $21 $22) (load-absolute-32 $22 &SELECTED-COLOR) ; color (load-immediate $1 5) ; size of brush (call &draw-box ($21 $20 $22 $7 $8 $1 $1) nil) (jump &draw-loop)) ; Flush and halt (halt 0)) (label set-color-if-clicked ; (click_x, click_y, box_x, box_y, color, box_size) ; Compute right = box_x + box_size (add-int $6 $2 $5) ; $6 = right edge ; Compute bottom = box_y + box_size (add-int $7 $3 $5) ; $7 = bottom edge ; Bounds check: x in [box_x, right] and y in [box_y, bottom] (jump-lt-int &fail $0 $2) (jump-gt-int &fail $0 $6) (jump-lt-int &fail $1 $3) (jump-gt-int &fail $1 $7) (load-immediate $10 &SELECTED-COLOR) (store-absolute-8 $10 $4) (label fail) (return nil)) (label draw-outlined-swatch ; (base, color, x, y, width) ; Constants (load-absolute-32 $5 &GRAY) (load-absolute-32 $10 &SELECTED-COLOR) (jump-eq-int &set-selected $10 $1) (jump-eq-int &end-set-selected $5 $5) (label set-selected) (load-absolute-32 $5 &DARK-GRAY) (label end-set-selected) (load-immediate $6 20) ; outline size (load-immediate $7 17) ; fill size (load-immediate $8 2) ; offset (call &draw-box ($0 $4 $5 $2 $3 $6 $6) nil) (add-int $9 $2 $8) ; x + 2 (add-int $10 $3 $8) ; y + 2 (call &draw-box ($0 $4 $1 $9 $10 $7 $7) nil) (return nil)) (label draw-box ; (base, screen_width, color, x_start, y_start, width, height) ; Compute start address: base + y*640 + x (mul-int $15 $4 $1) ; $15 = y * 640 (add-int $15 $15 $3) ; $15 += x (add-nat $15 $0 $15) ; $15 = base + pixel_offset (load-immediate $25 4) (add-nat $15 $15 $25) ; need to add offset for fat pointer size ; Outer loop: height times (load-immediate $30 1) ; increment (label draw-box-outer (add-int $27 $15 $5) ; $27 = row end = current + width (register-move $29 $15) ; $7 = pixel pointer (memset-8 $29 $2 $5) ; draw row (add-int $15 $15 $1) ; next row (+= 640) (sub-int $6 $6 $30) ; decrement row count (jump-gt-int &draw-box-outer $6 0)) (return nil))) (data (label screen-namespace "/dev/screen/0") (label mouse-namespace "/dev/mouse/0") (label SELECTED-COLOR 255) (label BLACK 0) (label WHITE 255) (label DARK-GRAY 73) (label GRAY 146) (label LIGHT-GRAY 182)))