diff --git a/njvm.c b/njvm.c index 6c7995c..d501000 100644 --- a/njvm.c +++ b/njvm.c @@ -1,6 +1,5 @@ #include #include -#include #include "instruktion.c" #include "code.c" #include "stack.c" @@ -11,16 +10,13 @@ #define DEBUG - // Stack struct stack stack; #define SIZE 1000 -// ProgrammSpeicher +// Program struct program program; -unsigned int* programmSpeicher; - void version(void) { printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); } @@ -29,19 +25,18 @@ void help(void) { printf("usage: ./njvm [option] [option] ...\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n"); } -void execute(void) { - int i = 0; +void execute(struct program program) { + int i; int intInput; - int temp; + unsigned int temp; char charInput; - while (1) { - - switch (programmSpeicher[i] >> 24) { + for (i = 0; i < *program.size; ++i) { + switch (program.program[i] >> 24) { case HALT: goto end; break; case PUSHC: - push(stack,IMMEDIATE(programmSpeicher[i])); + push(stack,IMMEDIATE(program.program[i])); break; case ADD: push(stack,pop(stack)+pop(stack)); @@ -76,10 +71,9 @@ void execute(void) { printf("%c",pop(stack)); break; } - i++; } -end: - return; + end: + return; } #ifdef DEBUG @@ -101,8 +95,8 @@ int main(int argc, char *argv[]) { stack.stack = s; // Initialize ProgrammSpeicher - int psize = 1; - unsigned int p[1]; + int psize = 1000; + unsigned int p[1000]; program.size = &psize; program.program = p; @@ -132,7 +126,7 @@ int main(int argc, char *argv[]) { // Started printf("Ninja Virtual Machine started\n"); printProgram(program); - execute(); + execute(program); // Stopped printf("Ninja Virtual Machine stopped\n"); return 0; diff --git a/program.c b/program.c index 34b5c9f..0eddcff 100644 --- a/program.c +++ b/program.c @@ -3,65 +3,67 @@ */ #ifndef PROGRAM #define PROGRAM + #include #include "instruktion.c" #include struct program { - int* size; - unsigned int* program; + int *size; + unsigned int *program; }; -void copyToProgram(unsigned int codeToCopy[], int size, struct program program){ - program.program = codeToCopy; - *program.size = size; -} - -void printProgram(struct program program){ - printf("%i\n",*program.size); - char c[10]; - for (int i = 0; i > *program.size; i++) { - switch (program.program[i] >> 24) { - case PUSHC: - strcpy(c,"pushc"); - break; - case ADD: - strcpy(c,"add"); - break; - case SUB: - strcpy(c,"sub"); - break; - case MUL: - strcpy(c,"mul"); - break; - case DIV: - strcpy(c,"div"); - break; - case MOD: - strcpy(c,"mod"); - break; - case RDINT: - strcpy(c,"rdint"); - break; - case WRINT: - strcpy(c,"wrint"); - break; - case RDCHR: - strcpy(c,"rdchr"); - break; - case WRCHR: - strcpy(c,"wrchr"); - break; - case HALT: - strcpy(c,"halt"); - break; - default: - strcpy(c,"ERROR"); - break; - +void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) { + for (int i = 0; i < size; ++i) { + program.program[i] = codeToCopy[i]; } - IMMEDIATE(program.program[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(program.program[i])) : printf("%03i:\t%s\n",i,c); - printf("afslaf"); - } + *program.size = size; } + +void printProgram(struct program program) { + char c[10]; + for (int i = 0; i < *program.size; i++) { + switch (program.program[i] >> 24) { + case PUSHC: + strcpy(c, "pushc"); + break; + case ADD: + strcpy(c, "add"); + break; + case SUB: + strcpy(c, "sub"); + break; + case MUL: + strcpy(c, "mul"); + break; + case DIV: + strcpy(c, "div"); + break; + case MOD: + strcpy(c, "mod"); + break; + case RDINT: + strcpy(c, "rdint"); + break; + case WRINT: + strcpy(c, "wrint"); + break; + case RDCHR: + strcpy(c, "rdchr"); + break; + case WRCHR: + strcpy(c, "wrchr"); + break; + case HALT: + strcpy(c, "halt"); + break; + default: + strcpy(c, "ERROR"); + break; + } + IMMEDIATE(program.program[i]) ? printf("%03i:\t%s\t%i\n", i, c, IMMEDIATE(program.program[i])) : printf( + "%03i:\t%s\n", i, c); + } +} + #endif /* ifndef PROGRAMM */