diff --git a/stdlib/stdlib.sl b/stdlib/stdlib.sl index 3772235..1bd11b1 100644 --- a/stdlib/stdlib.sl +++ b/stdlib/stdlib.sl @@ -474,6 +474,17 @@ } ; +# : abs ( x -- |x| ) +:asm abs { + mov rax, [r12] ; get value + test rax, rax ; check sign + jge .done ; keep if non-negative + neg rax ; flip sign when negative +.done: + mov [r12], rax ; store result +} +; + # : bitnot ( 0|1 -- 1|0 ) :asm bitnot { mov rax, [r12] ; get value diff --git a/toint.sl b/toint.sl new file mode 100644 index 0000000..efb9c81 --- /dev/null +++ b/toint.sl @@ -0,0 +1,39 @@ +import stdlib.sl +import io.sl + +word digitsN>num # ( d_{n-1} ... d0 n -- value ), digits bottom=MSD, top=LSD, length on top (MSD-most significant digit, LSD-least significant digit) + 0 swap # place accumulator below length + for # loop n times using the length on top + r@ pick # fetch next digit starting from MSD (uses loop counter as index) + swap # acc on top + 10 * # acc *= 10 + + # acc += digit + end +end + + +word toint + swap + over 0 swap + dup >r + for + over over + + c@ 48 - + swap rot + swap + 1 + + end + 2drop + r> + dup >r + digitsN>num + r> 1 + + for + swap drop + end + rdrop +end + +word main + "1234" toint 1 + puti cr +end