all repos — xyw @ 88877124cdbbd5971f945225c8ec4a7589014f6b

A minimal virtual machine and assembler for terminals.

examples/lib/putdw.xyw

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
; .include "lib/macros.xyw"

; Print a 16bit integer as decimal
; a16 -- | [x]
.label putdw
  POPxw 
  ; If value is less than 10, print single digit
  ; Push 16-bit literal as two bytes (high, low), then compare
  PSHxw $000A LTHw LBYTE putdw.single JCNw
  ; Divide by 10, stack becomes [remainder, quotient] with quotient on top
  PSHxw $000A DIVw
  ; Call PUTDw recursively with quotient (on top, consumed by POPxw)
  putdw JSRw
  ; Remainder (16-bit) is still on stack, get low byte and print it
  SWP POP $30 ADD PUTC
  CLRxw
  RTS
  .label putdw.single
    ; Print single digit (need low byte of XW since value is 0-9)
    PSHxw SWP POP $30 ADD PUTC
    CLRxw
    RTS