They used to write the carry cell themselves -- set_0 inside clb_add_nc, set_1
inside clb_inc_nc / clb_neg_nc / clb_sub_nc -- which made the parameter a lie: a
cell the caller passes and the callee overwrites is not a carry input, it is a
scratch cell wearing a carry's name. It also ruled out the thing a carry is for:
multi byte arithmetic. The carry out of the low byte could not be handed to the
high byte, because the high byte's call would zero (or one) it first.
Now the macros only read it, and the caller decides:
clb_add_nc A B S Ci S = A + B + Ci Ci = 0 plain add, or chained
clb_inc_nc A S Ci S = A + Ci Ci = 1 gives A + 1
clb_neg_nc A S Ci S = ~A + Ci Ci = 1 gives -A, Ci = 0 gives ~A
clb_sub_nc A B S Ci S = A + ~B + Ci Ci = 1 gives A - B, Ci = 0 A-B-1
which also turns the negate into two operations for free: two's complement
negation at Ci=1 and the ones' complement (~A) at Ci=0. The second one has its
own driver and test now (drv/raw_not_nc_byte.r8asm).
The drivers set the bit explicitly before the call, which is where that
instruction belongs -- the tapes end up the same length as before (688/430/468/725
instead of 693/433/471/730: one setup instruction moved out of the macro).
All nine byte level operations still pass, and the docs (AGENTS.md, README.md and
the four file headers) now state the carry in contract instead of describing a
constant.
25 lines
545 B
Plaintext
25 lines
545 B
Plaintext
%include "asm/raw_add_nc.r8asm"
|
|
|
|
; clb_add_nc 的字节级驱动:操作数直接放在 A、B 里。
|
|
;
|
|
; r8forge_derive drv/raw_add_nc_byte.r8asm -2 0=5 2=3
|
|
;
|
|
; out 依次是 A、B、S、Ci。和 raw_add 的驱动不同,A、B 是被吃掉的(结束为 0)。
|
|
;
|
|
; 进位 cell 是真正的进位输入,这里先清零 = 普通加法。要多字节串接就把上一字节的
|
|
; 进位输出接在这个 cell 上,别再清它。
|
|
.var A 0
|
|
.var Ci 1
|
|
.var B 2
|
|
.var S 4
|
|
|
|
.tp Ci
|
|
set_0
|
|
|
|
clb_add_nc A B S Ci
|
|
|
|
output A
|
|
output B
|
|
output S
|
|
output Ci
|