From 63dc8498642b9bd088910a923ed953108f98d207 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Fri, 8 Jan 2016 06:47:29 -0500 Subject: use custom long-to-ascii routine --- ultostr.s | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 ultostr.s (limited to 'ultostr.s') diff --git a/ultostr.s b/ultostr.s new file mode 100644 index 0000000..29420e4 --- /dev/null +++ b/ultostr.s @@ -0,0 +1,79 @@ +; +; Modified from cc65's libsrc/common/ltoa.s +; Originally by Ullrich von Bassewitz, 11.06.1998 + +; modified version by B. Watson + +; - rename ultoa => ultostr +; - got rid of ltoa (don't need it) +; - hardcode radix to 10 +; - don't need __hextab since we don't do hex (saves 16 bytes) +; - inline dopop subroutine + +; char* ultostr (unsigned long value, char* s); + + .export _ultostr + .import popax + .importzp sreg, ptr1, ptr2, ptr3 + +radix = 10 + +.code + +_ultostr: + ; pop the arguments + sta ptr1 + stx ptr1+1 + sta sreg ; save for return + stx sreg+1 + jsr popax ; get low word of value + sta ptr2 + stx ptr2+1 + jsr popax ; get high word of value + sta ptr3 + stx ptr3+1 + +; Convert to string by dividing and push the result onto the stack + +ultostr: lda #$00 + pha ; sentinel + +; Divide val/radix -> val, remainder in a + +L5: ldy #32 ; 32 bit + lda #0 ; remainder +L6: asl ptr2 + rol ptr2+1 + rol ptr3 + rol ptr3+1 + rol a + cmp #radix + bcc L7 + sbc #radix + inc ptr2 +L7: dey + bne L6 + + ora #'0' ; get ascii character + pha ; save char value on stack + + lda ptr2 + ora ptr2+1 + ora ptr3 + ora ptr3+1 + bne L5 + +; Get the characters from the stack into the string + + ldy #0 +L9: pla + sta (ptr1),y + beq L10 ; jump if sentinel + iny + bne L9 ; jump always + +; Done! Return the target string + +L10: lda sreg + ldx sreg+1 + rts -- cgit v1.2.3