The library had add, subtract (with a free compare in its carry), negate,
increment, decrement, multiply, and the bit ops -- but nothing that divides.
Asking for a decimal expansion without it means inlining a division per digit,
which is where the 11k character estimate came from.
clb_divmod N D Q W V T Ci X BUF leaves Q = N div D and N itself as the
remainder, eating D. Plain repeated subtraction:
Q = 0
while N >= D: N -= D ; Q += 1
Two things it took to get right:
* clb_sub_nc eats both operands, and the subtraction cannot alias (measured:
clb_sub_nc A B A Ci with A=15 B=10 gives 0), so each round needs copies of
N and D. Two of the three copies are pure overhead, but they are in the
loop body, which appears once.
* The first round has to be able to do nothing. Setting T = N and Q = -1
before the loop makes the first commit a no-op and lets the phantom count
cancel out, so the probe can live at the *bottom* of the loop and appear
only once. Without that the probe is duplicated and the tape grows by
about 1.3k.
5839 characters with a minimal driver (5845 with the two outputs), 9 cells,
all 76 sampled pairs correct in r8forge_byte_op_test. Runtime is O(N/D): the
worst case 2047/1 crosses the 5M step budget and hangs, so the test keeps
N/D <= 600 and SPEC says so. Division by zero is undefined, D >= 1 is the
caller's job.
Next: clb_digits on top of this, which needs a shared loop plus per-cell
conditional writes (computed addressing does not exist here) rather than four
copies of divmod.
78 lines
2.3 KiB
Plaintext
78 lines
2.3 KiB
Plaintext
%include "asm/raw_sub_nc.r8asm"
|
|
%include "asm/add_ip.r8asm"
|
|
|
|
; 除法/取模:Q = N div D,R = N mod D。
|
|
;
|
|
; 用它自己那句老话:**只有纸带长度是目标**。所以这里不写长除法的移位/恢复,
|
|
; 就是最钝的那招:
|
|
;
|
|
; Q = 0
|
|
; while N >= D: N -= D ; Q += 1
|
|
;
|
|
; 循环体在纸带里只出现一次,长度和 N、D 的大小无关;代价是运行 O(N/D)
|
|
; (N=2047、D=1 时约 2047 轮,是这台机器上最慢的宏,但还在 500 万步以内)。
|
|
;
|
|
; 两个关键点,都是踩出来的:
|
|
;
|
|
; 1. **`clb_sub_nc` 会把 N 和 D 都吃掉**,而减法不能用 `S == A` 的别名
|
|
; (实测 `clb_sub_nc A B A Ci` 直接给出 0)。所以每轮都要 copy 出 W = N、
|
|
; V = D 再减 —— 三个 copy 里有两个是给它交的"过路费"。
|
|
;
|
|
; 2. **首轮要能"什么都不做"**:把 T 预置成 N,Q 预置成 -1。于是第一轮的
|
|
; "提交 T -> N" 是空操作,"Q += 1" 正好把 -1 补回 0;探测放在循环体末尾,
|
|
; 所以 `N < D`(零轮)和 `N == D`(一轮)都对,而且探测代码只出现一次。
|
|
; 没有这个技巧就得在循环外面再写一遍探测,纸带多 1.3k。
|
|
;
|
|
; 除零没有定义:调用者保证 D >= 1,这里不检查(D = 0 会一直循环)。
|
|
|
|
; Macro clb_divmod
|
|
; %1: Cell N
|
|
; %2: Cell D
|
|
; %3: Cell Q
|
|
; %4: Cell W
|
|
; %5: Cell V
|
|
; %6: Cell T
|
|
; %7: Cell Ci
|
|
; %8: Cell X
|
|
; %9: Cell BUF
|
|
;
|
|
; N 是被除数,结束时就是余数 R;D 是除数,会被吃干净;Q 是商(宏自己清零后累加)。
|
|
; W、V、T、Ci、X、BUF 都是草稿 cell,进来时要求是干净的。
|
|
; 前提 D >= 1(除零没有定义,这里不检查)。
|
|
%macro clb_divmod 9
|
|
.anchor before_clb_divmod
|
|
|
|
; T = N:第一轮"提交"是空操作
|
|
.tp %1
|
|
copy %6 %9
|
|
|
|
; Q = -1:第一轮的 +1 把它补回 0
|
|
erase %3
|
|
clb_dec_ip %3 %8
|
|
|
|
; Ci = 1 只为让底下那个 biz 直接进循环体;
|
|
; 真正的判断在体尾的 clb_sub_nc 里
|
|
.tp %7
|
|
set_1
|
|
|
|
biz
|
|
; ---- 体:提交上一轮减出来的 T,记一个商,再探一次
|
|
.tp %6
|
|
copy %1 %9 ; N = T
|
|
|
|
clb_inc_ip %3 %8 ; Q += 1
|
|
|
|
.tp %1
|
|
copy %4 %9 ; W = N
|
|
.tp %2
|
|
copy %5 %9 ; V = D
|
|
.tp %7
|
|
set_1 ; 进位入 = 1,做的是普通减法
|
|
clb_sub_nc %4 %5 %6 %7 ; T = N - D,Ci = (N >= D)
|
|
|
|
.tp %7
|
|
rnz
|
|
|
|
.tp before_clb_divmod
|
|
%endmacro
|