Files
R8Forge/drv/calc_num.r8asm
dsh b873d71082 calc: stage 2 verified -- the digit window and clb_undigits
drv/calc_num.r8asm reads 6 characters, shifts digits into a four cell ASCII
window (D3 D2 D1 D0, most significant first) and converts it with clb_undigits
whenever a non digit arrives, then resets the window to "0000".

    in  12 34      out  12 34        tape 88413, 78908 steps
    in  0 7        out  0 7 0 0      (each EOF read is a terminator too)
    in  2047       out  2047 0
    in  9999       out  1807 0       (9999 wraps mod 2048, as the machine does)
    in  1234/0007  out  1234 0 / 7 0  (leading zeros work)

tape_run and derive agree on every case.

One deliberate departure from docs/calc.md: the digit branch copies the character
itself into the window instead of C - 48.  clb_undigits xors each cell with 48 on
the way in, so the window has to hold ASCII -- storing the numeric value paid the
xor twice (first version: "12 34" -> 540 562, e.g. 49^48=1 but 1^48=49 then * 10 +
... lands at 540).  So cell 2 (the 48 constant) and the sub_nc call are unused;
the file says so.

The window shift goes high to low (D3 <- D2 <- D1 <- D0) which is the order that
does not clobber unshifted digits; each copy is preceded by an erase of the
buffer even though this build's copy turns out to clean it itself.
2026-11-11 10:22:58 +08:00

