Files
R8Forge/drv/digits.r8asm
T
dsh a515e823d1 clb_digits: integers become a four digit ASCII string
clb_digits N REG S writes '2' '0' '4' '7' into REG..REG+3 for 2047 -- ASCII
digits, most significant first, fixed width four, zero padded, which is what
was asked for after the ASCII/order/source questions.

divmod appears once, wrapped in a loop that runs four times.  The first
version dispatched each digit to its own cell with four branches and a one-hot
flag handoff, and only the first branch ever fired; the digits themselves were
right (checked by printing N after every division: 204, 20, 2, 0).  Rather than
keep chasing that, the dispatch is gone: each round shifts the digits already
written one cell up (high to low, so nothing gets clobbered) and the new digit
always goes to REG+0.  Four rounds later REG+0..3 are thousands, hundreds, tens,
ones in that order.  Simpler, two nesting levels less, four cells less, and it
works: 0/1/7/10/42/99/100/512/1000/1234/2047 all correct.

Two real traps found on the way, both now in the macro comments and SPEC:

  * copy's buffer cell must be *clean*: a dirty buffer makes the copy produce
    0 (measured: Q = 4, buffer = 0x7f8 -> N = 0).  The library only says the
    buffer is borrowed, never that it has to be zeroed.
  * building a constant cannot be done as 'set a bit, rotate, set another,
    rotate': the second rotation carries the first bit away.  Walk the ring
    once instead -- W rotations, set_1 at each target bit, then back home.

10829 characters with a minimal driver (13743 with the four outputs), 16 cells
in total, and r8forge_byte_op_test's test_digits covers all 11 values.
2026-11-07 17:18:02 +08:00

19 lines
429 B
Plaintext

%include "asm/digits.r8asm"
; clb_digits 的驱动:0 号 cell 放值,1..4 是输出区域,5 起是草稿。
;
; r8forge_derive drv/digits.r8asm -1 0=2047 # 50 48 52 55 = "2047"
; r8forge_derive drv/digits.r8asm -1 0=7 # 48 48 48 55 = "0007"
;
; out 依次是区域四格(ASCII 码,所以 48 是 '0')。
.var N 0
.var REG 1
.var S 5
clb_digits N REG S
output REG+0
output REG+1
output REG+2
output REG+3