Files
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

99 lines
2.0 KiB
Plaintext

; clb_mul:二进制移位-加法乘法器,(PH:PL) = MC * MUL。
; 契约见 SPEC §9,设计说明见 docs/byte-ops.md。
;
%include "asm/lib.r8asm"
%include "asm/raw_add_nc.r8asm"
;
; Macro clb_mul
; %1: Cell MC
; %2: Cell MUL
; %3: Cell PL
; %4: Cell PH
; %5: Cell MCA
; %6: Cell S
; %7: Cell Ci
; %8: Cell T
; %9: Cell F
; %10: Cell NR
;
; (PH:PL) = MC * MUL。MC 保持原样,MUL 结束为 0。
; MCA、S、Ci、T 是草稿;F 是移位寄存器的最高位(进位),NR 是轮数计数器。
%macro clb_mul 10
.anchor before_clb_mul
; A = PH = 0, C = F = 0, Q = PL = MUL
.tp %4
erase %4
.tp %3
erase %3
.tp %9
erase %9
.tp %2
copy %3 %8
.tp %2
erase %2
; 轮数计数器:_CELL_SIZE 个 1,每轮吃掉一个
.tp %10
%rep _CELL_SIZE
set_1
ror
%endrep
.tp %10
.anchor mul_iter
biz
; ---- 1. 乘数这一位是 1 就把被乘数加到积的高半:(C:PH) += MC
; 进位出到 Ci,再搬进 F(搬之前 F 一定是 0)。
; `biz`/`rnz` 是一对循环,不是 if/else:body 必须把被测位吃掉(最后那个
; flb),否则 rnz 看到还是 1 就跳回去。被测的正好是 PL 的最低位,
; 而它本来就要在右移时被丢掉,吃掉它不影响移位。
.tp %3
biz
.tp %1
copy %5 %8
.tp %7
set_0
clb_add_nc %4 %5 %6 %7
.tp %6
copy %4 %8
.tp %7
clb_bit_mov %9
.tp %3
flb
rnz
; ---- 2. (F:PH:PL) 右移一位:C 补进 PH 的最高位,PH 的最低位补进 PL 的最高位,
; PL 的最低位(这一轮刚看过的乘数位)丢掉。先低位那对,再高位那对。
.tp %9
ror
.tp %4
ror
.tp %3
ror
.tp %4
rol
.tp %3
rol
.tp %4
clb_bit_mov %3
.tp %9
rol
clb_bit_mov %4
.tp %9
ror
.tp %4
ror
.tp %3
ror
; ---- 3. 这一轮结束,计数器吃掉一个 1
.tp %10
set_0
ror
.tp mul_iter
rnz
.tp before_clb_mul
%endmacro