clb_ones turns a count into that many ones, and these two macros spend those ones
one per iteration, so a tape can loop a number of times that only exists at run
time:
clb_ones N C T F
loop C
... body ...
endloop C
\loop c\ is .tp c + biz + set_0 + ror: test the low bit, spend it, rotate the
next one into place. \endloop c\ is .tp c + rnz. No .anchor is used, so the
pair nests, and endloop puts dp back on the counter whatever the body did with
it; the body must not touch c, and c is all zeros once the loop is over.
Checked with verify_loop.r8asm (939 characters): N = 0..11 makes the body run
exactly N times, A = N and C = 0 -- including N = 0, where biz skips the body
entirely. Nesting checked too: an inner loop rebuilt from a copy of its count
gives NO * MI (11 * 11 = 121, and 0 when either count is 0).
One trap found on the way, written into SPEC 5.1: in .r8asm source a loop is
spelled biz/rnz, not [ / ] -- the brackets are the tape encoding, and writing
them makes the assembler say " unknown macro [ \.
188 lines
2.8 KiB
Plaintext
188 lines
2.8 KiB
Plaintext
; Macro step
|
|
; %1: Count step_count
|
|
%macro step 1
|
|
%rep %1
|
|
stp
|
|
stp
|
|
%endrep
|
|
%endmacro
|
|
|
|
; Macro back
|
|
; %1: Count back_count
|
|
%macro back 1
|
|
%rep %1
|
|
btp
|
|
btp
|
|
%endrep
|
|
%endmacro
|
|
|
|
; Macro set_0
|
|
; %this: CellBit operand
|
|
%macro set_0 0
|
|
biz
|
|
flb
|
|
rnz
|
|
%endmacro
|
|
|
|
; Macro set_1
|
|
; %this: CellBit operand
|
|
%macro set_1 0
|
|
set_0
|
|
flb
|
|
%endmacro
|
|
|
|
; Macro erase
|
|
; %1: Cell operand
|
|
%macro erase 1
|
|
.anchor before_erase
|
|
.tp %1
|
|
%rep _CELL_SIZE
|
|
set_0
|
|
ror
|
|
%endrep
|
|
.tp before_erase
|
|
%endmacro
|
|
|
|
; Macro rol
|
|
; %this: Cell operand
|
|
%macro rol 0
|
|
%rep _CELL_SIZE - 1
|
|
ror
|
|
%endrep
|
|
%endmacro
|
|
|
|
|
|
|
|
; Macro clb_bit_mov
|
|
; %this: CellBit data
|
|
; %1: CellBit destination
|
|
|
|
%macro clb_bit_mov 1
|
|
.anchor before_clb_bit_mov
|
|
.tp %1
|
|
set_0
|
|
.tp before_clb_bit_mov
|
|
biz
|
|
.tp %1
|
|
flb
|
|
.tp before_clb_bit_mov
|
|
flb
|
|
rnz
|
|
.tp before_clb_bit_mov
|
|
%endmacro
|
|
|
|
|
|
; Macro bit_copy
|
|
; %this: CellBit data
|
|
; %1: CellBit destination
|
|
; %2: CellBit buffer
|
|
|
|
%macro bit_copy 2
|
|
.anchor before_bit_copy
|
|
|
|
.tp %1
|
|
set_0
|
|
.tp %2
|
|
set_0
|
|
.tp before_bit_copy
|
|
biz
|
|
.tp %1
|
|
flb
|
|
.tp %2
|
|
flb
|
|
.tp before_bit_copy
|
|
flb
|
|
rnz
|
|
.tp %2
|
|
clb_bit_mov before_bit_copy
|
|
|
|
.tp before_bit_copy
|
|
%endmacro
|
|
|
|
|
|
|
|
; Macro copy
|
|
; %this: CellBit data
|
|
; %1: CellBit destination
|
|
; %2: CellBit buffer
|
|
|
|
%macro copy 2
|
|
.anchor before_copy
|
|
%rep _CELL_SIZE
|
|
bit_copy %1 %2
|
|
ror
|
|
.tp %1
|
|
ror
|
|
.tp %2
|
|
ror
|
|
.tp before_copy
|
|
%endrep
|
|
%endmacro
|
|
|
|
|
|
; Macro bit_copy_to_buffer
|
|
; %this: UnitBit operand
|
|
; %1: CellBit tmp
|
|
%macro bit_copy_to_buffer 1
|
|
stp
|
|
.anchor bit_copy_to_buffer_buf_1
|
|
btp
|
|
bit_copy bit_copy_to_buffer_buf_1 %1
|
|
%endmacro
|
|
|
|
|
|
; Macro copy_to_buffer
|
|
; %this: Unit operand
|
|
; %1: Cell tmp
|
|
|
|
%macro copy_to_buffer 1
|
|
stp
|
|
.anchor copy_to_buffer_buf_1
|
|
btp
|
|
copy copy_to_buffer_buf_1 %1
|
|
%endmacro
|
|
|
|
; Macro output
|
|
; %1: Cell operand
|
|
|
|
%macro output 1
|
|
.anchor before_output
|
|
.tp %1
|
|
out
|
|
.tp before_output
|
|
%endmacro
|
|
|
|
; Macro loop
|
|
; %1: Cell C
|
|
;
|
|
; 动态循环的开口:按 C 里 1 的个数跑,每轮吃掉最低位的一个 1。
|
|
; C 一般由 clb_ones 从"次数"算出来 —— 这样循环次数就是**运行期**的值,
|
|
; 不再是汇编期的常数:
|
|
;
|
|
; clb_ones N C T F ; C = N 个 1,N 被吃成 0
|
|
; loop C
|
|
; ... body ...
|
|
; endloop C
|
|
;
|
|
; 两条约定:
|
|
; * 不开 anchor(所以能嵌套),`endloop` 之后 dp 停在 C 上;
|
|
; * 循环体不许碰 C。循环跑完 C 是 0 —— "配额"正好用完。
|
|
;
|
|
; 为什么循环体不能省掉那个 set_0: `biz`/`rnz` 判的是最低位,body 里不把它吃掉,
|
|
; `rnz` 看到还是 1 就跳回去,循环永远不结束。吃掉一格 = 用掉一轮配额。
|
|
%macro loop 1
|
|
.tp %1
|
|
biz
|
|
set_0
|
|
ror
|
|
%endmacro
|
|
|
|
; Macro endloop
|
|
; %1: Cell C
|
|
;
|
|
; loop 的收口。`rnz` 判的还是 C 的最低位,所以先把 dp 指回 C。
|
|
%macro endloop 1
|
|
.tp %1
|
|
rnz
|
|
%endmacro
|