1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-18 15:05:14 +00:00
cc65/libsrc/common/ltoa.s

152 lines
3.1 KiB
ArmAsm
Raw Normal View History

;
; Ullrich von Bassewitz, 11.06.1998
;
; char* ltoa (long value, char* s, int radix);
; char* ultoa (unsigned long value, char* s, int radix);
;
.export _ltoa, _ultoa
2018-05-22 13:59:05 +00:00
.import popax, popptr1, negeax
.import __hextab, __longminstr
.importzp sreg, ptr1, ptr2, ptr3, tmp1
2018-05-22 13:59:05 +00:00
.macpack cpu
.code
;
; Common subroutine to pop the parameters and put them into core
;
dopop: sta tmp1 ; will loose high byte
2018-05-22 13:59:05 +00:00
jsr popax ; get s to ptr2
sta ptr2
stx ptr2+1
2018-05-22 13:59:05 +00:00
sta ptr3 ; save for return
stx ptr3+1
2018-05-22 13:59:05 +00:00
jsr popptr1 ; get low word of value to ptr1
jsr popax ; get high word of value to sreg
sta sreg
stx sreg+1
rts
;
; ltoa
;
_ltoa: jsr dopop ; pop the arguments
; We must handle $80000000 in a special way, since it is the only negative
; number that has no positive 32-bit counterpart
2018-05-22 13:59:05 +00:00
ldx sreg+1 ; get high byte
ldy tmp1 ; get radix
cpy #10
bne ultoa
2018-05-22 13:59:05 +00:00
lda sreg
ora ptr1+1
ora ptr1
bne L2
cpx #$80
bne L2
ldy #11
L1: lda __longminstr,y ; copy -2147483648
2018-05-22 13:59:05 +00:00
sta (ptr2),y
dey
bpl L1
jmp L10
; Check if the value is negative. If so, write a - sign and negate the
; number.
L2: txa ; get high byte
bpl ultoa
lda #'-'
2018-05-22 13:59:05 +00:00
.if (.cpu .bitand CPU_ISET_65SC02)
sta (ptr2)
.else
ldy #0
2018-05-22 13:59:05 +00:00
sta (ptr2),y ; store sign
.endif
inc ptr2
bne L3
2018-05-22 13:59:05 +00:00
inc ptr2+1
2018-05-22 13:59:05 +00:00
L3: lda ptr1 ; negate val
ldx ptr1+1
jsr negeax
sta ptr1
stx ptr1+1
jmp ultoa
;
; utoa
;
_ultoa: jsr dopop ; pop the arguments
; Convert to string by dividing and push the result onto the stack
ultoa: lda #$00
pha ; sentinel
; Divide val/tmp1 -> val, remainder in a
L5: ldy #32 ; 32 bit
lda #0 ; remainder
2018-05-22 13:59:05 +00:00
L6: asl ptr1
rol ptr1+1
rol sreg
rol sreg+1
rol a
cmp tmp1
bcc L7
sbc tmp1
2018-05-22 13:59:05 +00:00
inc ptr1
L7: dey
bne L6
tay ; get remainder into y
lda __hextab,y ; get hex character
pha ; save char value on stack
2018-05-22 13:59:05 +00:00
lda ptr1
ora ptr1+1
ora sreg
ora sreg+1
bne L5
; Get the characters from the stack into the string
ldy #0
L9: pla
2018-05-22 13:59:05 +00:00
sta (ptr2),y
beq L10 ; jump if sentinel
iny
bne L9 ; jump always
; Done! Return the target string
2018-05-22 13:59:05 +00:00
L10: lda ptr3
ldx ptr3+1
rts