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.
21 lines
363 B
Plaintext
21 lines
363 B
Plaintext
%include "asm/raw_inc_nc.r8asm"
|
|
|
|
; clb_inc_nc 的字节级驱动。A 被吃掉,S = A + Ci。
|
|
;
|
|
; r8forge_derive drv/raw_inc_nc_byte.r8asm -2 0=255
|
|
;
|
|
; 自增就是"加一个 1",所以进位输入先置 1(宏自己不碰这一位)。
|
|
; out 依次是 A、S、Ci。
|
|
.var A 0
|
|
.var Ci 1
|
|
.var S 2
|
|
|
|
.tp Ci
|
|
set_1
|
|
|
|
clb_inc_nc A S Ci
|
|
|
|
output A
|
|
output S
|
|
output Ci
|