((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 $1 0) (syscall OPEN $0 $1) ; draw to the device because otherwise the screen wont open (syscall WRITE $0 $0 $0) (load-immediate $16 &mouse-namespace) (load-immediate $3 12) ; malloc sizeof mouse data (malloc $4 $3) (label draw-loop ; load mouse click data (syscall READ $16 $2 $3 $4) (load-immediate $5 4) ; offset for x (add-nat $6 $5 $2) (load-r $7 $6) ; load x (load-immediate $5 8) ; offset for y (add-nat $6 $5 $2) (load-r $8 $6) ; load y (load-immediate $5 12) ; offset for btn1 (add-nat $6 $5 $2) (load-r8 $9 $6) ; load btn1 pressed (load-immediate $5 13) ; offset for btn2 (add-nat $6 $5 $2) (load-r8 $10 $6) ; load btn2 pressed ; first row (load $1 &BLACK) (push $1) (load-immediate $1 1) (push $1) (load-immediate $1 1) (push $1) (call &draw-outlined-swatch) (load $1 &WHITE) (push $1) (load-immediate $1 21) (push $1) (load-immediate $1 1) (push $1) (call &draw-outlined-swatch) ; row 2 (load $1 &CHARCOAL) (push $1) (load-immediate $1 1) (push $1) (load-immediate $1 21) (push $1) (call &draw-outlined-swatch) (load $1 &DARK-GRAY) (push $1) (load-immediate $1 21) (push $1) (load-immediate $1 21) (push $1) (call &draw-outlined-swatch) ; row 3 (load $1 &RED) (push $1) (load-immediate $1 1) (push $1) (load-immediate $1 41) (push $1) (call &draw-outlined-swatch) (load $1 &ORANGE) (push $1) (load-immediate $1 21) (push $1) (load-immediate $1 41) (push $1) (call &draw-outlined-swatch) ; row 3 (load $1 &YELLOW) (push $1) (load-immediate $1 1) (push $1) (load-immediate $1 61) (push $1) (call &draw-outlined-swatch) (load $1 &GREEN) (push $1) (load-immediate $1 21) (push $1) (load-immediate $1 61) (push $1) (call &draw-outlined-swatch) ; row 4 (load $1 &BLUE) (push $1) (load-immediate $1 1) (push $1) (load-immediate $1 81) (push $1) (call &draw-outlined-swatch) (load $1 &PURPLE) (push $1) (load-immediate $1 21) (push $1) (load-immediate $1 81) (push $1) (call &draw-outlined-swatch) (syscall WRITE $0 $0 $0) (jump-eq-nat &draw-loop $9 $11) ; if click on box then switch color ; else draw the pixel (jump-eq-nat &draw-loop $10 $11)) ; Flush and halt (halt)) (label draw-outlined-swatch (pop $3) ; y (pop $2) ; x (pop $1) ; color ; Constants (load $4 &GRAY) (load-immediate $5 20) ; outline size (load-immediate $6 17) ; fill size (load-immediate $7 2) ; offset (push $4) ; color (gray) (push $2) ; x (push $3) ; y (push $5) ; width (20) (push $5) ; height (20) (call &draw-box) (add-int $8 $2 $7) ; x + 2 (add-int $9 $3 $7) ; y + 2 (push $1) ; color (original) (push $8) ; x + 2 (push $9) ; y + 2 (push $6) ; width (17) (push $6) ; height (17) (call &draw-box) (return)) ; draw-box(color, x, y) ; Pops: y, x, color (label draw-box ; Pop arguments (reverse order) (pop $14) ; height (pop $12) ; width (pop $13) ; y_start (pop $11) ; x_start (pop $3) ; color ;; get the screen width dynamically from the device (load-immediate $0 &screen-namespace) (load-immediate $16 1) ; device info call (load-immediate $17 16) ; sizeof screen device info (malloc $18 $17) (syscall IOCTL $0 $16 $18) (load-immediate $1 12) ; offset for width (add-nat $19 $18 $1) (load-r $2 $19) ; load width (load-immediate $1 4) ; offset for width (add-nat $19 $18 $1) (load-r $21 $19) ; load base pointer (add-nat $21 $21 $1) ; need to add offset for fat pointer size ; Constants (load-immediate $1 1) ; increment ; Compute start address: base + y*640 + x (mul-int $15 $13 $2) ; $15 = y * 640 (add-int $15 $15 $11) ; $15 += x (add-nat $15 $21 $15) ; $15 = base + pixel_offset (register-move $4 $15) ; Outer loop: height times (register-move $5 $14) ; $5 = row counter (label draw-box-outer (add-int $6 $4 $12) ; $6 = row end = current + width (register-move $7 $4) ; $7 = pixel pointer (label draw-box-inner (store-8 $7 $3) ; write color (add-int $7 $7 $1) ; next pixel (jump-lt-int &draw-box-inner $7 $6)) (add-int $4 $4 $2) ; next row (+= 640) (sub-int $5 $5 $1) ; decrement row count (jump-gt-int &draw-box-outer $5 0)) (return))) (data (label screen-namespace "/dev/screen/0") (label mouse-namespace "/dev/mouse/0") (label BLACK 0) (label WHITE 255) (label CHARCOAL 36) (label DARK-GRAY 73) (label GRAY 146) (label LIGHT-GRAY 182) (label DARK-RED 128) (label RED 224) (label DARK-YELLOW 144) (label YELLOW 252) (label DARK-TEAL 9) (label TEAL 18) (label DARK-GREEN 12) (label GREEN 16) (label LIME 28) (label LIGHT-CYAN 159) (label NAVY 2) (label BLUE 3) (label DEEP-SKY-BLUE 10) (label LIGHT-BLUE 19) (label PURPLE 131) (label LIGHT-PURPLE 147) (label DARK-MAGENTA 130) (label MAGENTA 227) (label PLUM 129) (label PINK 226) (label SADDLE-BROWN 72) (label PERU 141) (label SIENNA 136) (label ORANGE 241) (label DARK-ORANGE 208) (label GOLD 244)))