1 ;
--------------------------------------------------------------------------
4 ; Copyright
(C
) 2020, Sergey Belyashov
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
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.
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
,
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 ;
--------------------------------------------------------------------------
35 ;void __itoa
(int value
, char
*string
, unsigned char radix
);
49 ;positive
/negative numbers are supported only for radix
=10
53 ;
add minus sign to result
and inverse value
(Carry Flag is
0 here
)
58 ld (hl
), #0x2D ;minus symbol
63 ;void __uitoa
(unsigned int value
, char
*string
, unsigned char radix
);
79 ___uitoa_dehl
: ;DE
- value
, HL
- string
, 6 (ix
) - radix
89 cp
a, #10 ;most popular radix
92 ;
-------- decimal conversion
93 ; this algorithm up to
2 times faster than generic
101 push
bc ;
BC - pointer to string
102 push hl ;HL
- pointer to BCD
[4]
104 call ___uitobcd ;HL
- value
105 pop de ;DE
- pointer to string
106 pop hl ;HL
- pointer to BCD value
107 ld b, #3 ;number of bytes in BCD value
108 ld a, #0x30 ;ASCII code of '0'
121 ;skip trailing zeroes
122 ld b, #5 ;real BCD number is at most 5 digits
123 dec de ;so always skip last zero
128 jr NZ
, 107$ ;break loop if non-zero found
131 inc de ;always point to symbol next to last significant
135 ;
---------------------------
139 jr C
, 190$ ;radix is less than
2
146 ;
-------- radix is power of
2
148 ; DE
- value
, HL
- pointer to string
, C
- mask
160 cp
a, #0x3A ;convert to 0...9A...Z
171 ;
---------------------------
173 ;
-------- custom radix
(generic algorithm
)
193 ;
---------------------------
195 ;
-------- finish string
and reverse its order
199 call ___strreverse_reg
209 ;in
: HL
- divident
, C
- divisor
210 ;out
: HL
- quotient
, A - remainder