git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/asm/sm83/ltoa.s

      1 ;--------------------------------------------------------------------------
      2 ;  ltoa.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 ltoa
     30 
     31         .area   _HOME
     32 
     33 _ultoa::
     34         push    BC
     35         
     36         lda     HL, 8(SP)
     37         ld      A, (HL+)
     38         ld      C, A
     39         ld      B, (HL)         ; BC: dest
     40 
     41         lda     HL, 4(SP)
     42         ld      E, L
     43         ld      D, H            ; DE : ulong *
     44         
     45         call    .ultoa
     46         
     47         pop     BC
     48         ret
     49 
     50 _ltoa::
     51         push    BC
     52         
     53         lda     HL, 8(SP)
     54         ld      A, (HL+)
     55         ld      C, A
     56         ld      B, (HL)         ; BC: dest
     57 
     58         lda     HL, 4(SP)
     59         ld      E, L
     60         ld      D, H            ; DE : ulong *
     61 
     62         call    .ltoa
     63 
     64         pop     BC
     65         ret
     66 
     67 .ltoa::
     68         push    DE
     69         
     70         ld      A, #3
     71         add     E
     72         ld      E, A
     73         adc     D
     74         sub     E
     75         ld      D, A
     76         
     77         ld      A, (DE)
     78         add     A, A
     79         pop     DE
     80         jr      NC, .ultoa
     81 
     82 1$:
     83         push    DE
     84         ld      A, (DE)
     85         cpl
     86         add     #1
     87         ld      (DE), A
     88         inc     DE
     89 
     90         ld      A, (DE)
     91         cpl
     92         adc     #0
     93         ld      (DE), A
     94         inc     DE
     95 
     96         ld      A, (DE)
     97         cpl
     98         adc     #0
     99         ld      (DE), A
    100         inc     DE
    101 
    102         ld      A, (DE)
    103         cpl
    104         adc     #0
    105         ld      (DE), A
    106         
    107         pop     DE
    108         
    109         ld      A, #'-'
    110         ld      (BC), A
    111         inc     BC
    112 
    113         call    .ultoa
    114         dec     DE
    115         ret
    116 
    117 .ultoa::                        ; convert unsigned int into ascii
    118         add     SP, #-5
    119         lda     HL, 4(SP)
    120         
    121         xor     A               ; clear value
    122         ld      (HL-), A
    123         ld      (HL-), A
    124         ld      (HL-), A
    125         ld      (HL-), A
    126         ld      (HL), A
    127 
    128         push    BC
    129         ld      B, #32
    130 1$:
    131         ld      H, D
    132         ld      L, E
    133 
    134         sla     (HL)
    135         inc     HL
    136         rl      (HL)
    137         inc     HL
    138         rl      (HL)
    139         inc     HL
    140         rl      (HL)
    141 
    142         rra
    143         lda     HL, 2(SP)
    144         rla
    145 
    146         ld      A, (HL)
    147         adc     A
    148         daa
    149         ld      (HL+), A        ; #0
    150         ld      A, (HL)
    151         adc     A
    152         daa
    153         ld      (HL+), A        ; #1
    154         ld      A, (HL)
    155         adc     A
    156         daa
    157         ld      (HL+), A        ; #2
    158         ld      A, (HL)
    159         adc     A
    160         daa
    161         ld      (HL+), A        ; #3
    162         ld      A, (HL)
    163         adc     A
    164         daa
    165         ld      (HL), A         ; #4
    166 
    167         dec     B
    168         jr      NZ, 1$
    169 
    170         pop     BC
    171         push    BC
    172 
    173         ld      DE, #((5 << 8) | '0')
    174         lda     HL, 6(SP)
    175 
    176         scf
    177         ccf
    178 
    179 3$:
    180         rl      D
    181         ld      A, (HL)
    182         swap    A
    183         and     #0x0f
    184         bit     0, D
    185         jr      NZ, 6$
    186         or      A
    187         jr      Z, 4$
    188 6$:
    189         add     A, E
    190         ld      (BC), A
    191         set     0, D
    192         inc     BC
    193 4$:
    194         ld      A, (HL-)
    195         and     #0x0f
    196         bit     0, D
    197         jr      NZ, 7$
    198         or      A
    199         jr      Z, 5$
    200 7$:     
    201         add     A, E
    202         ld      (BC), A
    203         set     0, D
    204         inc     BC
    205 5$:
    206         rr      D
    207         dec     D
    208         jr      NZ, 3$
    209         jr      C, 8$
    210 
    211         ld      A, #'0'         ; n == 0
    212         ld      (BC), A
    213         inc     BC
    214 8$:
    215         xor     A
    216         ld      (BC), A         ; write trailing #0
    217 
    218         pop     DE
    219 
    220         add     sp, #5
    221 
    222         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.