Files
R8Forge/docs/calc.md
T
dsh 10f164b480 docs: the signed wrappers in SPEC, AGENTS, README and TODO
SPEC gains 7.10 with both contracts, the five design points and the measured
tape lengths; 5.4 stops saying "implementation pending" and spells out why the
range is asymmetric -- |N| can be outside the signed range while still fitting
unsigned, so 1024 has to print -1024 and the absolute value cannot be done by
clearing the sign bit.

Numbers re-measured on this machine, because some had drifted: the flagship is
62351 characters / 202159 steps (documented as 62513 / 206491), --opt takes it
to 56338, the calculator is 62624, the byte level test is 23 items over 22
macros, and asm/ has 55 macros in 23 files.

tests/widths.ps1 no longer compares the viz's serial output below 8 bit cells:
the viz prints one number per OUT there, so the 7 bit "viz FAILED" was the
script's expectation, not the tape -- the calculator itself passes at 7 bits
through r8forge_derive, and the same failure reproduces with the previous
calculator (checked against HEAD in a temp worktree).  The TODO gains the open
question of whether the flagship should keep the 65536 limit at all, plus the
evidence that a 16 bit tape of 96052 characters runs fine in the viz.
2026-11-18 21:14:07 +08:00

6.0 KiB
Raw Blame History

桌上计算器(RPN)设计与分阶段计划

目标:examples/calc.r8asm —— 一条长驻的纸带,跑了就是一个计算器:

> 12 34 + .
46
> 2047 3 / .
682
> ,

条件已经齐了:inp 在三份 VM 里都有实现、tape_run/derive 能用 -i 喂输入、例程头部能写 ; in(examples_test 会把那串字符交给 inp)。

1. 命令协议

输入 动作
'0'..'9' 攒进 4 位数字缓冲(遇到终止符一次性 clb_undigits 换成整数,压栈)
'*' '+' '-' '/' 弹两个、算、压回;'-' 在"还在等操作数"时是一元负号(§7)
'.' 打印栈顶(clb_digits_signed + 4 个 output)
',' 清栈
其它(空格/回车) 忽略(作为数字的终止符)
0(EOF) erase 掉循环计数器,主循环结束,纸带自然跑完

六个命令字符 * + , - . / 正好是 0x2A..0x2F 连续,数字是 0x30..0x39,所以:

t = C - 0x2A                     ; 命令: 0..5  数字: 6..15
数字  <=> C 的 bit4 == 1         ; 一次 ror×4 + biz
命令号 = t                       ; 0=* 1=+ 2=, 3=- 4=. 5=/

2. 内存地图

cell 用途
0 C:当前字符
1 T:分派/临时
2..3 A、B:运算操作数
4 HAVE:栈里有没有数(0/1)
5 CNT:主循环计数器(开机造 W 个 1)
6..9 数字缓冲 D0..D3(ASCII,右对齐)
10..12 栈指针之外的栈深度标志?——v1 固定 4 深,见 §4
16..26 库草稿(mul/divmod/digits/undigits 的 S)
32..43 常量区(开机造一次):TEN(10)、'0'(48)、11 个 1、2047、~10

3. 分阶段计划(每步都能单点验证)

每个驱动都放在 drv/,用 r8forge_derive drv/<名字>.r8asm 0 -i "..." 跑,; out 逐项核对。

  1. drv/calc_lex.r8asm:只做分派。喂 "0 5 9 + - . , *",输出"这是数字(d) / 这是命令(k)"。 验证点:bit4 判断 + t 的取值范围。
  2. drv/calc_num.r8asm:"12"+终止符 → 整数 12(4 位缓冲右移 + clb_undigits)。 验证点:"0" "9" "10" "2047" 四个输入。
  3. drv/calc_disp.r8asm:整数 → > 提示符 + 4 位 ASCII + 换行。
  4. drv/calc_op.r8asm:12 34 + → 46(+ - * / 各一次)。
  5. examples/calc.r8asm:主循环 + EOF 退出 + ; in/; out 头部,进 examples_test。
  6. 有符号数(做完,见 §7):显示换 clb_digits_signed、除法换 clb_divmod_signed、 加一元负号。3 5 - . → -0002、-5 2 * . → -0010、-7 -2 / . → 0003。

