Files
R8Forge/drv/raw_neg_nc_byte.r8asm
dsh 026ad82455 The nc macros take a real carry in, they do not seed it themselves
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.
2026-11-07 09:12:47 +08:00

22 lines
480 B
Plaintext

%include "asm/raw_neg_nc.r8asm"
; clb_neg_nc 的字节级驱动(S = ~A + Ci)。A 被吃掉。
;
; r8forge_derive drv/raw_neg_nc_byte.r8asm -2 0=1 # ci=1:取负,得 255
;
; 补码取负 = ~A + 1,所以进位输入置 1。这个 cell 是真正的进位输入,
; 置 0 的话同一个宏算的是逐位取反(~A,见 drv/raw_not_nc_byte.r8asm)。
; out 依次是 A、S、Ci。
.var A 0
.var Ci 1
.var S 2
.tp Ci
set_1
clb_neg_nc A S Ci
output A
output S
output Ci