clb_digits N REG S writes '2' '0' '4' '7' into REG..REG+3 for 2047 -- ASCII
digits, most significant first, fixed width four, zero padded, which is what
was asked for after the ASCII/order/source questions.
divmod appears once, wrapped in a loop that runs four times. The first
version dispatched each digit to its own cell with four branches and a one-hot
flag handoff, and only the first branch ever fired; the digits themselves were
right (checked by printing N after every division: 204, 20, 2, 0). Rather than
keep chasing that, the dispatch is gone: each round shifts the digits already
written one cell up (high to low, so nothing gets clobbered) and the new digit
always goes to REG+0. Four rounds later REG+0..3 are thousands, hundreds, tens,
ones in that order. Simpler, two nesting levels less, four cells less, and it
works: 0/1/7/10/42/99/100/512/1000/1234/2047 all correct.
Two real traps found on the way, both now in the macro comments and SPEC:
* copy's buffer cell must be *clean*: a dirty buffer makes the copy produce
0 (measured: Q = 4, buffer = 0x7f8 -> N = 0). The library only says the
buffer is borrowed, never that it has to be zeroed.
* building a constant cannot be done as 'set a bit, rotate, set another,
rotate': the second rotation carries the first bit away. Walk the ring
once instead -- W rotations, set_1 at each target bit, then back home.
10829 characters with a minimal driver (13743 with the four outputs), 16 cells
in total, and r8forge_byte_op_test's test_digits covers all 11 values.
105 lines
2.9 KiB
Plaintext
105 lines
2.9 KiB
Plaintext
%include "asm/divmod.r8asm"
|
|
|
|
; 整数 -> 十进制字符串:把值展开成 4 个连续 cell,每格低 8 位放一个 ASCII 数字。
|
|
;
|
|
; clb_digits N REG S ; REG..REG+3 得到 "2047" 那样的 4 个字节
|
|
;
|
|
; 目标形态:**ASCII '0'..'9'、高位在前、定长 4 位、左边补 '0'**。
|
|
; 11 位 cell 装得下 0..2047,也就是 4 位十进制,所以 L 写死 4。
|
|
;
|
|
; 朴素做法是 4 份 divmod(4x4189 ≈ 17k)。这里让 **divmod 只出现一次**:外面套一个
|
|
; 跑 4 次的运行期循环,而且**不做"写哪一格"的分派** —— 每轮把已经写好的数字整体
|
|
; 往高地址挪一格,新数字永远写 REG+0:
|
|
;
|
|
; 第 1 轮: 挪(空的) ; REG+0 = 个位
|
|
; 第 2 轮: 挪(个位 -> REG+1) ; REG+0 = 十位
|
|
; ...
|
|
; 第 4 轮: 挪(三位 -> 高位) ; REG+0 = 千位
|
|
;
|
|
; 挪的顺序必须**从高到低**(REG+2 -> REG+3、REG+1 -> REG+2、REG+0 -> REG+1),
|
|
; 也就是"高位在前"的字符串,不用倒序、不用 4 个分支。
|
|
;
|
|
; (第一版是 4 个分支 + one-hot flag 分派,只跑对了第一支 —— 甩掉分派之后不仅
|
|
; 正确,还少了两层嵌套和 4 个 cell。)
|
|
;
|
|
; 三个踩过的坑:
|
|
; * **`copy` 的缓冲格必须是干净的**:缓冲带垃圾时拷贝结果是 0(实测)。
|
|
; * **`%synth` 那句老话反过来**:造常数不能"置一位转到位、再置一位转到位" ——
|
|
; 第二次旋转会把第一次放好的位一起带走;要**绕整圈走一次**。
|
|
; * 区域格、S 起的草稿格进来时必须都是干净的(0)。
|
|
;
|
|
; 前提:N = 值(结束时为 0);REG 起 4 格、S 起 8 格连续且干净。没有负数。
|
|
|
|
; Macro clb_digits
|
|
; %1: Cell N
|
|
; %2: Cell REG
|
|
; %3: Cell S
|
|
;
|
|
; N 是值(被吃干净);REG..REG+3 是输出,ASCII 数字,高位在前;S 起的 8 个连续
|
|
; cell 是草稿(S=10、S+1=商、S+2..S+5=divmod 的草稿块、S+6=轮数、S+7=拷贝缓冲),
|
|
; 进来要求干净。
|
|
%macro clb_digits 3
|
|
.anchor before_clb_digits
|
|
|
|
; ---- K = 4 个连续的 1(第 0..3 位),给 loop 用
|
|
.tp %3+6
|
|
set_1
|
|
rol
|
|
set_1
|
|
rol
|
|
set_1
|
|
rol
|
|
set_1
|
|
|
|
; ---- 四位,一轮一位
|
|
loop %3+6
|
|
; 重建 TEN = 10(位 1、3):绕整圈,在 1 和 3 各置一个 1
|
|
erase %3+0
|
|
.tp %3+0
|
|
ror
|
|
set_1
|
|
%rep 2
|
|
ror
|
|
%endrep
|
|
set_1
|
|
%rep 8
|
|
ror
|
|
%endrep
|
|
|
|
; N = 这位数字(0..9),Q = 商
|
|
clb_divmod %1 %3+0 %3+1 %3+2
|
|
|
|
; ---- 已有数字整体上移一格:从高到低搬
|
|
erase %3+7
|
|
.tp %2+2
|
|
copy %2+3 %3+7
|
|
erase %3+7
|
|
.tp %2+1
|
|
copy %2+2 %3+7
|
|
erase %3+7
|
|
.tp %2+0
|
|
copy %2+1 %3+7
|
|
|
|
; ---- REG+0 = '0' + 数字(位 4、5)
|
|
erase %2+0
|
|
.tp %2+0
|
|
%rep 4
|
|
ror
|
|
%endrep
|
|
set_1
|
|
ror
|
|
set_1
|
|
%rep 6
|
|
ror
|
|
%endrep
|
|
clb_add_ip %2+0 %1 %3+2 %3+3
|
|
|
|
; ---- 下一位的被除数(缓冲要干净)
|
|
erase %3+7
|
|
.tp %3+1
|
|
copy %1 %3+7
|
|
endloop %3+6
|
|
|
|
.tp before_clb_digits
|
|
%endmacro
|