putd_w.xyw

putd-w.xyw
calendarsave
; .include "lib/macros.xyw"

; Print a 16bit integer as decimal
; w ->
.label putd_w
  POPxw 
  ; If value is less than 10, print single digit
  ; Push 16-bit literal as two bytes (high, low), then compare
  PSHxw $000A LTHw putd_w.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)
  putd_w JSRw
  ; Remainder (16-bit) is still on stack, get low byte and print it
  SWP POP $30 ADD PUTC
  RTS
  .label putd_w.single
    ; Print single digit (need low byte of XW since value is 0-9)
    PSHxw SWP POP $30 ADD PUTC
    RTS