aboutsummaryrefslogtreecommitdiff
path: root/ultostr.s
diff options
context:
space:
mode:
authorB. Watson <yalhcru@gmail.com>2016-01-08 06:47:29 -0500
committerB. Watson <yalhcru@gmail.com>2016-01-08 06:47:29 -0500
commit63dc8498642b9bd088910a923ed953108f98d207 (patch)
treef50efa4b8bc90fe04924555e58e377d8b0e49487 /ultostr.s
parent7012ad570a2739a51f9f1f5605a9efa44416869c (diff)
downloadtaipan-63dc8498642b9bd088910a923ed953108f98d207.tar.gz
use custom long-to-ascii routine
Diffstat (limited to 'ultostr.s')
-rw-r--r--ultostr.s79
1 files changed, 79 insertions, 0 deletions
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