The other machine's ROT-8-viz was three commits behind and still had the old serial
INP ("only the low 7 bits change, leave the rest alone"), so the calculator printed
> 0000 there while the same 62800 character tape printed > 0005 in the current
checkout. Recompiling "fixed it by magic" -- it was the whole-cell INP commit.
Worth writing down because the failure mode is deceptive: viz is the authority, so
a stale authority looks like a program bug. The bullet now says to verify the
binary is fresh first, and names the symptom.
87 lines
3.0 KiB
Plaintext
87 lines
3.0 KiB
Plaintext
; clb_divmod:二进制长除法(移位-恢复),商 Q、余数 R 各占一格,N、D 保留。
|
|
; 契约见 SPEC §7.6,设计推理和实测见 docs/divmod.md。
|
|
;
|
|
%include "asm/raw_sub.r8asm"
|
|
%include "asm/raw_or.r8asm"
|
|
;
|
|
; Macro clb_divmod
|
|
; %1: Cell N
|
|
; %2: Cell D
|
|
; %3: Cell Q
|
|
; %4: Cell R
|
|
; %5: Cell S
|
|
;
|
|
; Q = N div D,R = N mod D。**N 和 D 都保持原样**(旧版把 N 吃成余数)。
|
|
; Q 由宏自己清零后累加。
|
|
; S 起的 7 个**连续** cell 是草稿:进来**没有一格要求是干净的**。buf 现在在 S+6,
|
|
; 宏开头先 `erase` 它 —— 它是 copy/bit_copy 的缓冲,缓冲带垃圾时拷贝结果是 0。
|
|
; 其它格子脏了没关系:要么整格重写(raw_add 逐位写 Sd/Bo),要么只按一位用
|
|
; (Rhi/Cm/Bo/Ci),NR 每次进来都会重建成 W 个 1。
|
|
; (`clb_digits` 就是这么反复用同一块草稿的:第二轮起它是脏的。)
|
|
; 前提 D >= 1(除零没有定义,这里不检查)。
|
|
%macro clb_divmod 5
|
|
.anchor before_clb_divmod
|
|
|
|
; s+0 Rhi : 本轮左移出来的高位(轮内布尔)
|
|
; s+1 NR : 轮数计数器
|
|
; s+2 Cm : 提交标志 = Rhi | Bo(借 raw_sub 的 Bc 那个草稿格用)
|
|
; s+3 Ci : raw_sub 的进位草稿
|
|
; s+4 Bo : raw_sub 的"没借位"(1 表示 Rlo >= D)
|
|
; s+5 Sd : 减法结果
|
|
; s+6 buf : 拷贝缓冲(bit_copy / copy 共用,先后用完都是干净的),入口先 erase
|
|
;
|
|
; 这张表是 5040 种"角色 -> s+k"排列全部汇编、在最小驱动上跑 0=2047 量出来的
|
|
; (docs/divmod.md 第 6 节)。buf 不在 S+5 的排列多付一条入口 erase,成本已经
|
|
; 算进搜索里;换来的好处是草稿区从此不需要调用者保证任何一格干净。
|
|
|
|
erase %4
|
|
erase %3
|
|
erase %5+6
|
|
|
|
; 轮数:_CELL_SIZE 个 1,每轮 set_0 + ror 吃掉一个。一串 1 的长度就是剩余轮数,
|
|
; 所以"做几轮"不占纸带长度,只占运行时间(和 clb_mul 一样的计数法子)。
|
|
.tp %5+1
|
|
%rep _CELL_SIZE
|
|
set_1
|
|
ror
|
|
%endrep
|
|
|
|
.tp %5+1
|
|
biz
|
|
; ---- 1. R = (Rhi:Rlo) << 1 | (N 的下一位)
|
|
.tp %1
|
|
rol ; 下一位进 N 的 LSB
|
|
.tp %4
|
|
rol ; Rlo 左移一格,最高位绕回 bit0
|
|
clb_bit_mov %5+0 ; 绕回位 -> Rhi(先搬走,顺带清掉 Rlo 的 bit0)
|
|
.tp %1
|
|
bit_copy %4 %5+6 ; Rlo 的 bit0 = N 的 LSB(N 保留)
|
|
|
|
; ---- 2. S = Rlo - D,Bo = (Rlo >= D)。D 不能被吃,所以走 raw_sub
|
|
raw_sub %4 %2 %5+5 %5+4 %5+2 %5+3 %5+6
|
|
|
|
; ---- 3. 提交条件 = Rhi | Bo(clb_bit_or 把两个输入都吃掉,Rhi 因此归零)
|
|
clb_bit_or %5+0 %5+4 %5+2
|
|
|
|
; ---- 4. 商左旋一位;提交时 R = S,商位记 1
|
|
.tp %3
|
|
rol ; rol 之后 Q 的 bit0 一定是 0(还没写满 W 位)
|
|
.tp %5+2
|
|
biz
|
|
.tp %5+5
|
|
copy %4 %5+6 ; R = S
|
|
.tp %3
|
|
set_1 ; 商位 = 1
|
|
.tp %5+2
|
|
flb ; 把条件吃掉(biz/rnz 是循环对)
|
|
rnz
|
|
|
|
; ---- 5. 这一轮结束,轮数吃掉一个
|
|
.tp %5+1
|
|
set_0
|
|
ror
|
|
rnz
|
|
|
|
.tp before_clb_divmod
|
|
%endmacro
|