Files
R8Forge/asm/digits.r8asm
T
dsh d9689f81ba asm: drop the design-story comments, leave the contract (and a pointer)
Seven files still opened with a paragraph about how they came to be -- "the first
version cleared the same bit four times", "the older repeated-subtraction divmod
needed 1.48M steps", "last round rejected shift-and-add because it was longer",
nested with measured step counts.  None of that tells a caller how to use the
macro, and it is exactly the material that goes stale (raw_sub's figure had been
retracted, and the numbers are SPEC's job now).  The stories already live in
docs/divmod.md and docs/byte-ops.md, so:

    digits    119 -> 80 lines     mul      125 -> 98
    divmod    106 -> 80           inc_ip    81 -> 72
    undigits   95 -> 73           raw_sub   52 -> 38
    dec_ip     21 -> 18

Each now opens with two lines: what the macro computes and where the contract
lives (SPEC section, plus docs/ when there is a design write-up).  Everything
from the `;   Macro <name>` block down -- parameter types, preconditions,
what-is-eaten, the non-obvious invariants -- is untouched, which is what
r8forge_macro_docs_test checks and what a caller actually reads.

Baseline unchanged: tape 64513, 21 byte-op items, 15 examples, 48 macros.
2026-11-09 17:20:44 +08:00

81 lines
2.1 KiB
Plaintext

; clb_digits:整数 -> 4 位 ASCII 十进制(高位在前、定长 4 位)。
; 契约和纸带长度见 SPEC §7.7,设计说明见 docs/byte-ops.md。
;
%include "asm/divmod.r8asm"
%include "asm/add_ip.r8asm"
;
; Macro clb_digits
; %1: Cell N
; %2: Cell REG
; %3: Cell S
;
; N 是值(被吃干净);REG..REG+3 是输出,ASCII 数字,高位在前;S 起的 13 个连续
; cell 是草稿(S=10、S+1=商、S+2=余数、S+3..S+9=divmod 草稿块、S+10=轮数、
; S+11/S+12=add_ip 的草稿),进来要求干净。
%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+10
set_1
%rep _CELL_SIZE * 30103 / 100000
rol
set_1
%endrep
; ---- 四位,一轮一位
loop %3+10
; 重建 TEN = 10(位 1、3):绕整圈,在 1 和 3 各置一个 1
erase %3+0
.tp %3+0
ror
set_1
%rep 2
ror
%endrep
set_1
%rep _CELL_SIZE - 3
ror
%endrep
; N = 这位数字(R,余数),Q = 商
clb_divmod %1 %3+0 %3+1 %3+2 %3+3
; ---- 已有数字整体上移一格:从高到低搬。
; 轮数 = L-1,用 %for 跟着位宽自动来(11 位 3 轮、16 位 4 轮)。
%for k _CELL_SIZE * 30103 / 100000
erase %3+8
.tp %2 + _CELL_SIZE * 30103 / 100000 - 1 - k
copy %2 + _CELL_SIZE * 30103 / 100000 - k %3+8
%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+2 %3+11 %3+12
; ---- 下一位的被除数(缓冲要干净)
erase %3+8
.tp %3+1
copy %1 %3+8
endloop %3+10
.tp before_clb_digits
%endmacro