undar-lang/test/box.asm.lisp

209 lines
4.7 KiB
Common Lisp

((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)
; 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 30)
(push $1)
(load-immediate $1 40)
(push $1)
(call &draw-outlined-swatch)
(load $1 &DARK-GRAY)
(push $1)
(load-immediate $1 10)
(push $1)
(load-immediate $1 40)
(push $1)
(call &draw-outlined-swatch)
; row 3
(load $1 &RED)
(push $1)
(load-immediate $1 30)
(push $1)
(load-immediate $1 60)
(push $1)
(call &draw-outlined-swatch)
(load $1 &ORANGE)
(push $1)
(load-immediate $1 10)
(push $1)
(load-immediate $1 60)
(push $1)
(call &draw-outlined-swatch)
; row 3
(load $1 &YELLOW)
(push $1)
(load-immediate $1 30)
(push $1)
(load-immediate $1 80)
(push $1)
(call &draw-outlined-swatch)
(load $1 &GREEN)
(push $1)
(load-immediate $1 10)
(push $1)
(load-immediate $1 80)
(push $1)
(call &draw-outlined-swatch)
; row 4
(load $1 &BLUE)
(push $1)
(load-immediate $1 30)
(push $1)
(load-immediate $1 100)
(push $1)
(call &draw-outlined-swatch)
(load $1 &PURPLE)
(push $1)
(load-immediate $1 10)
(push $1)
(load-immediate $1 100)
(push $1)
(call &draw-outlined-swatch)
; Flush and halt
(syscall WRITE $0 $0 $0)
(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
; 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
(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)))