itoa.s (4564B)
1;-------------------------------------------------------------------------- 2; itoa.s 3; 4; Copyright (C) 2020, Tony Pavlov 5; 6; This library is free software; you can redistribute it and/or modify it 7; under the terms of the GNU General Public License as published by the 8; Free Software Foundation; either version 2, or (at your option) any 9; later version. 10; 11; This library is distributed in the hope that it will be useful, 12; but WITHOUT ANY WARRANTY; without even the implied warranty of 13; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14; GNU General Public License for more details. 15; 16; You should have received a copy of the GNU General Public License 17; along with this library; see the file COPYING. If not, write to the 18; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 19; MA 02110-1301, USA. 20; 21; As a special exception, if you link this library with other files, 22; some of which are compiled with SDCC, to produce an executable, 23; this library does not by itself cause the resulting executable to 24; be covered by the GNU General Public License. This exception does 25; not however invalidate any other reasons why the executable file 26; might be covered by the GNU General Public License. 27;-------------------------------------------------------------------------- 28 29 .module itoa 30 31 .area _HOME 32 33_uitoa:: 34 push BC 35 lda HL, 4(SP) 36 ld A, (HL+) 37 ld E, A 38 ld A, (HL+) 39 ld D, A ; DE: uint 40 ld A, (HL+) 41 ld C, A 42 ld B, (HL) ; BC: dest 43 call .utoa 44 pop BC 45 ret 46 47_itoa:: 48 push BC 49 lda HL, 4(SP) 50 ld A, (HL+) 51 ld E, A 52 ld A, (HL+) 53 ld D, A ; DE: int 54 ld A, (HL+) 55 ld C, A 56 ld B, (HL) ; BC: dest 57 call .itoa 58 pop BC 59 ret 60 61.itoa:: ; convert int into ascii 62 ld A, D 63 add A, A 64 jr NC, .utoa 65 66 rra ; DE = abs(DE) 67 cpl 68 ld D, A 69 ld A, E 70 cpl 71 ld E, A 72 inc DE 73 74 ld A, #'-' 75 ld (BC), A 76 inc BC 77 78 call .utoa 79 dec DE 80 ret 81 82.utoa:: ; convert unsigned int into ascii 83 add SP, #-3 84 lda HL, 2(SP) 85 86 xor A ; clear value 87 ld (HL-), A 88 ld (HL-), A 89 ld (HL), A 90 91 push BC 92 ld B, #8 931$: 94 sla E 95 rl D 96 97 ld A, (HL) 98 adc A 99 daa 100 ld (HL+), A 101 ld A, (HL) 102 adc A 103 daa 104 ld (HL+), A 105 ld A, (HL) 106 adc A 107 daa 108 ld (HL-), A 109 dec HL 110 111 sla E 112 rl D 113 114 ld A, (HL) 115 adc A 116 daa 117 ld (HL+), A 118 ld A, (HL) 119 adc A 120 daa 121 ld (HL+), A 122 ld A, (HL) 123 adc A 124 daa 125 ld (HL-), A 126 dec HL 127 128 dec B 129 jr NZ, 1$ 130 131 pop BC 132 push BC 133 134 ld DE, #'0' 135 lda HL, 4(SP) 136 137 ld A, (HL-) 138 and #0x0f 139 or A 140 jr Z, 3$ 141 add A, E 142 ld D, #1 ; make D nonzero 143 ld (BC), A 144 inc BC 1453$: 146 ld A, (HL) 147 swap A 148 and #0x0f 149 add D 150 jr Z, 4$ 151 sub D 152 add A, E 153 ld D, #1 ; make D nonzero 154 ld (BC), A 155 inc BC 1564$: 157 ld A, (HL-) 158 and #0x0f 159 add D 160 jr Z, 5$ 161 sub D 162 add A, E 163 ld D, #1 ; make D nonzero 164 ld (BC), A 165 inc BC 1665$: 167 ld A, (HL) 168 swap A 169 and #0x0f 170 add D 171 jr Z, 6$ 172 sub D 173 add A, E 174 ld (BC), A 175 inc BC 1766$: 177 ld A, (HL) 178 and #0x0f 179 add A, E 180 ld (BC), A 181 inc BC 182 183 xor A 184 ld (BC), A ; write trailing #0 185 186 pop DE 187 188 add sp, #3 189 190 ret