Files
R8Forge/asm/puts_dyn.r8asm
dsh a1f88e3d25 asm: raw_puts_dyn -- print a buffer whose length is only known at run time
A terminal has to echo the line the user just typed, and that length is a cell, not
a constant; clb_puts cannot do it because .tp takes a literal address.  So this one
walks MAX compile time slots and decides per slot whether to print: CNT starts as a
copy of LEN, clb_nonzero turns "CNT != 0" into the flag F, and the print happens
inside `biz F` with the countdown *inside* the guarded body as well.

That last detail is the whole bug this round: with clb_dec_ip outside the guard, a
CNT of 0 wrapped to 2047 on the first skipped cell and the rest of the buffer came
out (LEN=0 printed E F G H).  The fix is also why the driver covers LEN = 3, 0 and
8 -- the zero case is what caught it.

  drv/puts_dyn.r8asm   13376 chars, out 65 66 67 65 66 67 68 69 70 71 72 (ABC, nothing, ABCDEFGH)
  reference machine    "ABCABCDEFGH"     6306 steps

Also learnt: `copy` takes two arguments (%this is the source), and it wants a clean
buffer -- the first version passed three and then reused a dirty T, which showed up
as a countdown that started from the wrong value.

Not in asm/test.r8asm: an 8 slot version costs thousands of characters and the
flagship is about a thousand under the 65536 limit.  SPEC 5.3 records the contract
and that exception; the macro count is 53.
2026-11-17 20:11:01 +08:00

34 lines
806 B
Plaintext

%include "asm/lib.r8asm"
%include "asm/add_ip.r8asm"
%include "asm/dec_ip.r8asm"
; Macro raw_puts_dyn
; %1: Cell REG
; %2: Count MAX
; %3: Cell LEN
; %4: Cell CNT
; %5: Cell F
; %6: Cell T
;
; 打印 REG 起最多 MAX 格里的前 LEN 格,LEN 是**运行期**的值(把一段缓冲原样打出来)。
; LEN 保持原样;CNT 当倒计数(结束时 0,LEN > MAX 时剩 LEN-MAX);F 是每格的条件标志;
; T 兼作 copy 的缓冲和 nonzero/dec_ip 的草稿,进来必须干净。MAX 仍是汇编期常数 ——
; 打印哪一格要写死 `.tp`,能变的只有"打不打"。
%macro raw_puts_dyn 6
.tp %6
erase %6
.tp %3
copy %4 %6
%for k %2
clb_nonzero %4 %5 %6
.tp %5
biz
output %1+k
clb_dec_ip %4 %6
.tp %5
flb
rnz
%endfor
%endmacro