clb_digits_signed N REG S T F prints '-' (45) and then |N| as the same four zero padded digits; clb_divmod_signed N D Q R S T1 T2 F is the C style truncated division (q = trunc(n/d), r = n - q*d, remainder follows the dividend). Both are wrappers: clb_digits / clb_divmod still do the work, so the tape only grows by the sign handling -- 572 characters for the display (12151 vs 11579 in the same driver layout) and 1191 for the division (6018 vs 4827). The sign bit is read with rol + bit_copy + ror, 49 characters. clb_bit_mov is deliberately NOT used: it eats the source bit, so N comes back as N - HALF and 1024 -- whose magnitude 1024 only fits the unsigned range -- prints as 0000. A clb_sub_nc against a HALF constant was the other candidate: 765 characters for the call plus ~60 for the constant cell. A flag has to outlive the block that consumes it, because biz/rnz only exit once the body clears the condition bit. So sign(N) is moved into T2, sign(D) into F, and the remainder block flips F into sign(N)^sign(D) on its way out; a single conditional negation of the quotient then carries the xor. The negation scratch is S+6: the only cell in S with a "clean on entry" contract, and clb_neg_ip leaves it zero again for clb_divmod. Verified by r8forge_byte_op_test: all 2048 values for the display (worst 117606 steps) and 4202 division pairs -- every |a|,|b| <= 32 plus the 0/+-1/+-1023/+-1024 boundary combinations -- compared against C's / and %. examples/signed_demo.r8asm is the single point regression that runs as a program (the flagship cannot carry them: +12.1k / +5.6k on a 62351 tape).
60 lines
1.9 KiB
Plaintext
60 lines
1.9 KiB
Plaintext
%include "asm/digits.r8asm"
|
||
|
||
; out 45 48 48 48 51 45 48 48 48 49 45 49 48 50 52
|
||
;
|
||
; signed_demo —— 有符号数包装宏(SPEC §7.10)的一组用例,三段各打一行:
|
||
;
|
||
; -0003 clb_divmod_signed:-7 / 2 = -3(截断),商按二补码写成 2045
|
||
; -0001 同一个除法的余数:余数跟被除数同号
|
||
; -1024 clb_digits_signed:1024 的位型就是 -1024
|
||
;
|
||
; 第三段是最容易写错的那个:**|N| 可能放得进无符号范围、但符号位是 1**
|
||
; (-1024 的绝对值就是 1024)。所以取绝对值只能走 clb_neg_ip,不能靠
|
||
; "清掉第 W-1 位" —— 那种写法在 1024 上会打出 0。
|
||
;
|
||
; r8forge examples/signed_demo.r8asm > t.rot8
|
||
; r8forge_tape_run t.rot8 # out 45 48 48 48 51 45 48 48 48 49 45 49 48 50 52
|
||
;
|
||
; 布局:用例自己带草稿区,所以宏之间互不干扰。
|
||
.var N 0 ; clb_divmod_signed 的被除数
|
||
.var D 1 ; 除数
|
||
.var Q 2 ; 商
|
||
.var R 3 ; 余数
|
||
.var S 5 ; S..S+11:clb_divmod_signed 的 7 格草稿 + T1、T2、F
|
||
.var T1 12
|
||
.var T2 13
|
||
.var F 14
|
||
.var REG 15 ; REG..REG+3:四位数字输出
|
||
.var S2 20 ; S2..S2+12:clb_digits_signed 的 13 格草稿
|
||
.var T 33 ; 取负用的草稿格
|
||
.var F2 34 ; 符号标志格
|
||
|
||
; Macro show
|
||
; %1: Cell value
|
||
;
|
||
; 把一个值按有符号定宽四位打出来(负数先打 '-')。clb_digits_signed 进场要求
|
||
; REG、S2、T 干净、F2 的最低位数是 0,所以每次都把 REG 和 S2 整块擦一遍。
|
||
%macro show 1
|
||
%for k 4
|
||
erase REG + k
|
||
%endfor
|
||
%for k 13
|
||
erase S2 + k
|
||
%endfor
|
||
clb_digits_signed %1 REG S2 T F2
|
||
%for k 4
|
||
output REG + k
|
||
%endfor
|
||
%endmacro
|
||
|
||
; ---- 1) -7 / 2:商 -3、余数 -1 ----
|
||
load N 2041 ; -7 = 2048 - 7
|
||
load D 2
|
||
clb_divmod_signed N D Q R S T1 T2 F
|
||
show Q ; -0003
|
||
show R ; -0001
|
||
|
||
; ---- 2) 1024 这个位型 = -1024 ----
|
||
load N 1024
|
||
show N ; -1024
|