clb_digits_signed N REG S T F prints '-' (45) and then |N| as the same four zero padded digits; clb_divmod_signed N D Q R S T1 T2 F is the C style truncated division (q = trunc(n/d), r = n - q*d, remainder follows the dividend). Both are wrappers: clb_digits / clb_divmod still do the work, so the tape only grows by the sign handling -- 572 characters for the display (12151 vs 11579 in the same driver layout) and 1191 for the division (6018 vs 4827). The sign bit is read with rol + bit_copy + ror, 49 characters. clb_bit_mov is deliberately NOT used: it eats the source bit, so N comes back as N - HALF and 1024 -- whose magnitude 1024 only fits the unsigned range -- prints as 0000. A clb_sub_nc against a HALF constant was the other candidate: 765 characters for the call plus ~60 for the constant cell. A flag has to outlive the block that consumes it, because biz/rnz only exit once the body clears the condition bit. So sign(N) is moved into T2, sign(D) into F, and the remainder block flips F into sign(N)^sign(D) on its way out; a single conditional negation of the quotient then carries the xor. The negation scratch is S+6: the only cell in S with a "clean on entry" contract, and clb_neg_ip leaves it zero again for clb_divmod. Verified by r8forge_byte_op_test: all 2048 values for the display (worst 117606 steps) and 4202 division pairs -- every |a|,|b| <= 32 plus the 0/+-1/+-1023/+-1024 boundary combinations -- compared against C's / and %. examples/signed_demo.r8asm is the single point regression that runs as a program (the flagship cannot carry them: +12.1k / +5.6k on a 62351 tape).
174 lines
6.7 KiB
Plaintext
174 lines
6.7 KiB
Plaintext
; clb_digits:整数 -> 4 位 ASCII 十进制(高位在前、定长 4 位)。
|
|
; 契约和纸带长度见 SPEC §7.7,设计说明见 docs/byte-ops.md。
|
|
;
|
|
%include "asm/divmod.r8asm"
|
|
%include "asm/add_ip.r8asm"
|
|
%include "asm/inc_ip.r8asm" ; clb_neg_ip(带符号显示要用)
|
|
;
|
|
; Macro clb_digits
|
|
; %1: Cell N
|
|
; %2: Cell REG
|
|
; %3: Cell S
|
|
;
|
|
; N 是值(被吃干净);REG..REG+3 是输出,ASCII 数字,高位在前;S 起的 13 个连续
|
|
; cell 是草稿,进来要求干净。
|
|
;
|
|
; ---- 13 格草稿的排法(按"少走路"重排过,见下面"布局"一段) ----------------
|
|
;
|
|
; S+0 clb_add_ip 的 T S+1 余数 R(这一位的数字)
|
|
; S+2..S+8 clb_divmod 的草稿块(Rhi、Sd、Bo、Cm=Bc、Ci、buf、NR)
|
|
; S+9 TEN(除数) S+10 商 Q
|
|
; S+11 clb_add_ip 的 F S+12 轮数计数器(loop)
|
|
;
|
|
; ---- 布局:这台机器没有随机寻址,每次访问的代价就是 |Δ地址| --------------
|
|
;
|
|
; 旧排法是 TEN、商、余数、草稿块、轮数、add_ip 的 T、F 依次排下来,结果每轮都要
|
|
; 在块前面和块里面来回走:divmod 每轮拿 TEN 当 raw_sub 的被减数(copy TEN -> Bc,
|
|
; 缓冲就是块里的 buf),TEN 却离 buf 有 8 格。现在的三条依据:
|
|
;
|
|
; * 余数 R 紧贴在草稿块**前面**、TEN 紧贴在**后面**。R 是 raw_add 的 A,TEN 是
|
|
; raw_sub 的 B,这两个距离都乘进内层 11×11 的位循环里。实测:只把 TEN 从最前
|
|
; 面挪到块后面(其它单元的先后不变)就省 22,226 步,是这次改动的大头。
|
|
; * add_ip 的 T 紧挨着 R(clb_inc_ip / clb_dec_ip 的进位链每轮在这两格之间来回);
|
|
; 光把 T 和 R 换个位置就差 4,450 步。
|
|
; * 轮数计数器只被 loop 每轮碰一次,踢到最远的一头(S+12)。
|
|
;
|
|
; 效果(最小驱动,见 SPEC §7.7):纸带 13,717 -> 11,717 字符;2047 这个值
|
|
; 145,114 -> 113,264 步。2048 个值全扫:最坏 164,843 -> 123,731(值 1999)、
|
|
; 平均 144,904 -> 112,552。**契约一个字没改**:还是"REG 起 4 格、S 起 13 格
|
|
; 连续且干净",只有格子内部的排法变了。
|
|
%macro clb_digits 3
|
|
.anchor before_clb_digits
|
|
|
|
; ---- K = L 个连续的 1(第 0..L-1 位),给 loop 用
|
|
;
|
|
; L = 十进制位数 = floor(_CELL_SIZE * log10(2)) + 1,用 30103/100000 做整数近似:
|
|
; 11 位 -> 4、16 位 -> 5、8 位 -> 3、32 位 -> 10,和 2^W-1 的真实位数一致。
|
|
; **轮数因此自动跟着位宽走**,不用手改。
|
|
.tp %3+12 ; 轮数计数器
|
|
set_1
|
|
%rep _CELL_SIZE * 30103 / 100000
|
|
rol
|
|
set_1
|
|
%endrep
|
|
|
|
; ---- 四位,一轮一位
|
|
loop %3+12
|
|
; 重建 TEN = 10(位 1、3):绕整圈,在 1 和 3 各置一个 1
|
|
erase %3+9 ; TEN
|
|
.tp %3+9
|
|
ror
|
|
set_1
|
|
%rep 2
|
|
ror
|
|
%endrep
|
|
set_1
|
|
%rep _CELL_SIZE - 3
|
|
ror
|
|
%endrep
|
|
|
|
; N = 这位数字(R,余数),Q = 商
|
|
clb_divmod %1 %3+9 %3+10 %3+1 %3+2 ; TEN、Q、R、草稿块
|
|
|
|
; ---- 已有数字整体上移一格:从高到低搬。
|
|
; 轮数 = L-1,用 %for 跟着位宽自动来(11 位 3 轮、16 位 4 轮)。
|
|
; 缓冲借 divmod 的 buf(S+7),它在 divmod 结束时是干净的。
|
|
%for k _CELL_SIZE * 30103 / 100000
|
|
erase %3+7 ; buf
|
|
.tp %2 + _CELL_SIZE * 30103 / 100000 - 1 - k
|
|
copy %2 + _CELL_SIZE * 30103 / 100000 - k %3+7
|
|
%endfor
|
|
|
|
; ---- REG+0 = '0' + 数字(位 4、5)。
|
|
; 走一圈:转 4 格置位、再转 1 格置位,最后转回原点。
|
|
; **尾部的转数必须写成 `_CELL_SIZE - 5`**,不能写死 6 —— 16 位下
|
|
; 写死会让 0x30 建错(实测 16 位时得到 0x600)。
|
|
erase %2+0
|
|
.tp %2+0
|
|
%rep 4
|
|
ror
|
|
%endrep
|
|
set_1
|
|
ror
|
|
set_1
|
|
%rep _CELL_SIZE - 5
|
|
ror
|
|
%endrep
|
|
clb_add_ip %2+0 %3+1 %3+0 %3+11 ; A=REG+0、B=R、T=T1、F
|
|
|
|
; ---- 下一位的被除数(缓冲要干净)
|
|
erase %3+7 ; buf
|
|
.tp %3+10 ; Q
|
|
copy %1 %3+7 ; N = Q
|
|
endloop %3+12
|
|
|
|
.tp before_clb_digits
|
|
%endmacro
|
|
|
|
; Macro clb_digits_signed
|
|
; %1: Cell N
|
|
; %2: Cell REG
|
|
; %3: Cell S
|
|
; %4: Cell T
|
|
; %5: CellBit F
|
|
;
|
|
; 带符号的 clb_digits:按二补码解释 N(第 _CELL_SIZE-1 位是符号位),负数先打一个
|
|
; '-' (45),再打 **|N|** 的四位十进制,和 clb_digits 一样定宽补零、高位在前:
|
|
;
|
|
; 7 -> 48 48 48 55 "0007"
|
|
; 2047 -> 45 48 48 48 49 "-0001" (2047 = -1)
|
|
; 1024 -> 45 49 48 50 52 "-1024" (1024 = -1024,|N| 仍放得进无符号范围)
|
|
; 0 -> 48 48 48 48 "0000"
|
|
;
|
|
; N 被吃干净(最后交给 clb_digits);REG 起 4 格、S 起 13 格**必须干净**、T 进来
|
|
; 必须干净,契约与 clb_digits 完全一样。F 是符号标志格,结束时是 0。
|
|
;
|
|
; ---- 符号位怎么读:转一格,让它落到 bit0 ----
|
|
;
|
|
; `biz` 只判 bit0,而符号位在 bit W-1。`rol`(= W-1 次 `ror`)正好把 bit W-1 转到
|
|
; bit0,再用 **bit_copy**(保留源)把它抄进 F,最后 `rol` 的逆操作 `ror` 把 N 原样
|
|
; 还回去。三段一共 10 + 38 + 1 个字符。
|
|
;
|
|
; 为什么不用"clb_sub_nc T HALF 看有没有借位"那种比较:clb_sub_nc 展开一次就是
|
|
; 765 字符,还要再花 ~60 字符造 HALF 常数格。转位法一整段才 49 字符。
|
|
;
|
|
; **不能用 clb_bit_mov 抄符号位**:它会顺手把源位清掉,于是 N 少了符号位、转回
|
|
; 原样之后变成 N-HALF —— 而正是负数那一支要拿 N 取负,少了这个位 |N| 就错
|
|
; (1024 会打成 0)。bit_copy 走缓冲格,源位原样保留,这正是它比 clb_bit_mov
|
|
; 多出来的那 27 个字符买的东西。
|
|
;
|
|
; ---- 负数那一支 ----
|
|
;
|
|
; '-' 放在 S+10(clb_divmod 的商格):**第一轮 divmod 进来第一件事就是 erase 它**
|
|
; (asm/divmod.r8asm 开头的 `erase %3`),所以这个常数格不用调用者事后擦,也不破坏
|
|
; "S 起 13 格干净"这个入口契约(我们只在 S+10 留下 45,后面自然被清)。
|
|
;
|
|
; 取负走 clb_neg_ip(2 个 cell、110 字符);T 在 bit_copy 之后是干净的,正好当它的
|
|
; 草稿格,取负结束又回到 0。
|
|
%macro clb_digits_signed 5
|
|
.anchor before_clb_digits_signed
|
|
|
|
; ---- 1) F = 符号位,N 里那一位不动
|
|
.tp %1
|
|
rol ; bit W-1 -> bit0
|
|
.tp %1
|
|
bit_copy %5 %4 ; F = 符号,N 保留(缓冲 T 结束是干净的)
|
|
.tp %1
|
|
ror ; N 转回原样
|
|
|
|
; ---- 2) 负数:先打 '-' 再把 N 取负
|
|
.tp %5
|
|
biz
|
|
load %3+10 45 ; '-' = 45,放商格(divmod 第一轮会 erase 它)
|
|
output %3+10
|
|
clb_neg_ip %1 %4 ; N = -N = |N|
|
|
.tp %5
|
|
flb ; 吃掉条件(biz/rnz 是循环对)
|
|
rnz
|
|
|
|
; ---- 3) |N| 走原来的宏
|
|
clb_digits %1 %2 %3
|
|
|
|
.tp before_clb_digits_signed
|
|
%endmacro
|