update debug

This commit is contained in:
Elias Bennour 2023-12-09 23:03:32 +01:00
parent 829735eb8c
commit b87ebbe841

14
njvm.c
View File

@ -35,6 +35,10 @@ void execute(struct program program) {
unsigned int temp; unsigned int temp;
char charInput; char charInput;
for (i = 0; i < *program.size; ++i) { for (i = 0; i < *program.size; ++i) {
if (debug == 1) printf("=== DEBUG: Stack before instruction %i ===\n", i);
if (debug == 1) printStack(stack, fp);
if (debug == 1) printf("=== DEBUG: Instruction %i ===\n", i);
switch (program.program[i] >> 24) { switch (program.program[i] >> 24) {
case HALT: case HALT:
if (debug == 1) printf("halt\n"); if (debug == 1) printf("halt\n");
@ -101,11 +105,9 @@ void execute(struct program program) {
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i])); *stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
break; break;
case RSF: case RSF:
if (debug == 1) printStack(stack, fp);
if (debug == 1) printf("rsf\n"); if (debug == 1) printf("rsf\n");
*stack.current = fp + 2; *stack.current = fp + 2;
fp = pop(stack); fp = pop(stack);
if (debug == 1) printStack(stack, fp);
break; break;
case POPL: case POPL:
if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
@ -164,29 +166,29 @@ void execute(struct program program) {
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1; i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
if (debug == 1) printf("new i: %i\n", i); if (debug == 1) printf("new i: %i\n", i);
} }
break;
case JMP: case JMP:
if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1; i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
if (debug == 1) printf("new i: %i\n", i); if (debug == 1) printf("new i: %i\n", i);
break; break;
case CALL: case CALL:
if (debug == 1) printStack(stack, fp);
if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i]))); if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, i + 1); push(stack, i + 1);
if (debug == 1) printf("push: %i\n", i + 1); if (debug == 1) printf("push: %i\n", i + 1);
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1; i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
if (debug == 1) printf("new i: %i\n", i); if (debug == 1) printf("new i: %i\n", i);
if (debug == 1) printStack(stack, fp);
break; break;
case RET: case RET:
if (debug == 1) printStack(stack, fp);
if (debug == 1) printf("ret\n"); if (debug == 1) printf("ret\n");
if (debug == 1) printf("pop: %i\n", peek(stack, 1)); if (debug == 1) printf("pop: %i\n", peek(stack, 1));
i = pop(stack); i = pop(stack);
if (debug == 1) printf("new i: %i\n", i); if (debug == 1) printf("new i: %i\n", i);
if (debug == 1) printStack(stack, fp);
break; break;
} }
if (debug == 1) printf("=== DEBUG: Stack after instruction %i ===\n", i);
if (debug == 1) printStack(stack, fp);
if (debug == 1) printf("=== DEBUG: Instruction %i ===\n", i);
} }
end: end:
return; return;