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.
61 lines
1.6 KiB
Plaintext
61 lines
1.6 KiB
Plaintext
%include "asm/add_ip.r8asm"
|
|
|
|
; §8 原地 `_ip` 家族(`asm/inc_ip.r8asm`、`asm/not_ip.r8asm`、`asm/add_ip.r8asm`)的例程。
|
|
;
|
|
; 这一组的共同点:**结果写回原来的 cell**,草稿只有一个 `T`(clb_add_ip 多一个 F):
|
|
; clb_not_ip A A = ~A(反码)
|
|
; clb_inc_ip A T A = A + 1
|
|
; clb_neg_ip A T A = -A(补码取负)
|
|
; clb_dec_ip A T A = A - 1
|
|
; clb_add_ip A B T F A += B,B 被吃干净
|
|
; clb_nonzero B F T F = (B != 0),B 不动
|
|
; 对比写回 S 的切片版(clb_inc_nc 3 cell、clb_neg_nc 3 cell),原地版少一个 cell、短五倍:
|
|
; `clb_add_ip` 是"while B != 0: A += 1; B -= 1",所以运行是 O(B),纸带和 B 无关。
|
|
;
|
|
; 纸带:r8forge examples/byte_in_place.r8asm > /tmp/i.rot8
|
|
; r8forge_tape_run /tmp/i.rot8
|
|
;
|
|
; out 2042 2043 5 4 14 0 0 0 1 7
|
|
|
|
.var A 0 ; 主力 cell
|
|
.var T 1 ; 所有 _ip 宏共用的草稿
|
|
.var B 2 ; clb_add_ip 的加数 / clb_nonzero 的被测值
|
|
.var F 3 ; clb_add_ip、clb_nonzero 的标志位
|
|
|
|
; ---- clb_not_ip:5 -> ~5 = 2042
|
|
.tp A
|
|
.xor 5
|
|
clb_not_ip A
|
|
output A
|
|
|
|
; ---- clb_inc_ip:2042 + 1 = 2043
|
|
clb_inc_ip A T
|
|
output A
|
|
|
|
; ---- clb_neg_ip:-2043 = 5(-A = ~A + 1)
|
|
clb_neg_ip A T
|
|
output A
|
|
|
|
; ---- clb_dec_ip:5 - 1 = 4(B - 1 = ~(-B))
|
|
clb_dec_ip A T
|
|
output A
|
|
|
|
; ---- clb_add_ip:A += B,B 被吃掉(4 + 10 = 14)
|
|
.tp B
|
|
.xor 10
|
|
clb_add_ip A B T F
|
|
output A
|
|
output B
|
|
|
|
; ---- clb_nonzero:B == 0 时 F = 0(B 不动)
|
|
clb_nonzero B F T
|
|
output F
|
|
output B
|
|
|
|
; ---- clb_nonzero:B = 7 时 F = 1(注意是 set_1 不是 flb,见 §8)
|
|
.tp B
|
|
.xor 7
|
|
clb_nonzero B F T
|
|
output F
|
|
output B
|