From ec9e72292785c887dab50da2695f9584f8335491 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 11 Oct 2020 18:36:20 +0200 Subject: [PATCH] added conv.str2byte and conv.str2ubyte --- compiler/res/prog8lib/conv.p8 | 22 ++++++++++++++++++++++ examples/numbergame.p8 | 2 +- examples/test.p8 | 19 ++++++++----------- examples/textelite.p8 | 11 +++++++++-- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/compiler/res/prog8lib/conv.p8 b/compiler/res/prog8lib/conv.p8 index 8318e7f86..081851ec3 100644 --- a/compiler/res/prog8lib/conv.p8 +++ b/compiler/res/prog8lib/conv.p8 @@ -249,6 +249,28 @@ output .text "0000", $00 ; 0-terminated output buffer (to make printing ea }} } + +asmsub str2ubyte(str string @ AY) clobbers(Y) -> ubyte @A { + ; -- returns the unsigned byte value of the string number argument in AY + ; the number may NOT be preceded by a + sign and may NOT contain spaces + ; (any non-digit character will terminate the number string that is parsed) + ; TODO implement optimized custom version of this instead of simply reusing str2uword + %asm {{ + jmp str2uword + }} +} + + +asmsub str2byte(str string @ AY) clobbers(Y) -> ubyte @A { + ; -- returns the signed byte value of the string number argument in AY + ; the number may be preceded by a + or - sign but may NOT contain spaces + ; (any non-digit character will terminate the number string that is parsed) + ; TODO implement optimized custom version of this instead of simply reusing str2word + %asm {{ + jmp str2word + }} +} + asmsub str2uword(str string @ AY) -> uword @ AY { ; -- returns the unsigned word value of the string number argument in AY ; the number may NOT be preceded by a + sign and may NOT contain spaces diff --git a/examples/numbergame.p8 b/examples/numbergame.p8 index 2d223071c..eee717b11 100644 --- a/examples/numbergame.p8 +++ b/examples/numbergame.p8 @@ -30,7 +30,7 @@ main { txt.print("es") txt.print(" left.\nWhat is your next guess? ") void txt.input_chars(input) - ubyte guess = lsb(conv.str2uword(input)) + ubyte guess = conv.str2ubyte(input) if guess==secretnumber { ending(true) diff --git a/examples/test.p8 b/examples/test.p8 index 5f4956a75..02277c253 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -11,18 +11,15 @@ main { str name = "irmen de jong" uword strptr = &name + txt.print("ubyte? ") + void txt.input_chars(name) + ubyte ub = conv.str2ubyte(name) + txt.print_ub(ub) - txt.print_ub(strlen("1234")) - txt.chrout('\n') - txt.print_ub(strlen(name)) - txt.chrout('\n') - txt.print_uwhex(strptr, 1) - txt.chrout('\n') - txt.print(strptr) - txt.chrout('\n') - txt.print_ub(strlen(strptr)) - txt.chrout('\n') - + txt.print("\nbyte? ") + void txt.input_chars(name) + byte bb = conv.str2byte(name) + txt.print_b(bb) } diff --git a/examples/textelite.p8 b/examples/textelite.p8 index 4316812c2..183ef9862 100644 --- a/examples/textelite.p8 +++ b/examples/textelite.p8 @@ -78,6 +78,13 @@ trader { } sub do_buy() { + txt.print("\nBuy what commodity? ") + str commodity = "???????????????" + void txt.input_chars(commodity) + txt.print("\nHow much?") + void txt.input_chars(input) + ubyte buy_fuel = 10*conv.str2ubyte(input) + ubyte amount = lsb() txt.print("\nTODO BUY\n") ; TODO } @@ -88,7 +95,7 @@ trader { sub do_fuel() { txt.print("\nBuy fuel. Amount? ") void txt.input_chars(input) - ubyte buy_fuel = 10*lsb(conv.str2uword(input)) + ubyte buy_fuel = 10*conv.str2ubyte(input) ubyte max_fuel = ship.Max_fuel - ship.fuel if buy_fuel > max_fuel buy_fuel = max_fuel @@ -110,7 +117,7 @@ trader { sub do_hold() { txt.print("\nCheat! Set cargohold size: ") void txt.input_chars(input) - ship.Max_cargo = lsb(conv.str2uword(input)) + ship.Max_cargo = conv.str2ubyte(input) } sub do_next_galaxy() {