code wiki / (root) / nx_argcap_test.nx

nx_argcap_test.nx source

↩ module page · 31 lines · 1785 B

1// nx_argcap_test.nx -- prove the raised function-arg cap (16 -> 24) end-to-end: a 20-arg call must 2// deliver EVERY argument intact (regs 0..5 + stack 6..19, including the new op16..op19 slots). If any 3// arg were dropped/misplaced the weighted sum would differ. license_tier: ORIGINAL 4import "nx_syscalls.nx" 5 6// distinct prime-ish weights so a swap/drop of ANY single arg changes the total (not just a count). 7func sum20(a0: i64, a1: i64, a2: i64, a3: i64, a4: i64, a5: i64, a6: i64, a7: i64, a8: i64, a9: i64, 8 a10: i64, a11: i64, a12: i64, a13: i64, a14: i64, a15: i64, a16: i64, a17: i64, a18: i64, a19: i64) -> i64 { 9 var s: i64 = 0 10 s = s + a0*1 + a1*2 + a2*3 + a3*4 + a4*5 11 s = s + a5*6 + a6*7 + a7*8 + a8*9 + a9*10 12 s = s + a10*11 + a11*12 + a12*13 + a13*14 + a14*15 13 s = s + a15*16 + a16*17 + a17*18 + a18*19 + a19*20 14 return s 15} 16 17func pn(v: i64) -> i64 { 18 let bb: *u8 = sys_mmap(28); var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0 19 if m == 0 { t[0] = 48 as u8; k = 1 } 20 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 21 var i: i64 = 0; while i < k { bb[i] = t[k-1-i]; i = i + 1 } sys_write(1, bb, k); return 0 22} 23func pp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 24 25func main() -> i64 { 26 // args 1..20; expected weighted sum = sum_{i=1..20} i*i = 1+4+9+...+400 = 2870. 27 let got: i64 = sum20(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) 28 pp("ARGCAP-20 weighted-sum=\x00" as *u8); pn(got); pp(" expect=2870\n\x00" as *u8) 29 if got == 2870 { pp("ARGCAP result=PASS verdict=GREEN (20-arg call delivers every arg)\n\x00" as *u8); sys_exit(0); return 0 } 30 pp("ARGCAP result=FAIL verdict=RED\n\x00" as *u8); sys_exit(1); return 1 31}