From f7ac81b06ff73f6a95d74113c1fc4c0ef46fa5e2 Mon Sep 17 00:00:00 2001 From: dsh Date: Tue, 17 Nov 2026 19:55:32 +0800 Subject: [PATCH] asm: clb_puts and load -- printing a sentence, and the constant helper clb_puts REG LEN prints LEN consecutive cells as characters (the low 7 bits, which is what serial OUT sends). LEN has to be an assembly time constant: .tp takes a literal address here, so "print until the cell is zero" is not expressible -- the walk would leave the data pointer somewhere else. Each character costs two .tp plus one out (three or four tape characters), uses no extra cell and leaves the source cells alone. load N V is the helper every driver was writing by hand (erase then .xor): .xor is an exclusive or, so a second load without the erase would mix with the old value. drv/puts.r8asm 198 chars, in-family check: out 72 73 33 72 (HI! plus the untouched source cell) examples/hello.r8asm 727 chars, out 72 69 76 76 79 44 32 82 56 33, and the reference machine prints "HELLO, R8!" Both are documented in SPEC 5.2; the macro count goes 48 -> 50 and the examples 16 -> 17. The flagship tape is untouched (64513) because asm/test.r8asm still has its own local load -- same body, and lib's definition comes first, so nothing changed. --- AGENTS.md | 4 ++-- README.md | 4 ++-- SPEC.md | 14 +++++++++++++- TODO.md | 4 ++-- asm/lib.r8asm | 31 +++++++++++++++++++++++++++++++ drv/puts.r8asm | 10 ++++++++++ examples/hello.r8asm | 18 ++++++++++++++++++ 7 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 drv/puts.r8asm create mode 100644 examples/hello.r8asm diff --git a/AGENTS.md b/AGENTS.md index 162daa9..81d2a7f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -32,8 +32,8 @@ cmake --build build --config Release # 当前基线:都通过,别弄坏(`ctest --test-dir build -j 4` 一次跑完 8 条,约 9 秒) r8forge asm/test.r8asm # 64513 字符(每个标准宏至少一次 + 每个语言特性) r8forge_byte_op_test # 21 项(divmod 16384 组、digits 2048、undigits 4096) -r8forge_examples_test # 16 个 example + asm/test.r8asm,核对头部 `; out`/`; in` -r8forge_macro_docs_test # 48 macros in 22 files +r8forge_examples_test # 17 个 example + asm/test.r8asm,核对头部 `; out`/`; in` +r8forge_macro_docs_test # 50 macros in 22 files r8forge_synth_test examples/full_add.r8asm # all eight combinations ok r8forge_derive verify_loop.r8asm -1 0=5 # out 5 0 powershell -File tests\widths.ps1 # 位宽矩阵:每个位宽一套工具链 + 断言(约 20 分钟) diff --git a/README.md b/README.md index f833155..2b736ca 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,9 @@ include/r8forge/r8veritas.hh 搜索器 include/r8forge/geometry.hh 默认几何(11 位 × 259 格) src/r8asm.cc 汇编器实现 src/synth.cc 真值表 -> 搜索 -asm/ 标准库(48 个宏,含 8 个 %synth 合成块) +asm/ 标准库(50 个宏,含 8 个 %synth 合成块) drv/ 单点验证驱动(喂固定输入跑一个宏,看 out/stat) -examples/ 16 个能跑的例程:每组宏一份 + %synth 的几种写法 + 一个交互例程 +examples/ 17 个能跑的例程:每组宏一份 + %synth 的几种写法 + 一个交互例程 tests/ 汇编器 / 虚拟机 / 字节级 / 例程 / 文档格式 自检 docs/ 设计笔记(长文) .r8forge_cache/ 合成结果缓存(已入库) diff --git a/SPEC.md b/SPEC.md index 2cbfa88..771ce30 100644 --- a/SPEC.md +++ b/SPEC.md @@ -329,6 +329,18 @@ endloop C --- + +### 5.2 常量与文本:`load` 和 `clb_puts` + +`load N V`:把汇编期常数 `V` 装进格 `N` —— `erase` 再 `.xor`,两步都要有(`.xor` 是异或,不清格就是对旧值异或)。 + +`clb_puts REG LEN`:把 `REG` 起 `LEN` 个连续 cell 的低 7 位当字符依次打出来。`LEN` 必须是 +**汇编期常数**:没有间接寻址,`.tp` 只能写死,"遇到 0 就停"的运行期长度做不到。每个字符 +约 3~4 字符纸带(两条 `.tp` 加一条 `out`),不占额外 cell,源格保留。 + +两个都是 `asm/lib.r8asm` 里的基础宏;`examples/hello.r8asm`(727 字符)和 `drv/puts.r8asm` +(198 字符)分别演示和单点验证它们。 + ## 6. 位级宏(一个 cell 装一位) ### 6.1 总表 @@ -750,7 +762,7 @@ r8forge_tape_run t.rot8 # 打印 out 和步数 |---|---|---| | `r8forge_byte_op_test` | 21 个测试项 = 20 个宏(`clb_neg_nc` 的 Ci=1 / Ci=0 各算一项),见 §7.1、§7.2、§7.5、§7.6、§7.7、§7.9、§8、§9 | **按返回值**:穷举 0..255(`clb_add_ip` 2048 抽样、`clb_mul` 65536 全穷举、`clb_ones` 12 个计数全穷举、`clb_divmod` 8 个除数 × 全部 2048 个被除数 = 16384 组全穷举、`clb_digits` 全部 2048 个值全穷举、`clb_undigits` 2048 个值 × 干净/高位垃圾 2 种 = 4096 组全穷举),任一不符即退出码非 0 | | `r8forge_synth_test examples/full_add.r8asm` | `%synth` 端到端:汇编 + 跑 8 种输入 | `all eight combinations ok` | -| `r8forge_examples_test` | `examples/` 里全部 16 个程序(10 组宏例程 + 4 个 `%synth` 例子 + 交互例程 + 计算器,见 §10.3)**加上 `asm/test.r8asm`**(§10.2) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败;写了 `; in ...` 的先把那串字符喂给 `inp` | +| `r8forge_examples_test` | `examples/` 里全部 17 个程序(10 组宏例程 + 4 个 `%synth` 例子 + 交互例程 + 计算器,见 §10.3)**加上 `asm/test.r8asm`**(§10.2) | **按返回值**:每个头部 `; out ...` 与实跑 `out` 逐项相等,缺这一行也算失败;写了 `; in ...` 的先把那串字符喂给 `inp` | | `r8forge verify_clb_bit_add_nc.r8asm` | 4 cell 加法切片(进位进→出) | 单点:`0+0+Ci` | | `r8forge verify_clb_bit_inc_nc.r8asm` | 3 cell 自增切片 | 单点 | | `r8forge_derive verify_loop.r8asm -1 0=N` | 动态循环:`N=0..11` 时循环体跑 `N` 次(`A = N`、`C = 0`) | 逐值 `out` | diff --git a/TODO.md b/TODO.md index d1119e8..6815f71 100644 --- a/TODO.md +++ b/TODO.md @@ -17,8 +17,8 @@ * **终端纸带**:提示符 + 逐字符回显 + 行编辑(退格打印 `\b \b`)+ 命令分派。零件都在 `drv/calc_*`(分派、数字攒取、显示、四则)——缺的是"长驻循环 + 落行 + 会话结束"。 验收用头部 `; in` 喂一整段会话,`examples_test` 会自动跑。 -* **文本输出**:`clb_puts`(把连续 cell 当字符串打出来)+ 迷你 `printf`(`%d` `%c` `%s`)。 - 有了它,例程能打印整句话而不是光秃秃的数字,终端纸带也用得上。 +* **迷你 `printf`**:`clb_puts` 和 `load` 已经有了(`examples/hello.r8asm` 能打一整句),还缺 + `%d` / `%c` / `%s` 的格式化 —— 那需要"格式串 + 参数表"的约定,比 puts 复杂一档。 ## 工具 diff --git a/asm/lib.r8asm b/asm/lib.r8asm index 25d25d6..36940ce 100644 --- a/asm/lib.r8asm +++ b/asm/lib.r8asm @@ -185,3 +185,34 @@ ror .tp %1 rnz %endmacro + +; Macro load +; %1: Cell N +; %2: Count V +; +; 把一个汇编期常数装进一格:先 `erase` 再 `.xor`。两步都要有 —— `.xor` 是异或,不清格 +; 就是"和旧值异或",同一格连着 load 两次会得到两个常数的异或。 +%macro load 2 +.tp %1 +erase %1 +.xor %2 +%endmacro + +; Macro clb_puts +; %1: Cell REG +; %2: Count LEN +; +; 把 REG 起 LEN 个连续 cell 的低 7 位当字符依次打出来(串行 I/O 下就是一行字)。 +; LEN 必须是**汇编期常数**:这台机器没有间接寻址,`.tp` 只能写死地址,所以"遇到 0 就停" +; 那种运行期长度做不到(会拐进别的格子)。每个字符的代价是两条 `.tp` 加一条 `out` +; (三四个字符纸带),不占额外 cell,源格也原样保留。 +; +; load MSG 72 ; "H" +; load MSG+1 73 ; "I" +; load MSG+2 33 ; "!" +; clb_puts MSG 3 ; 打印 H I ! +%macro clb_puts 2 +%for k %2 + output %1+k +%endfor +%endmacro diff --git a/drv/puts.r8asm b/drv/puts.r8asm new file mode 100644 index 0000000..e42b21d --- /dev/null +++ b/drv/puts.r8asm @@ -0,0 +1,10 @@ +%include "asm/lib.r8asm" + +; clb_puts ????:MSG ??? "HI!",???,? output ?????????? +; out 72 73 33 72 +.var MSG 10 +load MSG 72 +load MSG+1 73 +load MSG+2 33 +clb_puts MSG 3 +output MSG diff --git a/examples/hello.r8asm b/examples/hello.r8asm new file mode 100644 index 0000000..3622c26 --- /dev/null +++ b/examples/hello.r8asm @@ -0,0 +1,18 @@ +; out 72 69 76 76 79 44 32 82 56 33 +; +; clb_puts ?????:?? ASCII ?? "HELLO, R8!",?????? +; ?? I/O ???????(ROT-8-viz ????,????)? +%include "asm/lib.r8asm" + +.var MSG 40 +load MSG 72 +load MSG+1 69 +load MSG+2 76 +load MSG+3 76 +load MSG+4 79 +load MSG+5 44 +load MSG+6 32 +load MSG+7 82 +load MSG+8 56 +load MSG+9 33 +clb_puts MSG 10