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

65 lines
1.4 KiB
Plaintext

%include "asm/mul.r8asm"
; §9 乘法器(`clb_mul`,10 cell)的例程。
;
; clb_mul MC MUL PL PH MCA S Ci T F NR ; (PH:PL) = MC * MUL
;
; 算法是**移位-加法**(竖式乘法),固定跑 `_CELL_SIZE` 轮,跟乘数的值无关:
; A:PH = 0 ; C:F = 0 ; Q:PL = MUL
; 每轮: if Q 的最低位: (C:PH) += MC ;然后 (C:PH:PL) >>= 1
; W 轮之后 (PH:PL) 就是积;PL 原来装乘数,被移空,所以 **MUL 结束为 0**,
; MC 保持原样。积可能超过一个 cell(11 位),高半在 PH 里。
;
; 这里算两个:82 * 141 = 11562(低半 1322、高半 5),以及
; 2047 * 2047(11 位域里最大的一对,旧版重复加法到这儿直接 HANG)。
; 两轮各用一块 cell,互不干扰。
;
; 纸带:r8forge examples/mul_demo.r8asm > /tmp/m.rot8
; r8forge_tape_run /tmp/m.rot8
;
; out 1322 5 82 0 1 2046
; 82 * 141
.var MC 0
.var MUL 1
.var PL 2
.var PH 3
.var MCA 4
.var S 5
.var Ci 6
.var T 7
.var F 8
.var NR 9
; 2047 * 2047(整块换一份干净草稿)
.var MC2 10
.var MUL2 11
.var PL2 12
.var PH2 13
.var MCA2 14
.var S2 15
.var Ci2 16
.var T2 17
.var F2 18
.var NR2 19
; ---- 82 * 141 = 11562 = 5 * 2048 + 1322
.tp MC
.xor 82
.tp MUL
.xor 141
clb_mul MC MUL PL PH MCA S Ci T F NR
output PL
output PH
output MC ; 被乘数保留
output MUL ; 乘数被移空
; ---- 2047 * 2047 = 4190209 = 2046 * 2048 + 1
.tp MC2
.xor 2047
.tp MUL2
.xor 2047
clb_mul MC2 MUL2 PL2 PH2 MCA2 S2 Ci2 T2 F2 NR2
output PL2
output PH2