From 4e863ecdac5d67bb4040757107e530a118e0d4af Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 8 Jul 2023 22:57:16 +0200 Subject: [PATCH] vm: fixed abs() and word-to-string conversion --- compiler/res/prog8lib/virtual/conv.p8 | 40 +++++++++++++++------------ docs/source/todo.rst | 2 -- examples/test.p8 | 10 ++----- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/compiler/res/prog8lib/virtual/conv.p8 b/compiler/res/prog8lib/virtual/conv.p8 index 36f6f23ae..6611b6c82 100644 --- a/compiler/res/prog8lib/virtual/conv.p8 +++ b/compiler/res/prog8lib/virtual/conv.p8 @@ -106,19 +106,21 @@ sub str_uwhex (uword value) { sub str_uw0 (uword value) { ; ---- convert the uword in A/Y in decimal string form, with left padding 0s (5 positions total) - ubyte tenthousands = (value / 10000) as ubyte - value -= 10000*tenthousands - ubyte thousands = (value / 1000) as ubyte - value -= 1000*thousands - ubyte hundreds = (value / 100) as ubyte - value -= 100 as uword * hundreds - ubyte tens = (value / 10) as ubyte - value -= 10*tens + uword value2 = value/10 + ubyte digits = value-value2*10 as ubyte + uword value3 = value2/10 + ubyte tens = value2-value3*10 as ubyte + uword value4 = value3/10 + ubyte hundreds = value3-value4*10 as ubyte + uword value5 = value4/10 + ubyte thousands = value4-value5*10 as ubyte + uword value6 = value5/10 + ubyte tenthousands = value5-value6*10 as ubyte string_out[0] = tenthousands+'0' string_out[1] = thousands+'0' string_out[2] = hundreds+'0' string_out[3] = tens+'0' - string_out[4] = value as ubyte + '0' + string_out[4] = digits+'0' string_out[5] = 0 } @@ -139,14 +141,16 @@ sub str_w (word value) { } sub internal_str_uw(uword value, uword out_ptr) { - ubyte tenthousands = (value / 10000) as ubyte - value -= 10000*tenthousands - ubyte thousands = (value / 1000) as ubyte - value -= 1000*thousands - ubyte hundreds = (value / 100) as ubyte - value -= 100 as uword * hundreds - ubyte tens = (value / 10) as ubyte - value -= 10*tens + uword value2 = value/10 + ubyte digits = value-value2*10 as ubyte + uword value3 = value2/10 + ubyte tens = value2-value3*10 as ubyte + uword value4 = value3/10 + ubyte hundreds = value3-value4*10 as ubyte + uword value5 = value4/10 + ubyte thousands = value4-value5*10 as ubyte + uword value6 = value5/10 + ubyte tenthousands = value5-value6*10 as ubyte if tenthousands goto output_tenthousands if thousands @@ -169,7 +173,7 @@ output_tens: @(out_ptr) = tens+'0' out_ptr++ output_ones: - @(out_ptr) = value as ubyte + '0' + @(out_ptr) = digits+'0' out_ptr++ @(out_ptr) = 0 } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index a295a1981..522f4fb6a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,6 @@ TODO ==== -- fix VM print_w -- fix VM abs(byte) it always returns 0 also check abs(word) - IR: instructions that do type conversion (SZ etc, CONCAT, SGN) should put the result in a DIFFERENT register. ... diff --git a/examples/test.p8 b/examples/test.p8 index 9ba8aef3d..ce8138a38 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,14 +3,7 @@ %option no_sysinit main { - sub set_color(ubyte dummy, uword arg) { - arg++ - } - sub start() { - ubyte intens - set_color(0, (intens >> 1) * $111) - byte intensity = -25 txt.print_b(intensity) txt.nl() @@ -19,9 +12,10 @@ main { txt.nl() txt.print_b(intensity) txt.nl() - word intensityw = 2555 + txt.print_uw0(12345) txt.nl() + word intensityw = -12345 txt.print_w(intensityw) txt.nl() txt.print_w(abs(intensityw))