asm: raw_puts_dyn -- print a buffer whose length is only known at run time
A terminal has to echo the line the user just typed, and that length is a cell, not a constant; clb_puts cannot do it because .tp takes a literal address. So this one walks MAX compile time slots and decides per slot whether to print: CNT starts as a copy of LEN, clb_nonzero turns "CNT != 0" into the flag F, and the print happens inside `biz F` with the countdown *inside* the guarded body as well. That last detail is the whole bug this round: with clb_dec_ip outside the guard, a CNT of 0 wrapped to 2047 on the first skipped cell and the rest of the buffer came out (LEN=0 printed E F G H). The fix is also why the driver covers LEN = 3, 0 and 8 -- the zero case is what caught it. drv/puts_dyn.r8asm 13376 chars, out 65 66 67 65 66 67 68 69 70 71 72 (ABC, nothing, ABCDEFGH) reference machine "ABCABCDEFGH" 6306 steps Also learnt: `copy` takes two arguments (%this is the source), and it wants a clean buffer -- the first version passed three and then reused a dirty T, which showed up as a countdown that started from the wrong value. Not in asm/test.r8asm: an 8 slot version costs thousands of characters and the flagship is about a thousand under the 65536 limit. SPEC 5.3 records the contract and that exception; the macro count is 53.
This commit is contained in:
1 parent
37d1dca0c7
commit
a1f88e3d25
5 files changed
+69
-3
No files matched your search
@@ -30,10 +30,10 @@ cmake --build build --config Release
|
||||
# "No preprocessor test for PellesC")。本机路径见 AGENTS.local.md。
|
||||
|
||||
# 当前基线:都通过,别弄坏(`ctest --test-dir build -j 4` 一次跑完 8 条,约 9 秒)
|
||||
r8forge asm/test.r8asm # 64513 字符(每个标准宏至少一次 + 每个语言特性)
|
||||
r8forge asm/test.r8asm # 64513 字符(除 raw_puts_dyn 外每个宏一次 + 每个语言特性)
|
||||
r8forge_byte_op_test # 21 项(divmod 16384 组、digits 2048、undigits 4096)
|
||||
r8forge_examples_test # 17 个 example + asm/test.r8asm,核对头部 `; out`/`; in`
|
||||
r8forge_macro_docs_test # 52 macros in 22 files
|
||||
r8forge_macro_docs_test # 53 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 分钟)
|
||||
|
||||
@@ -82,7 +82,7 @@ include/r8forge/r8veritas.hh 搜索器
|
||||
include/r8forge/geometry.hh 默认几何(11 位 × 259 格)
|
||||
src/r8asm.cc 汇编器实现
|
||||
src/synth.cc 真值表 -> 搜索
|
||||
asm/ 标准库(52 个宏,含 8 个 %synth 合成块)
|
||||
asm/ 标准库(53 个宏,含 8 个 %synth 合成块)
|
||||
drv/ 单点验证驱动(喂固定输入跑一个宏,看 out/stat)
|
||||
examples/ 17 个能跑的例程:每组宏一份 + %synth 的几种写法 + 一个交互例程
|
||||
tests/ 汇编器 / 虚拟机 / 字节级 / 例程 / 文档格式 自检
|
||||
|
||||
@@ -341,6 +341,18 @@ endloop C
|
||||
两个都是 `asm/lib.r8asm` 里的基础宏;`examples/hello.r8asm`(727 字符)和 `drv/puts.r8asm`
|
||||
(198 字符)分别演示和单点验证它们。
|
||||
|
||||
|
||||
### 5.3 `raw_puts_dyn`:运行期长度的打印
|
||||
|
||||
`raw_puts_dyn REG MAX LEN CNT F T`:打印 `REG` 起最多 `MAX` 格里的前 `LEN` 格,`LEN` 是**运行期**
|
||||
的值(把一段缓冲原样打出来)。`LEN` 保持原样;`CNT` 是倒计数(结束时 0,`LEN > MAX` 时剩
|
||||
`LEN-MAX`);`F` 是每格的条件标志;`T` 兼作 `copy` 缓冲和 nonzero/dec_ip 的草稿、**进来必须
|
||||
干净**。`MAX` 仍是汇编期常数 —— 打印哪一格要写死 `.tp`,能变的只有"打不打"。
|
||||
|
||||
每格约 450 字符纸带、几百步;`drv/puts_dyn.r8asm` 单点验证(`LEN` = 3 / 0 / 8 → `ABC` / 空 /
|
||||
`ABCDEFGH`)。**它不在 `asm/test.r8asm` 里**:MAX=8 的版本要三万多字符,而全家桶离 65536
|
||||
只剩约一千。
|
||||
|
||||
## 6. 位级宏(一个 cell 装一位)
|
||||
|
||||
### 6.1 总表
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
%include "asm/lib.r8asm"
|
||||
%include "asm/add_ip.r8asm"
|
||||
%include "asm/dec_ip.r8asm"
|
||||
|
||||
; Macro raw_puts_dyn
|
||||
; %1: Cell REG
|
||||
; %2: Count MAX
|
||||
; %3: Cell LEN
|
||||
; %4: Cell CNT
|
||||
; %5: Cell F
|
||||
; %6: Cell T
|
||||
;
|
||||
; 打印 REG 起最多 MAX 格里的前 LEN 格,LEN 是**运行期**的值(把一段缓冲原样打出来)。
|
||||
; LEN 保持原样;CNT 当倒计数(结束时 0,LEN > MAX 时剩 LEN-MAX);F 是每格的条件标志;
|
||||
; T 兼作 copy 的缓冲和 nonzero/dec_ip 的草稿,进来必须干净。MAX 仍是汇编期常数 ——
|
||||
; 打印哪一格要写死 `.tp`,能变的只有"打不打"。
|
||||
%macro raw_puts_dyn 6
|
||||
.tp %6
|
||||
erase %6
|
||||
.tp %3
|
||||
copy %4 %6
|
||||
%for k %2
|
||||
clb_nonzero %4 %5 %6
|
||||
.tp %5
|
||||
biz
|
||||
output %1+k
|
||||
clb_dec_ip %4 %6
|
||||
.tp %5
|
||||
flb
|
||||
rnz
|
||||
%endfor
|
||||
%endmacro
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
%include "asm/puts_dyn.r8asm"
|
||||
|
||||
; raw_puts_dyn 单点验证:MSG 八格放 A..H,分别按 LEN = 3 / 0 / 8 打。
|
||||
; out 65 66 67 65 66 67 68 69 70 71 72
|
||||
.var MSG 10
|
||||
.var LEN 20
|
||||
.var CNT 21
|
||||
.var F 22
|
||||
.var T 23
|
||||
|
||||
%for k 8
|
||||
load MSG+k 65+k
|
||||
%endfor
|
||||
|
||||
load LEN 3
|
||||
raw_puts_dyn MSG 8 LEN CNT F T
|
||||
load LEN 0
|
||||
raw_puts_dyn MSG 8 LEN CNT F T
|
||||
load LEN 8
|
||||
raw_puts_dyn MSG 8 LEN CNT F T
|
||||
|
||||
Reference in new issue
Block a user