browser

putdw.xyw

putdw.xyw
calendarsave
; .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