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.
23 lines
426 B
Plaintext
23 lines
426 B
Plaintext
%include "asm/raw_sub_nc.r8asm"
|
|
|
|
; clb_sub_nc 的字节级驱动。A、B 都被吃掉,S = A + ~B + Ci。
|
|
;
|
|
; r8forge_derive drv/raw_sub_nc_byte.r8asm -2 0=5 2=3 # ci=1:A-B
|
|
;
|
|
; A - B = A + ~B + 1,所以进位输入置 1。输出里 Ci = 1 表示没有借位(A >= B)。
|
|
; out 依次是 A、B、S、Ci。
|
|
.var A 0
|
|
.var Ci 1
|
|
.var B 2
|
|
.var S 4
|
|
|
|
.tp Ci
|
|
set_1
|
|
|
|
clb_sub_nc A B S Ci
|
|
|
|
output A
|
|
output B
|
|
output S
|
|
output Ci
|