diff options
Diffstat (limited to 'quine.asm')
| -rw-r--r-- | quine.asm | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/quine.asm b/quine.asm index 0525cf4..5c5f95b 100644 --- a/quine.asm +++ b/quine.asm @@ -3771,28 +3771,60 @@ defword emitstring, 0 ; In: ; integer to print defword dot, 0 + dq docol, lit, 10, dotbase, exit +defword dothex, 0 + dq docol, lit, 16, dotbase_unsigned, exit + +; In: +; integer to print +; base to print in +defword dotbase, 0 dq docol + dq swap + ; (base, input) + ; Deal with negative numbers. dq dup, lit, 0, gt, zbranch, 7*8 dq lit, -1, mul dq litstring, "-", emitstring + dq swap, dotbase_unsigned, exit + +; In: +; integer to print +; base to print in +defword dotbase_unsigned, 0 + dq docol + + ; (input, base) ; Compute how many digits we need to display. Because we use logfloor, the ; logic of always printing at least one digit is already handled for us. - dq dup, lit, 10, swap, logfloor, lit, 1, add + dq swap, dup2, logfloor, lit, 1, add - ; (input, number of digits remaining) + ; (base, input, number of digits remaining) ; This is the start of the loop. - dq dup2, lit, 1, sub, lit, 10, swap, pow, divmod, swap, drop - ; (input, number of digits remaining, input divided by 10^x appropriately) - dq lit, 10, divmod, drop + dq dup2, lit, 1, sub + ; (base, input, number of digits remaining, input, intermediate value) + dq lit, 5, roll, dup, lit, 6, unroll, swap + ; (base, input, number of digits remaining, input, base, intermediate value) + dq pow, divmod, swap, drop + ; (base, input, number of digits remaining, + ; input divided by base^x appropriately) + dq lit, 4, roll, dup, lit, 5, unroll + ; (base, input, number of digits remaining, + ; input divided by base^x appropriately, base) + dq divmod, drop - ; (input, number of digits remaining, current digit) + ; (base, input, number of digits remaining, current digit) ; We construct a one-character string on the stack, then use a pointer to ; it. It will always contain its own null-termination without us having to ; do anything special to that end. - dq lit, "0", add, fetch_value_stack, emitstring + dq dup, lit, 10, gt, zbranch, 5*8 + dq lit, "0", branch, 6*8 + dq lit, 10, sub, lit, "a" + dq add + dq fetch_value_stack, emitstring ; We deallocate the string by dropping it. ; @@ -3801,8 +3833,8 @@ defword dot, 0 dq drop dq lit, 1, sub - dq dup, zbranch, 3*8, branch, -28*8 - dq drop, drop, exit + dq dup, zbranch, 3*8, branch, -51*8 + dq drop, drop, drop, exit ; In: ; base |