diff --git a/compiler/res/prog8lib/conv.p8 b/compiler/res/prog8lib/conv.p8 index 2e4f197b9..4e118e9e8 100644 --- a/compiler/res/prog8lib/conv.p8 +++ b/compiler/res/prog8lib/conv.p8 @@ -8,92 +8,90 @@ conv { str @shared string_out = "????????????????" ; result buffer for the string conversion routines -asmsub str_ub0 (ubyte value @ A) clobbers(X) -> str @AY { - ; ---- convert the ubyte in A in decimal string form, with left padding 0s (3 positions total) - %asm {{ - jsr conv.ubyte2decimal - sty string_out - sta string_out+1 - stx string_out+2 - lda #0 - sta string_out+3 - lda #string_out - rts - }} -} + asmsub str_ub0(ubyte value @A) clobbers(X) -> str @AY { + ; ---- convert the ubyte in A in decimal string form, with left padding 0s (3 positions total) + %asm {{ + jsr internal_str_ub0 + sty conv.string_out + stx conv.string_out+1 + sta conv.string_out+2 + lda #0 + sta conv.string_out+3 + lda #conv.string_out + rts + }} + } -asmsub str_ub (ubyte value @ A) clobbers(X) -> str @AY { - ; ---- convert the ubyte in A in decimal string form, without left padding 0s - %asm {{ - ldy #0 - sty P8ZP_SCRATCH_B1 - jsr ubyte2decimal ; result in Y/A/X (100s, 10s, 1s). - ; hundreds? - cpy #'0' - beq + - sty string_out - sta string_out+1 - stx string_out+2 - lda #0 - sta string_out+3 - jmp _done - ; tens? -+ cmp #'0' - beq + - sta string_out - stx string_out+1 - lda #0 - sta string_out+2 - jmp _done -+ ; ones. - stx string_out - lda #0 - sta string_out+1 -_done lda #string_out - rts - }} -} + asmsub str_ub(ubyte value @A) clobbers(X) -> str @AY { + ; ---- convert the ubyte in A in decimal string form, without left padding 0s + %asm {{ + jsr internal_str_ub0 + cpy #'0' + beq + + sty conv.string_out + stx conv.string_out+1 + sta conv.string_out+2 + lda #0 + sta conv.string_out+3 + jmp _done ++ cpx #'0' + beq + + stx conv.string_out + sta conv.string_out+1 + lda #0 + sta conv.string_out+2 + jmp _done ++ sta conv.string_out + lda #0 + sta conv.string_out+1 +_done lda #conv.string_out + rts + }} + } -asmsub str_b (byte value @ A) clobbers(X) -> str @AY { - ; ---- convert the byte in A in decimal string form, without left padding 0s - %asm {{ - ldy #0 - cmp #0 - bpl + - ldy #'-' - sty string_out - ldy #1 -+ sty P8ZP_SCRATCH_REG - jsr conv.byte2decimal ; result in Y/A/X (100s, 10s, 1s). and in uword2decimal.decHundreds, decTens, decOnes. - ; hundreds? - cpy #'0' - bne _out_hundreds - ldy P8ZP_SCRATCH_REG - cmp #'0' - bne _out_tens - beq _out_ones -_out_hundreds - ldy P8ZP_SCRATCH_REG - lda uword2decimal.decHundreds - sta string_out,y - iny -_out_tens - lda uword2decimal.decTens - sta string_out,y - iny -_out_ones - lda uword2decimal.decOnes - sta string_out,y - iny - lda #0 - sta string_out,y - lda #string_out - rts - }} -} + asmsub str_b(byte value @A) clobbers(X) -> str @AY { + ; ---- convert the byte in A in decimal string form, without left padding 0s + %asm {{ + cmp #0 + bpl str_ub + eor #255 + clc + adc #1 + jsr str_ub + ; insert a minus sign at the start + lda #0 + sta conv.string_out+4 + lda conv.string_out+2 + sta conv.string_out+3 + lda conv.string_out+1 + sta conv.string_out+2 + lda conv.string_out + sta conv.string_out+1 + lda #'-' + sta conv.string_out + lda #conv.string_out + rts + }} + } + + asmsub internal_str_ub0(ubyte value @A) -> ubyte @Y, ubyte @X, ubyte @A { + %asm {{ + ldy #'0'-1 + ldx #'9'+1 + sec +- iny + sbc #100 + bcs - +- dex + adc #10 + bmi - + adc #'0'-1 + rts + }} + } asmsub str_ubhex (ubyte value @ A) clobbers(X) -> str @AY { ; ---- convert the ubyte in A in hex string form diff --git a/docs/source/todo.rst b/docs/source/todo.rst index c9dad0dbd..de4be414b 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,6 @@ TODO ==== -- add more optimized routine in conv to convert (u)byte to string. (it now reuses the pretty large word to string routine) - ... diff --git a/examples/test.p8 b/examples/test.p8 index edd51e50e..4af68cea5 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,15 +1,29 @@ -; test_divmode.p8 %import textio +%import conv %zeropage basicsafe %option no_sysinit main { - ubyte c - ubyte l - sub start() { - divmod(99, 10, c, l) + for cx16.r0L in 0 to 255 { + txt.print(conv.str_ub0(cx16.r0L)) + txt.nl() + } + txt.nl() + txt.nl() + for cx16.r0L in 0 to 255 { + txt.print(conv.str_ub(cx16.r0L)) + txt.nl() + } + txt.nl() + txt.nl() + for cx16.r0sL in -128 to 127 { + txt.print(conv.str_b(cx16.r0sL)) + txt.nl() + } + txt.nl() + txt.nl() } }