Files
R8Forge/asm/undigits.r8asm
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

74 lines
1.7 KiB
Plaintext

; clb_undigits:4 位 ASCII 十进制 -> 整数,clb_digits 的逆。
; 契约见 SPEC §7.9。
;
%include "asm/lib.r8asm"
%include "asm/mul.r8asm"
;
; Macro clb_undigits
; %1: Cell REG
; %2: Cell N
; %3: Cell S
;
; clb_digits 的逆:REG 起 D 格(低 7 位的 ASCII 数字)-> N。不做合法性判定。
%macro clb_undigits 3
.anchor before_clb_undigits
erase %2
; 轮数计数器:D 个 1(一个数字格一个),和 clb_digits 的计数法子一样。
; 先 erase:set_1 只清它转到的那一位,格子里原来的高位会变成额外的 1,
; 循环就会多跑几轮。
erase %3+1
.tp %3+1
set_1
%rep _CELL_SIZE * 30103 / 100000
rol
set_1
%endrep
loop %3+1
; ---- 当前位:只留低 7 位(ASCII),再 ^48 变成 0..9。
; 先 ror 7 格把第 7 位转到最低位,再 set_0+ror 清掉第 7..W-1 位 ——
; 一共转 W 格,结束时格子正好回到原来的方向。
.tp %1
%rep 7
ror
%endrep
%rep _CELL_SIZE - 7
set_0
ror
%endrep
.xor 48
; ---- 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
; ---- (PH:PL) = N*10(N 是 MC,保持不变;MUL 被吃掉)
clb_mul %2 %3+0 %3+3 %3+4 %3+5 %3+6 %3+7 %3+8 %3+9 %3+10
; ---- N = PL + 当前位。进位输入先清 0(上一轮留下的是进位输出)
.tp %3+7
set_0
clb_add_nc %3+3 %1 %2 %3+7
; ---- 数字整体下移一格:从低到高搬(源还没被盖掉)
%for k _CELL_SIZE * 30103 / 100000
erase %3+2
.tp %1 + k + 1
copy %1 + k %3+2
%endfor
endloop %3+1
.tp before_clb_undigits
%endmacro