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

125 lines
2.5 KiB
Plaintext

%include "asm/raw_add_nc.r8asm"
%include "asm/raw_sub_nc.r8asm"
%include "asm/raw_neg_nc.r8asm"
%include "asm/raw_inc_nc.r8asm"
%include "asm/raw_and.r8asm"
%include "asm/raw_or.r8asm"
%include "asm/raw_xor.r8asm"
; §7.2 `clb_*_nc` 家族 + §7.3 `clb_and/or/xor` 的例程。
;
; 这一组把操作数**吃干净**(换来少一个草稿 cell):
; clb_add_nc A B S Ci S = A+B+Ci,A、B 结束为 0
; clb_inc_nc A S Ci S = A+Ci
; clb_neg_nc A S Ci S = ~A+Ci(Ci=1 就是 -A)
; clb_sub_nc A B S Ci S = A+~B+Ci(Ci=1 就是 A-B)
; 进位 cell 是**真正的进位输入**:给 0 是普通运算,给 1 是"自带 +1"。
; 每个宏后面把被吃掉的操作数打出来,顺便看进位输出的附带语义:
; `clb_sub_nc` 的进位是"没有借位"(A >= B 时为 1),`clb_neg_nc` 只在 A 原本是 0 时为 1。
;
; §7.3 那三个是**半破坏**的:clb_or / clb_xor 干净地吃掉 A、B,而
; `clb_and` 只在"A、B 都是 1"的那几位清 B —— 所以 B 结束在 `B & ~A`,
; 不是 0(240 & 204 -> ans=192、A=0、B=12)。这个例程把残留打出来。
;
; 纸带:r8forge examples/byte_clobber.r8asm > /tmp/c.rot8
; r8forge_tape_run /tmp/c.rot8
;
; out 300 0 0 52 1 5 1 2043 0 2043 0 0 1 192 0 12 252 0 0 60 0 0
.var A 0
.var B 1
.var S 2
.var Ci 3
; ---- clb_add_nc:200 + 100 = 300,A、B 被吃干净
.tp A
.xor 200
.tp B
.xor 100
.tp Ci
set_0
clb_add_nc A B S Ci
output S
output A
output B
; ---- 进位出:2000 + 100 = 2100 -> S = 52,进位 1
.tp A
.xor 2000
.tp B
.xor 100
.tp Ci
set_0
clb_add_nc A B S Ci
output S
output Ci
; ---- clb_sub_nc:15 - 10 = 5,进位 = "没有借位" = 1
.tp A
.xor 15
.tp B
.xor 10
.tp Ci
set_1
clb_sub_nc A B S Ci
output S
output Ci
; ---- 反过来:10 - 15 = -5 = 2043,进位 0(有借位)
.tp A
.xor 10
.tp B
.xor 15
.tp Ci
set_1
clb_sub_nc A B S Ci
output S
output Ci
; ---- clb_neg_nc:5 的补码是 2043,进位 0(A 不是 0)
.tp A
.xor 5
.tp Ci
set_1
clb_neg_nc A S Ci
output S
output Ci
; ---- clb_inc_nc:2047 + 1 回绕成 0,进位 1
.tp A
.xor 2047
.tp Ci
set_1
clb_inc_nc A S Ci
output S
output Ci
; ---- clb_and 的 B 残留:240 = 0b11110000,204 = 0b11001100
.tp A
.xor 240
.tp B
.xor 204
clb_and A B S
output S ; 192
output A ; 0
output B ; 12 = 204 & ~240,不是 0!
; ---- clb_or / clb_xor 是干净的
.tp A
.xor 240
erase B ; clb_and 把 B 留成 12,这里要的是 204
.tp B
.xor 204
clb_or A B S
output S ; 252
output A ; 0
output B ; 0
.tp A
.xor 240
.tp B
.xor 204
clb_xor A B S
output S ; 60
output A ; 0
output B ; 0