Files
R8Forge/drv/calc_disp.r8asm
dsh 3543003a78 digits: reorder the scratch block -- 22% fewer steps, 15% less tape
The profile said 77% of clb_digits' steps are pointer moves, so the layout of its
13 cell scratch block (S..S+12) is the cost.  The block is one rigid 7 cell unit
(clb_divmod owns its internal order: Rhi,Sd,Bo,Cm=Bc,Ci,buf,NR) plus six free
units, so the legal space is 5040 permutations.  All 5040 were assembled and run
(minimal driver, value 2047):

  old: TEN Q R [block] counter T F
  new: T1 R [block] TEN Q F counter

   tape 13,717 -> 11,717 chars (-14.6%)
   steps 145,114 -> 113,264 (-21.9%)
   pointer moves 111,958 -> 80,108 (-28.4%)   non-move steps identical (33,156)

Full 2048 value sweep: worst 164,843 -> 123,731 (-24.9%), mean 144,904 -> 112,552
(-22.3%), better on all 2048 values.  The old layout ranked 3459/5040.

Two things worth remembering.  First, ordering the units by the wrapper's first
visit order is *worse* than the old layout (150,748 steps): first visit ignores
revisit frequency -- the counter is touched once per round, TEN is copied into Bc
every round, T1 is hammered by clb_add_ip's carry chain.  Second, the n-1 lower
bound is not reachable here: 74% of the steps still execute inside clb_divmod's
block, whose order is not clb_digits' to choose, so this lever is now exhausted
(that is what the exhaustive search proves).

Min tape is not min steps either: block-first BQTRT1F1C gives the shortest tape
(10,713) but 123,890 steps.  Tape counts a .tp once, steps count its execution.

Ripple: asm/test.r8asm 64,513 -> 62,513 chars, 236,439 -> 206,491 steps, INS_SIZE
headroom ~1k -> ~3k; --opt 58,060 -> 56,350; calc --opt 52,857; calc_op 77,356 ->
69,324 chars / 599,090 -> 476,170 steps.

Verified: ctest 8/8, byte_op_test 21 items (digits 2048/2048), examples_test 18,
--opt assembles, SPEC 7.7/10.2 + AGENTS + README + docs/calc.md + docs/divmod.md
numbers updated.  New driver drv/digits_layout.r8asm keeps the measurement honest
(one clb_digits call, no output, value fed as 0=2047).
2026-11-18 17:27:05 +08:00

98 lines
4.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
; calc_disp —— 桌上计算器 stage 3:打印提示符 + 十进制数字 + 换行(docs/calc.md §3 第 3 步)。
;
; 打印两遍,形状一样:`'>'`、`' '`、clb_digits 把整数换成 4 位 ASCII 数字、四个数字、
; `'\n'`。46 打出 "> 0046\n"、2047 打出 "> 2047\n"(out 里是 ASCII 码,见下)。
;
; 内存地图(clb_digits 要 REG 起 4 格、S 起 13 格,SPEC §7.7):
; cell 0 N 要打印的整数(load 装进来,被 clb_digits 吃干净)
; cell 1..4 REG clb_digits 写出的 4 个 ASCII 数字,高位在前、左边补 '0'
; cell 5..17 S clb_digits 的 13 格草稿(S+0=add_ip 的 T、S+1=余数、
; S+2..S+8=divmod 草稿、S+9=TEN、S+10=商、S+11=add_ip 的 F、
; S+12=轮数;排法见 SPEC §7.7)
; cell 18 '>' 62
; cell 19 ' ' 32
; cell 20 '\n' 10
;
; 三个常量格只造一次(load 是"先整格 erase、再 xor 常数",所以装第二遍数字不用管它们)。
;
; 两遍之间要擦 REG 和 S:clb_digits 进来要求这两块干净(SPEC §7.7)。第一遍跑完
; REG 里是 "0046"、S 里还剩 TEN/商/余数/轮数计数器(还有挪位链和 add_ip 留下的
; 草稿),不擦的话第二遍会拿脏草稿开跑。开机时内存全 0,所以第一遍不用擦。
;
; clb_digits 吃 N(跑完 N 是 0),所以每遍都靠 load N 重新装值,别指望上一遍剩的。
;
; 验证:
; build/Release/r8forge_derive.exe drv/calc_disp.r8asm 0
; out 62 32 48 48 52 54 10 62 32 50 48 52 55 10
;
; 这条纸带 25188 字符;两遍共 220496 步(r8forge_tape_run 和 derive 结果一致)。
;
; 预置 cell 的 `-1` 跑法(实测,不是推的):
; * `-1 0=7`、`-1 0=2047`:输出逐项不变 —— 程序第一件事就是 load N(整格 erase
; 再 xor 装值,第一遍是 46),预置的 cell 0 被覆盖。cell 18/19/20 同理(三个常量
; 都由 load 覆盖)。
; * `-1 1=7 2=7 3=7 4=7`(REG 全脏):也不变 —— 四轮挪位链把开机垃圾全冲出去了。
; * S 里大部分格子也不怕:S+9(cell 14,TEN 每轮重建)、S+1/S+10(余数/商由
; divmod 整格写)、S+11(cell 16,add_ip 的 F 由 clb_nonzero 先 set_0)都自清理。
; * 但 S 不是全部自清理,契约里的"干净"是真的:cell 17 = S+12 是轮数计数器,
; 只 `set_1` 不清格,预置 `17=7` 会让它多出几个 1、第一遍多跑几轮,打出
; `48 48 48 48`("0000");cell 5 = S+0 是 add_ip 的 T,预置 `5=7` 会把
; 第一遍最后一位从 '6' 变成 '7',打出 `48 48 52 55`("0047")。
; 第二遍不受影响 —— 两遍之间擦了 REG 和 S。
%include "asm/lib.r8asm"
%include "asm/digits.r8asm" ; clb_digits(顺带带进 divmod / add_ip / raw_sub / raw_or)
; Macro load
; %1: Cell operand
; %2: Count value
;
; 先把整格清干净,再 `.xor` 上常数(`.xor` 是异或,不清格就变成和旧值异或)。
%macro load 2
erase %1
.tp %1
.xor %2
%endmacro
.var N 0 ; 要打印的整数(clb_digits 吃干净)
.var REG 1 ; REG..REG+3:clb_digits 的输出,4 个 ASCII 数字
.var S 5 ; S..S+12:clb_digits 的草稿区(13 格)
.var GT 18 ; '>' = 62
.var SP 19 ; ' ' = 32
.var NL 20 ; '\n' = 10
; ---- 常量区:三格只造一次 ----
load GT 62
load SP 32
load NL 10
; ---- 第一遍:46 -> "> 0046\n" ----
load N 46
output GT
output SP
clb_digits N REG S
output REG + 0
output REG + 1
output REG + 2
output REG + 3
output NL
; ---- 第二遍之前:把 clb_digits 要的两块擦回开机状态 ----
; 第一遍把 REG 写成了 "0046"、S 写成了 TEN/商/余数/轮数计数器,不擦第二遍会用脏草稿。
%for k 4
erase REG + k
%endfor
%for k 13
erase S + k
%endfor
; ---- 第二遍:2047 -> "> 2047\n" ----
load N 2047
output GT
output SP
clb_digits N REG S
output REG + 0
output REG + 1
output REG + 2
output REG + 3
output NL