Files
R8Forge/asm/fibonacci.r8asm
dsh 19cbfb36f6 R8Forge: assembler and searcher in one repo, %synth directive
The searcher stops being a separate program: %synth is an assembly-time
directive.  It describes a truth table, the assembler runs the search while
parsing, and the block list comes back as ready-to-paste r8asm.

Merging is what makes the cost model honest.  The price of a layer is
  cost = 2 (biz + rnz) + sum(move distance + op cost)
and the move distance depends on the call site's cell layout, not on anything
the assembler knows in advance: the same five-layer adder is 87 characters
with the cells adjacent and 136 with them two apart.  Only the side that sees
the layout can price it, which is now the assembler itself.

Working: the assembler (asm/test.r8asm still assembles to the same 1714
characters), the searcher (bit planes + backward one-step lookup, full_add in
5 layers, proven shortest in 7.2s), and %synth parsing plus the search itself.
Not working yet: splicing the synthesised body into the tape -- a synth call
fails loudly with 'unknown macro' instead of emitting anything, so nothing can
silently assemble wrong.  The two ways to finish it are in the README.

Also carries over the CMake fix: no set(CMAKE_CXX_FLAGS ...), which used to
drop /EHsc, push -O3 into Debug and fight /RTC1.
2026-11-05 23:59:15 +08:00

89 lines
2.3 KiB
Plaintext

; fibonacci.r8asm
;
; 斐波那契发生器,顺便当 r8asm 的一份冒烟测试:
; 宏、%include、表达式、.var/.anchor/.tp、.xor、biz/rnz、raw_add 都用上了。
;
; 这里不写死 8 位:计数器用 _CELL_MAX,循环长度自然等于 _CELL_SIZE。
; 8 位时的预期输出(用 rot8_test 跑出来的十进制数):
; 1 1 2 3 5 8 13 21 34 55
;
; 数据布局:每个变量占一个 Unit(数据 Cell + 辅助 Cell),
; 辅助 Cell 留给 copy 当位缓冲,raw_add 也会用到相邻的草稿 Cell。
;
; A a
; B b
; S a + b
; X a 的副本 —— raw_add 会把两个操作数都吃掉,所以每圈都得重新复印
; Y b 的副本
; Z 常数 0,当进位输入用
; CO 进位
; T 进位用的一位草稿
; CNT 循环计数器
; BUF copy 用的位缓冲
%include "asm/raw_add.r8asm"
; 一个 Unit 两个 Cell:数据 Cell + 辅助 Cell
.var UNIT 2
.var A 0
.var B (A + UNIT)
.var S (B + UNIT)
.var X (S + UNIT)
.var Y (X + UNIT)
.var Z (Y + UNIT)
.var CO (Z + UNIT)
.var T (CO + UNIT)
.var CNT (T + UNIT)
.var BUF (CNT + UNIT)
; ---------------------------------------------------------------- 前两项
; a = 1,b = 1,先打出来
.tp A
.xor 1
.tp B
.xor 1
output A
output B
; ---------------------------------------------------------------- 循环入口
; CNT 里塞满 _CELL_SIZE 个 1:一个 1 就是一圈。
; biz/rnz 在 rot-8 里就是一对 [ ],运行时按括号配对,
; 所以这里不用写标签,位置本身就是标签。
.tp CNT
.xor _CELL_MAX
biz
; ---------------------------------------------------------------- 循环体
; s = a + b
; raw_add 是破坏性的,先把它要吃的 a、b 各复印一份
.tp A
copy X BUF
.tp B
copy Y BUF
; 进位输入和进位输出都先清零
erase Z
erase CO
; S = X + Y + Z,进位从 CO 出来
raw_add X Y Z S CO T
; 打印新的一项
.tp S
output S
; 滚动:a <- b,b <- s
.tp Y
copy A BUF
.tp S
copy B BUF
; ------------------------------------------------------------ 计数
; 吃掉最低位的 1,再把下一位转下来,
; 等 _CELL_SIZE 个 1 都吃干净,最低位变成 0,循环结束
.tp CNT
flb
ror
rnz