wip recursive test

This commit is contained in:
zongor 2026-01-07 18:42:16 -08:00
parent 28f0c7b85b
commit 2334212952
1 changed files with 24 additions and 8 deletions

View File

@ -33,18 +33,34 @@ u32 syscall(u32 id, u32 args, u32 mem_ptr) {
i32 main() { i32 main() {
init_vm(); init_vm();
// hardcoded add 2 numbers and print debug code[cp++] = ENCODE_B(OP_LOAD_IMM, 0, 35);
int a = mp; mp+=4; code[cp++] = ENCODE_B(OP_PUSH, 0, 0);
int b = mp; mp+=4; int fib = cp + 3;
int c = mp; mp+=4; code[cp++] = ENCODE_A(OP_CALL, fib, 7, 1);
code[cp++] = ENCODE_B(OP_LOAD_IMM, a, 1); code[cp++] = ENCODE_A(OP_SYSCALL, SYSCALL_DBG_PRINT, 1, 0);
code[cp++] = ENCODE_B(OP_LOAD_IMM, b, 2);
code[cp++] = ENCODE_A(OP_ADD_INT, c, b, a);
code[cp++] = ENCODE_A(OP_SYSCALL, SYSCALL_DBG_PRINT, 1, c);
code[cp++] = ENCODE_A(OP_HALT, 0, 0, 0); code[cp++] = ENCODE_A(OP_HALT, 0, 0, 0);
int base_case_if_false = cp + 2;
code[cp++] = ENCODE_B(OP_LOAD_IMM, 1, 2);
code[cp++] = ENCODE_A(OP_JLT_INT, base_case_if_false, 0, 1);
code[cp++] = ENCODE_B(OP_RETURN, 0, 0);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 3, 2);
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
code[cp++] = ENCODE_B(OP_PUSH, 4, 0);
code[cp++] = ENCODE_A(OP_CALL, fib, 7, 5);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 3, 1);
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
code[cp++] = ENCODE_B(OP_PUSH, 4, 0);
code[cp++] = ENCODE_A(OP_CALL, fib, 7, 6);
code[cp++] = ENCODE_A(OP_ADD_INT, 7, 6, 5);
code[cp++] = ENCODE_B(OP_RETURN, 7, 0);
while(step_vm()) { while(step_vm()) {
// do stuff // do stuff
} }
return 0; return 0;
} }