Files
R8Forge/examples/digits_demo.r8asm
dsh 3f4149c9d0 examples: one runnable routine per macro group
SPEC is the contract and drv/ is what the tests sweep, but there was no place
that showed a reader how a group is actually used.  examples/ now has one stand
alone program per group, each ending in output calls:

    base_macros.r8asm    S5   set_0 vs erase, rol/ror, copy vs bit_copy vs
                              clb_bit_mov, step
    bit_adder.r8asm      S6   4 bit ripple carry adder built from
                              clb_bit_add_nc, then bit_raw_add for the
                              preserving variant
    byte_raw_ops.r8asm   S7.1 raw_add/raw_inc/raw_and/raw_or/raw_xor/raw_sub,
                              printing the operands afterwards to show the
                              whole point of the family: they survive
    byte_clobber.r8asm   S7.2 clb_add/sub/neg/inc_nc (operands eaten) and the
                              S7.3 clb_and residue (240 & 204 -> B = 12)
    byte_in_place.r8asm  S8   not/inc/neg/dec/add_ip and clb_nonzero
    sum_loop.r8asm       S5.1 clb_ones + loop/endloop: 1+2+...+N, count from
                              data rather than from the assembler
    mul_demo.r8asm       S9   82*141 and 2047*2047
    divmod_demo.r8asm    S7.6 2047/10 and 2047/1 (the case that used to hang),
                              reusing the scratch block on purpose
    digits_demo.r8asm    S7.7 1234 and 7 -> four ASCII digits

Every header carries a `;   out 1 2 3` line: the expected OUT stream, so the
file itself says what it prints.  The new r8forge_examples_test target runs all
of examples/*.r8asm and compares that line with the real output (a file without
the marker counts as a failure), printing the tape length and step count on the
way.  That makes examples the second place after byte_op_test that checks
behaviour instead of just "it assembles", and keeps the examples from rotting as
the macros change.  The six %synth examples get the marker too: their inputs are
all zero, so they print 0 0.

All 15 examples pass.  The values in the headers were checked with
r8forge_tape_run (the second interpreter) as well as the test's own machine.

Docs: SPEC S10.3 has the table and the convention, S11 gets the test row, and
README gets a short section plus the layout list.  AGENTS records the examples
in the handoff baseline and the "verified" table.
2026-11-07 22:34:15 +08:00

44 lines
1.3 KiB
Plaintext

%include "asm/digits.r8asm"
; §7.7 `clb_digits`(整数 -> 4 位 ASCII 十进制,18 cell)的例程。
;
; clb_digits N REG S ; REG..REG+3 = '0'..'9',高位在前,定长 4 位
;
; 做法是"divmod 只出现一次":外面套一个跑 4 次的运行期循环,每轮把已经写好的
; 数字整体往高地址挪一格,新数字永远写 REG+0。所以输出是**定长 4 位、左边补 '0'**,
; 11 位 cell(0..2047)正好 4 位十进制。
;
; 两个用例:1234 -> "1234"、7 -> "0007"。第二个用另一块干净的 REG/S
; (第一个用过的格子已经被写脏了,而 clb_digits 要求 REG 和 S 进来是干净的)。
;
; 纸带:r8forge examples/digits_demo.r8asm > /tmp/dg.rot8
; r8forge_tape_run /tmp/dg.rot8
; (输出是 ASCII 码:48 = '0'、49 = '1'、...)
;
; out 49 50 51 52 48 48 48 55
.var N 0 ; 值(被吃干净)
.var REG 1 ; REG..REG+3 是第一个输出区
.var S 5 ; 第一块草稿(S..S+12)
.var REG2 18 ; 第二个输出区
.var S2 22 ; 第二块草稿(S2..S2+12)
; ---- 1234 -> '1' '2' '3' '4'
.tp N
.xor 1234
clb_digits N REG S
output REG + 0
output REG + 1
output REG + 2
output REG + 3
; ---- 7 -> '0' '0' '0' '7'
.tp N
.xor 7
clb_digits N REG2 S2
output REG2 + 0
output REG2 + 1
output REG2 + 2
output REG2 + 3