Files
R8Forge/examples/divmod_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.2 KiB
Plaintext

%include "asm/divmod.r8asm"
; §7.6 除法/取模(`clb_divmod`,11 cell)的例程。
;
; clb_divmod N D Q R S ; Q = N div D,R = N mod D
;
; 二进制长除法,固定跑 `_CELL_SIZE` 轮(11 轮),**跟 N、D 的值无关**。
; 旧版是重复减法、O(N/D),下面第二个例子(`D = 1`)在旧版上会直接撞
; 500 万步上限;现在两个用例都是十一轮(约三万步)。
;
; 契约:N、D 保持原样(可以反复用),Q 由宏清零后累加,R 自己一格,
; S 起 7 个连续草稿格。**草稿格进来不要求干净**(只有 S+5 的拷贝缓冲要在
; 第一次用之前是干净的),所以同一个 S 块连续用两次没问题 —— 这里就是这么做的。
;
; 纸带:r8forge examples/divmod_demo.r8asm > /tmp/d.rot8
; r8forge_tape_run /tmp/d.rot8
;
; out 204 7 2047 10 2047 0
.var N 0
.var D 1
.var Q 2
.var R 3
.var S 4 ; S..S+6 是 7 格草稿
; ---- 2047 / 10 = 204 余 7
.tp N
.xor 2047
.tp D
.xor 10
clb_divmod N D Q R S
output Q
output R
output N ; 保留
output D ; 保留
; ---- 把 D 换成 1,再算一次:2047 / 1 = 2047 余 0(旧版在这里 HANG)
erase D
.tp D
.xor 1
clb_divmod N D Q R S
output Q
output R