gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/libc/asm/sm83/itoa.s
1 ;--------------------------------------------------------------------------
2 ; itoa.s
3 ;
4 ; Copyright (c) 2026, Phidias618
5 ;
6 ;--------------------------------------------------------------------------
7
8 .module itoa
9
10 .area _HOME
11
12
13 _itoa::
14 pop hl ; get the return address
15 inc sp ; get rid of the unused radix argument
16 push hl ; put the return address back
17 .itoa:: ; convert int into ascii
18 bit 7, d
19 jr z, .utoa
20
21 ; DE = -DE
22 xor a
23 sub e
24 ld e, a
25 sbc a
26 sub d
27 ld d, a
28
29 ld a, #'-'
30 ld (bc), A
31 inc bc
32
33 call .utoa
34 dec bc
35 ret
36
37 _uitoa::
38 pop hl ; get the return address
39 inc sp ; get rid of the unused radix argument
40 push hl ; put the return address back
41 .utoa:: ; convert unsigned int into ascii
42 ; input :
43 ; - de (x) : the 16 bit unsigned number to convert to ascii
44 ; - bc (dst) : the pointer to the destination string
45
46 ld h, b
47 ld l, c
48
49 push bc
50 ld bc, #10
51 jr 4$
52 0$:
53 push hl
54
55 ; compute an approximation of x / 10
56 ; by using the approximation 1/10 ~ 0.0001100110011
57
58 ld b, e
59 ld c, d
60
61 ld l, e
62 ld h, d
63 xor a
64 .rept 3
65 add hl, hl
66 adc a
67 .endm
68 ld e, h
69 ld d, a
70
71 add hl, hl
72 adc a
73 ld l, h
74 ld h, a
75 add hl, de
76
77 ld e, b
78 xor a
79 ld b, a
80
81 add hl, bc
82 srl c
83 add hl, bc
84
85 srl c
86 srl c
87 srl c
88 add hl, bc
89 srl c
90 add hl, bc
91
92 ; computes an approximation of the remainder
93 ; by using the computed approximation of x / 10
94 ; it will always be in the range [0; 75]
95 sub l
96 add a
97 add a
98 sub l
99 add a
100 add e ; e = x - 10 * hl
101
102 ; fix both the quotient and the remainder of x / 10
103 ; using the fact that the remainder should be less than 10
104 cp #40
105 jr c, 1$
106 sub #40
107 ld c, #4
108 add hl, bc ; add hl, #4
109 1$:
110 cp #20
111 jr c, 2$
112 sub #20
113 ; this can *not* trigger an OAM corruption glitch
114 ; because hl is too small
115 inc hl
116 inc hl
117 2$:
118 ld c, #10
119 cp c ; cp #10
120 jr c, 3$
121 sub c ; sub #10
122 ; this can *not* trigger an OAM corruption glitch
123 ; because hl is too small
124 inc hl
125 3$:
126 ; now a = x % 10
127 ld d, h
128 ld e, l ; x = x / 10
129
130 pop hl
131 add #'0'
132 ld (hl+), a
133 4$:
134 ld a, e
135 sub c ; sub #10
136 ld a, d
137 sbc b ; sbc #0
138
139 jr nc, 0$ ; exit the loop if x is only one digit long
140
141 ld a, e
142 add #'0'
143 ld (hl+), a
144
145 xor a
146 ld (hl-), a ; store the terminator of the string
147
148 pop bc ; bc = dst
149
150 ; reverse the order of the string
151 ld a, l
152 sub c ; a = length-1
153 ret z ; no need to reverse if length == 1
154 cp #3
155
156 ; swap 2 chars
157 ld d, (hl)
158 ld a, (bc)
159 ld (hl-), a
160 ld a, d
161 ld (bc), a
162
163 ret c
164 inc bc
165
166 ; swap 2 more chars
167 ld d, (hl)
168 ld a, (bc)
169 ld (hl-), a
170 ld a, d
171 ld (bc), a
172
173 dec bc ; bc = dst
174 ret
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.