update debug mode

This commit is contained in:
Elias Bennour 2023-12-09 21:55:12 +01:00
parent 33a2994c29
commit 10a6883750
3 changed files with 87 additions and 66 deletions

141
njvm.c
View File

@ -7,8 +7,8 @@
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c" #include "SDA.c"
// Debug // Debug
int debug = 1; int debug = 0;
// Stack // Stack
struct stack stack; struct stack stack;
@ -37,124 +37,128 @@ void execute(struct program program) {
for (i = 0; i < *program.size; ++i) { for (i = 0; i < *program.size; ++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");
goto end; goto end;
case PUSHC: case PUSHC:
if(debug == 1) printf("pushc\n"); if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i]))); push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
break; break;
case ADD: case ADD:
if(debug == 1) printf("add\n"); if (debug == 1) printf("add: %i + %i\n", peek(stack, 2), peek(stack, 1));
push(stack, pop(stack) + pop(stack)); push(stack, pop(stack) + pop(stack));
break; break;
case SUB: case SUB:
if(debug == 1) printf("sub\n"); if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
push(stack, pop(stack) - temp); push(stack, pop(stack) - temp);
break; break;
case MUL: case MUL:
if(debug == 1) printf("mul\n"); if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
push(stack, pop(stack) * pop(stack)); push(stack, pop(stack) * pop(stack));
break; break;
case DIV: case DIV:
if(debug == 1) printf("div\n"); if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
push(stack, pop(stack) / temp); push(stack, pop(stack) / temp);
break; break;
case MOD: case MOD:
if(debug == 1) printf("mod\n"); if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
push(stack, pop(stack) % temp); push(stack, pop(stack) % temp);
break; break;
case RDINT: case RDINT:
if(debug == 1) printf("rdint\n"); if (debug == 1) printf("rdint\n");
scanf("%i", &intInput); scanf("%i", &intInput);
push(stack, intInput); push(stack, intInput);
if (debug == 1) printf("pushed %i\n", intInput);
break; break;
case WRINT: case WRINT:
if(debug == 1) printf("wrint\n"); if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
printf("%i", pop(stack)); printf("%i", pop(stack));
break; break;
case RDCHR: case RDCHR:
if(debug == 1) printf("rdchr\n"); if (debug == 1) printf("rdchr\n");
scanf("%c", &charInput); scanf("%c", &charInput);
push(stack, charInput); push(stack, charInput);
if (debug == 1) printf("pushed %c\n", charInput);
break; break;
case WRCHR: case WRCHR:
if(debug == 1) printf("wrchr\n"); if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
printf("%c", pop(stack)); printf("%c", pop(stack));
break; break;
case PUSHG: case PUSHG:
if(debug == 1) printf("pushg\n"); if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)); push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
break; break;
case POPG: case POPG:
if(debug == 1) printf("popg\n"); if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda); setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
break; break;
case ASF: case ASF:
if(debug == 1) printf("asf\n"); if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, *stack.current); push(stack, *stack.current);
fp = *stack.current - 1; fp = *stack.current;
*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) printf("rsf\n"); if (debug == 1) printf("rsf\n");
*stack.current = fp + 2; *stack.current = fp + 2;
fp = pop(stack); fp = pop(stack);
case POPL: case POPL:
if(debug == 1) printf("popl\n"); if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack); stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
break; break;
case PUSHL: case PUSHL:
if(debug == 1) printf("pushl\n"); if (debug == 1) printf("pushl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
break; break;
case NE: case NE:
if(debug == 1) printf("ne\n"); if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1));
if (pop(stack) != pop(stack)) push(stack, 1); if (pop(stack) != pop(stack)) push(stack, 1);
else push(stack, 0); else push(stack, 0);
break; break;
case EQ: case EQ:
if(debug == 1) printf("eq\n"); if (debug == 1) printf("eq: %i == %i\n", peek(stack, 2), peek(stack, 1));
if (pop(stack) == pop(stack)) push(stack, 1); if (pop(stack) == pop(stack)) push(stack, 1);
else push(stack, 0); else push(stack, 0);
break; break;
case LT: case LT:
if(debug == 1) printf("lt\n"); if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
if (pop(stack) < temp) push(stack, 1); if (pop(stack) < temp) push(stack, 1);
else push(stack, 0); else push(stack, 0);
break; break;
case LE: case LE:
if(debug == 1) printf("le\n"); if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
if (pop(stack) <= temp) push(stack, 1); if (pop(stack) <= temp) push(stack, 1);
else push(stack, 0); else push(stack, 0);
break; break;
case GT: case GT:
if(debug == 1) printf("gt\n"); if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
if (pop(stack) > temp) push(stack, 1); if (pop(stack) > temp) push(stack, 1);
else push(stack, 0); else push(stack, 0);
break; break;
case GE: case GE:
if(debug == 1) printf("ge\n"); if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack); temp = pop(stack);
if (pop(stack) >= temp) push(stack, 1); if (pop(stack) >= temp) push(stack, 1);
else push(stack, 0); else push(stack, 0);
break; break;
case BRF: case BRF:
if(debug == 1) printf("brf\n"); if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
if(pop(stack)==0) if (debug == 1) printf("pop: %i\n", peek(stack, 1));
i = SIGN_EXTEND(IMMEDIATE(program.program[i])); if (pop(stack) == 0)
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
break; break;
case BRT: case BRT:
if(debug == 1) printf("brt\n"); if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
if(pop(stack)==1) if (debug == 1) printf("pop: %i\n", peek(stack, 1));
i = SIGN_EXTEND(IMMEDIATE(program.program[i])); if (pop(stack) == 1)
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
case JMP: case JMP:
if(debug == 1) printf("jmp\n"); if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
i = SIGN_EXTEND(IMMEDIATE(program.program[i])); i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
break; break;
} }
@ -184,29 +188,38 @@ int main(int argc, char *argv[]) {
program.program = p; program.program = p;
program.saveProgram = &saveProgram; program.saveProgram = &saveProgram;
if (argc > 2) { int run = 0;
if (strcmp(argv[2], "--debug")) { int sizeSDA;
debug = 1;
}
}
if (debug){
tests();
}
if (argc > 1) { if (argc > 1) {
if (strcmp(argv[1], "--version") == 0) { for (int i = 1; i < argc; ++i) {
version(); if (strcmp(argv[i], "--debug") == 0) {
} else if (strcmp(argv[1], "--help") == 0) { debug = 1;
help(); } else if (strcmp(argv[i], "--version") == 0) {
} else { version();
int temp = fromFile(argv[1], program); return 0;
int sizeSDA = temp; } else if (strcmp(argv[i], "--help") == 0) {
unsigned int s[sizeSDA]; help();
sda.size = &temp; return 0;
sda.sda = s; } else {
execute(program); sizeSDA = fromFile(argv[i], program);
run = 1;
}
} }
}
if (debug) {
tests();
}
if (run) {
unsigned int s[sizeSDA];
sda.size = &sizeSDA;
sda.sda = s;
if (debug == 1) printProgram(program);
execute(program);
} else { } else {
return 0; printf("Error: no code file specified\n");
return 1;
} }
} }

View File

@ -38,4 +38,12 @@ unsigned int pop(struct stack s) {
*s.current = *s.current -1; *s.current = *s.current -1;
return s.stack[*s.current]; return s.stack[*s.current];
} }
unsigned int peek(struct stack s, int steps) {
if (*s.current - steps < 0) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
return s.stack[*s.current - steps];
}
#endif #endif