4. 栈:v1 固定 4 深,不用指针

.tp 是汇编期常量,没有间接寻址,所以不要做动态栈指针。v1 就用 4 个固定格 S0..S3: 压栈 = 整体往高地址挪一格(copy 从高到低,3 格),弹栈 = 反向。深度超过 4 就不管 (压第 5 个覆盖最底下的,像老式计算器)。

代价:每次压/弹 3 个 copy ≈ 1.5k 字符纸带 + 每字符几百步。如果觉得贵,v0 可以只做两格 (A、B),先把 12+34= 跑通,再加栈。

5. 步数预算(按 11 位实测的每例代价)

动作 步数
攒一个数(clb_undigits) ≤ 36,481
+/-(clb_add_nc/clb_sub_nc) ~2.5k
*(clb_mul) ≤ 20,300
/(clb_divmod_signed) ≤ 35,202(byte_op_test 全扫的最坏值;无符号版是 31,579)
打印(clb_digits_signed) ≤ 117,606(byte_op_test 2048 个值的最坏值;无符号版 119,431)

一行 12 34 + . 实测 280,645 步(有符号之前是 314,777 之类,examples/calc.r8asm 的头部写着这一轮的全部实测值)。examples_test 的上限是 2000 万步,所以会话控制在 十几行以内(或把打印换成"里程表"版本,见下)。

6. 已知的坑(都在库里踩过)

  • 条件体的身体必须回到自己的条件格(净位移 0),否则 .tp 全错位;
  • copy 的缓冲格必须干净(脏了结果是 0);
  • 用 loop/endloop 计数器时,erase 计数器就能提前退出(EOF 就这么退);
  • 库宏每轮都重建 TEN 和轮数计数器 —— 这个终端的常量区(cell 32..43)就是为了把这些 搬到循环外,v2 再让 clb_undigits/clb_digits 支持"外部常量"版本。

7. 有符号数(第 6 步,做完)

约定在 SPEC §5.4、宏在 SPEC §7.10。计算器这边只动三处:

  1. 显示:clb_digits → clb_digits_signed(负数自己打 '-',然后打 |A| 的定宽四位)。

  2. 除法:clb_divmod → clb_divmod_signed(截断、余数随被除数)。加、减、乘不用动, 二补码的模 2^W 运算天然对。

  3. 一元负号:这是唯一需要新状态的地方。判据是 cell NEED:

    NEED = 1 表示"在等一个新操作数"(表达式开头 / 刚结算完 / 刚打完运算符 / 刚 ',')。 这时字符 45 是一元负号:置 NEG、清 ISCMD(第 3 步据此这一轮不改 OP);下一个 打进窗口的数在 flush 里被 clb_neg_ip 取负。NEED = 0 说明 P 里已经有一个数等着 结算,45 就是二元减号。

为什么不用"HAVE == 0 就当作一元负号"(最初的计划就是那样):HAVE 只表示"正在攒 数字",打完运算符之后它也是 0,于是 2 * -5 . 里的负号会被判成二元减号,OP 被改成 2(减),结果错。NEED 多出来的信息就是"这个位置本来该出现一个操作数"。

作废的旧例子:2047 3 / . → 0682 是无符号时代的。有符号之后 2047 的位型是 -1, -1/3 截断成 0,现在打 > 0000;要 341 就写 1023 3 / .。能打的整数范围从 0..2047 变成 -1024..1023。

实测(r8forge_tape_run):3 5 - . → > -0002、-5 2 * . → > -0010、 -7 -2 / . → > 0003、-1024 2 / . → > -0512、0 3 - . → > -0003。 纸带 59519 → 62624 字符(两套包装宏 + 判据,约 +3.1k)。