calc: stage 3 verified -- prompt, four digits, newline

drv/calc_disp.r8asm prints `> ` plus a number in decimal, twice (46 and 2047), so
the display path of the calculator is proven end to end:

    out 62 32 48 48 52 54 10 62 32 50 48 52 55 10
        >  sp  0  0  4  6  LF    >  sp  2  0  4  7  LF

tape 58382 characters, 564578 steps (two clb_digits calls dominate), tape_run and
derive agree.  This one fits under ROT-8-viz's 65536, unlike the two %rep 16/6
drivers.

The probe also pinned down which scratch really is sensitive: with cell 0 or the
four REG cells preset the output does not change (load erases, and the digit
shift chain flushes REG), but cell S+10 (the round counter, only set_1, never
cleared) and S+11 (clb_add_ip scratch) do change the first print -- "0047" and
"0000" respectively.  So "the caller must hand in clean scratch" is real, and the
two prints need the erase between them.
This commit is contained in:
dsh committed 2026-11-11 10:59:12 +08:00
1 parent b873d71082
commit bb1a434586
1 file changed
+96
+96
View File
@@ -0,0 +1,96 @@
; 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=TEN、S+1=商、S+2=余数、
; S+3..S+9=divmod 草稿、S+10=轮数、S+11/S+12=add_ip 草稿)
; cell 18 '>' 62
; cell 19 ' ' 32
; cell 20 '\n' 10
;
; 三个常量格只造一次(load 是"先整格 erase、再 xor 常数",所以装第二遍数字不用管它们)。
;
; 两遍之间要擦 REG 和 S:clb_digits 进来要求这两块干净(SPEC §7.7)。第一遍跑完
; REG 里是 "0046"、S 里还剩 TEN/商/余数/轮数计数器(S+0 甚至被挪位链写进了第一位数字),
; 不擦的话第二遍会拿脏草稿开跑。开机时内存全 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
;
; 这条纸带 58382 字符;一次 564578 步(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+0(cell 5,TEN 每轮重建)、S+1/S+2(商/余数由 divmod
; 整格写)、S+12(cell 17)都自清理。
; * 但 S 不是全部自清理,契约里的"干净"是真的:cell 15 = S+10 是轮数计数器,
; 只 `set_1` 不清格,预置 `15=7` 会让它多出几个 1、第一遍多跑几轮,打出
; `48 48 48 48`("0000");cell 16 = S+11 是 add_ip 的草稿,预置 `16=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