]> git.sur5r.net Git - cc65/blob - libsrc/common/itoa.s
Implemented line wrap.
[cc65] / libsrc / common / itoa.s
1 ;
2 ; Ullrich von Bassewitz, 31.05.1998
3 ;
4 ; char* itoa (int value, char* s, int radix);
5 ; char* utoa (unsigned value, char* s, int radix);
6 ;
7
8         .export         _itoa, _utoa
9         .import         addysp1
10         .import         __hextab
11         .importzp       sp, sreg, ptr2, ptr3, tmp1
12
13 .rodata
14 specval:
15         .byte   '-', '3', '2', '7', '6', '8', 0
16 .code
17
18 ;
19 ; Common subroutine to pop the parameters and put them into core
20 ;
21
22 dopop:  sta     tmp1            ; will loose high byte
23         ldy     #0
24         lda     (sp),y
25         sta     ptr2
26         sta     ptr3
27         iny
28         lda     (sp),y
29         sta     ptr2+1
30         sta     ptr3+1
31         iny
32         lda     (sp),y
33         sta     sreg
34         iny
35         lda     (sp),y
36         sta     sreg+1
37         jmp     addysp1         ; Bump stack pointer
38
39 ;
40 ; itoa
41 ;
42
43 _itoa:  jsr     dopop           ; pop the arguments
44
45 ; We must handle $8000 in a special way, since it is the only negative
46 ; number that has no positive 16-bit counterpart
47
48         ldy     tmp1            ; get radix
49         cpy     #10
50         bne     utoa
51         cmp     #$00
52         bne     L2
53         cpx     #$80
54         bne     L2
55
56         ldy     #6
57 L1:     lda     specval,y       ; copy -32768
58         sta     (ptr2),y
59         dey
60         bpl     L1
61         jmp     L10
62
63 ; Check if the value is negative. If so, write a - sign and negate the
64 ; number.
65
66 L2:     lda     sreg+1          ; get high byte
67         bpl     utoa
68         lda     #'-'
69         ldy     #0
70         sta     (ptr2),y        ; store sign
71         inc     ptr2
72         bne     L3
73         inc     ptr2+1
74
75 L3:     lda     sreg
76         eor     #$FF
77         clc
78         adc     #$01
79         sta     sreg
80         lda     sreg+1
81         eor     #$FF
82         adc     #$00
83         sta     sreg+1
84         jmp     utoa
85
86 ;
87 ; utoa
88 ;
89
90 _utoa:  jsr     dopop           ; pop the arguments
91
92 ; Convert to string by dividing and push the result onto the stack
93
94 utoa:   lda     #$00
95         pha                     ; sentinel
96
97 ; Divide sreg/tmp1 -> sreg, remainder in a
98
99 L5:     ldy     #16             ; 16 bit
100         lda     #0              ; remainder
101 L6:     asl     sreg
102         rol     sreg+1
103         rol     a
104         cmp     tmp1
105         bcc     L7
106         sbc     tmp1
107         inc     sreg
108 L7:     dey
109         bne     L6
110
111         tay                     ; get remainder into y
112         lda     __hextab,y      ; get hex character
113         pha                     ; save char value on stack
114
115         lda     sreg
116         ora     sreg+1
117         bne     L5
118
119 ; Get the characters from the stack into the string
120
121         ldy     #0
122 L9:     pla
123         sta     (ptr2),y
124         beq     L10             ; jump if sentinel
125         iny
126         bne     L9              ; jump always
127
128 ; Done! Return the target string
129
130 L10:    lda     ptr3
131         ldx     ptr3+1
132         rts
133
134
135
136
137
138
139
140
141
142
143
144
145
146