Files
R8Forge/asm
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
..