145 lines
6.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
; calc_num —— 桌上计算器 stage 2:把 4 位数字窗口攒成整数(docs/calc.md §3 第 2 步)。
;
; 固定读 6 个字符(%rep 6,没有 EOF 处理),每读一个:
; '0'..'9'(bit4 = 1) -> 攒进 4 位窗口 REG:把字符搬进 D0,窗口整体右移一格;
; 其它(空格/CR/EOF=0) -> clb_undigits 把窗口换成整数、output,再把窗口复位成 "0000"。
;
; 内存地图(clb_undigits 要 4 个连续格、MSB 在前,所以 D3 在低地址):
; cell 0 C 当前字符
; cell 1 T C 的一份拷贝(窗口里要放 ASCII,见下面"攒数字")
; cell 2 — 设计里给常数格 K('0' = 48)留的位置;这一阶段用不上
; cell 3..6 REG D3 D2 D1 D0,ASCII 数字,3 是最高位(clb_undigits 从 REG+0 往下吃)
; cell 7 BUF copy 的缓冲格(每份 copy 前都擦一遍)
; cell 8 N 解出来的整数
; cell 9..19 S clb_undigits 的 11 格草稿
;
; 判数字:和 stage 1 一样只做 bit4 粗筛 —— 转 4 格把 bit4 转到最低位,biz 判 cell&1。
;
; 攒数字:窗口里放的是 **ASCII 数字**,不是数字值 —— clb_undigits 每格进来先 ^48
; ('0'..'9' -> 0..9,SPEC §7.9),所以搬 C 本身,不能搬 C - '0':数字值会被再 ^48
; 一次。第一版就是照"t = C - 48 写进 D0"写的,实测 "0" 换出 48、"12" 换出 540
; (1^48 = 49、2^48 = 50),所以数字这一支只做拷贝:
; copy C -> T ; set_0 清掉 C 的条件位 ; 窗口右移 ; D0 <- T
; 窗口右移:D3<-D2、D2<-D1、D1<-D0、D0<-T —— 四个 copy 从高到低搬
; (低位先搬会把还没搬的高位盖掉;注意这和 clb_digits 的挪位方向正好相反)。
;
; 两个分支的形状:第一个 biz 的身体只做"数字"这一支,做完 erase F(把本轮"还没攒
; 数字"的标志清掉);第二个 biz 的身体做"非数字"这一支。每个身体都回到自己的条件格
; (净位移 0),见 AGENTS.md "语义上会咬人的几条" 第 2 条。
;
; F 借的是 S+0:clb_undigits 每轮都会整格重建 S+0(TEN),所以攒数字的时候借来当
; 本轮标志,脏的没关系。
;
; copy 的缓冲格:宏的契约是"缓冲必须干净"(SPEC §7.7 记过脏缓冲的坑,库里的
; clb_undigits 也是每份 copy 前都 erase 一遍缓冲格),这里照做 —— 虽然实测这份
; build 的 copy 结束时缓冲格自己会转回 0。
;
; 验证:
; build/Release/r8forge_derive.exe drv/calc_num.r8asm 0 -i "12 34"
; in 12 34
; out 12 34
; (空格把 12 换出来,读完 "34" 之后的 EOF=0 把 34 换出来。)
;
; 另外三组(r8forge_tape_run 的结果一样):
; -i "0 7" -> out 0 7 0 0 (' ' 换出 0;EOF 换出 7;再两次 EOF 各换出 0)
; -i "2047" -> out 2047 0
; -i "9999" -> out 1807 0 (9999 超过 11 位 cell 的 2047,Horner 按模 2048 绕回)
;
; 边界:和 stage 1 一样只用 bit4 粗筛,所以协议外的 0x10..0x1F、0x3A..0x3F 以及
; bit4 = 1 的大写字母(@ A..O)也会落进数字分支(本阶段不喂这些字符)。
;
; stage 2 仍然固定读 6 个字符:更长的输入只吃前 6 个;更短的输入后面读到 EOF=0,
; 按键值 0 当"非数字",于是会把当前窗口再换出来一次(上面 0 7 / 2047 / 9999 的尾巴
; 就是这么来的)。EOF 退出留给 stage 5 的主循环。
;
; 这条纸带 88413 字符;"12 34" 一次 78908 步(另外三组:0 7 -> 137436 步、
; 2047 -> 81000 步、9999 -> 82705 步)。
%include "asm/lib.r8asm"
%include "asm/undigits.r8asm" ; clb_undigits(顺带带进 clb_mul / clb_add_nc)
; Macro load
; %1: Cell operand
; %2: Count value
;
; 先把整格清干净,再 `.xor` 上常数(`.xor` 是异或,不清格就变成和旧值异或)。
%macro load 2
erase %1
.tp %1
.xor %2
%endmacro
.var C 0 ; 当前字符
.var T 1 ; C 的拷贝
.var REG 3 ; 4 位 ASCII 窗口(MSB 在前)
.var D3 REG+0 ; 最高位
.var D2 REG+1
.var D1 REG+2
.var D0 REG+3 ; 最低位:新数字写这里
.var BUF 7 ; copy 的缓冲格
.var N 8 ; 解出来的整数
.var S 9 ; clb_undigits 的草稿区(9..19,共 11 格)
.var F S+0 ; 借 S+0:本轮"还没攒过数字"的标志(1 = 要转换)
; ---- 开机:窗口 = "0000"(4 个 '0',换出来就是 0);其余格子干净 ----
load D3 48
load D2 48
load D1 48
load D0 48
%rep 6
load F 1 ; 默认"非数字":攒了数字的那一支会把它擦掉
.tp C
inp
; bit4 -> bit0:数字 0x30..0x39 是 1,空格/CR/EOF=0 是 0
%rep 4
ror
%endrep
.tp C
biz
; ---- 数字:把 C 搬进窗口的 D0(窗口整体右移一格) ----
%rep 7
ror ; 4 + 7 = 11,转回原字符
%endrep
.tp C
erase BUF ; copy 的缓冲格先擦干净
copy T BUF ; T = C(copy 的源是当前格)
.tp C
set_0 ; 条件位清 0:ASCII 已经拷进 T,C 不用留了
.tp F
erase F ; 攒过数字了 -> 下面的转换跳过
; 窗口右移:从高到低搬(低位先搬会把还没搬的高位盖掉)
.tp D2
erase BUF
copy D3 BUF ; D3 <- D2
.tp D1
erase BUF
copy D2 BUF ; D2 <- D1
.tp D0
erase BUF
copy D1 BUF ; D1 <- D0
.tp T
erase BUF
copy D0 BUF ; D0 <- T(新的 ASCII 数字)
.tp C ; 身体回到条件格;C 的 bit0 已经清掉,rnz 不跳回
rnz
; ---- 非数字:窗口 -> 整数,打出来,再复位成 "0000" ----
.tp F
biz
clb_undigits REG N S
output N
load D3 48
load D2 48
load D1 48
load D0 48
.tp F
erase F ; 标志清 0,rnz 不跳回
rnz
%endrep