Files
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

42 lines
1.4 KiB
Plaintext

%include "asm/ones.r8asm"
; §5.1 动态循环(`clb_ones` + `loop`/`endloop`)+ §7.5 `clb_ones` 的例程:
; 算 1 + 2 + ... + N,N 是**运行期**的值(不是汇编期常数)。
;
; clb_ones N C T F ; C = N 个 1,N 被吃掉
; loop C ... endloop C ; 每圈吃掉 C 里的一个 1 —— 跑几圈由数据决定
;
; 11 位 cell 装得下 0..2047,这里取 N = 10(1+...+10 = 55)。N = 0 时 `biz`
; 直接跳过,一圈不跑(和也是 0)—— 这就是"次数来自数据"和 `%rep` 的区别。
; 循环体只出现一次,所以纸带长度和 N 无关,运行是 O(N);`clb_add_ip` 自己又是
; O(当前项),整体 O(N^2),N <= 2047 时几十万步以内。
;
; 纸带:r8forge examples/sum_loop.r8asm > /tmp/s.rot8
; r8forge_tape_run /tmp/s.rot8
;
; out 55 0 0
.var N 0 ; 项数(会被 clb_ones 吃掉,结束为 0)
.var C 1 ; 循环配额:N 个 1,跑完是 0
.var I 2 ; 当前项
.var S 3 ; 累加和
.var T 4 ; _ip 家族的草稿
.var F 5 ; clb_add_ip 的标志
.var buf 6 ; copy 的缓冲(必须是干净的)
.var tmp 7 ; 当前项的副本
.tp N
.xor 10
clb_ones N C T F ; C = 10 个 1,N 变成 0
loop C
clb_inc_ip I T ; I += 1
.tp I
copy tmp buf ; tmp = I(copy 保留 I;缓冲自己会转干净)
clb_add_ip S tmp T F ; S += tmp,tmp 被吃掉
endloop C
output S
output C
output N