#include #include #include "instruktion.c" #include "code.c" #include "stack.c" #include "program.c" //Comment to disable debug #define DEBUG // Stack struct stack stack; #define SIZE 1000 // Program struct program program; void version(void) { printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); } 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(struct program program) { int i; int intInput; unsigned int temp; char charInput; for (i = 0; i < *program.size; ++i) { switch (program.program[i] >> 24) { case HALT: goto end; break; case PUSHC: push(stack,IMMEDIATE(program.program[i])); break; case ADD: push(stack,pop(stack)+pop(stack)); break; case SUB: temp = pop(stack); push(stack,pop(stack) - temp); break; case MUL: push(stack,pop(stack)*pop(stack)); break; case DIV: temp = pop(stack); push(stack,pop(stack)/temp); break; case MOD: temp = pop(stack); push(stack,pop(stack)%temp); break; case RDINT: scanf("%i",&intInput); push(stack,intInput); break; case WRINT: printf("%i",pop(stack)); break; case RDCHR: scanf("%c",&charInput); push(stack,charInput); break; case WRCHR: printf("%c",pop(stack)); break; } } end: return; } #ifdef DEBUG void tests(void){ printf("Runnig debug mode\n"); copyToProgram(code1,sizeof(code1)/sizeof(code1[0]),program); printProgram(program); } #endif /* ifdef DEBUG */ int main(int argc, char *argv[]) { // Initialize the Stack int size = SIZE; int current = 0; unsigned int s[SIZE]; stack.size = &size; stack.current = ¤t; stack.stack = s; // Initialize ProgrammSpeicher int psize = 1000; unsigned int p[1000]; program.size = &psize; program.program = p; #ifdef DEBUG tests(); #endif /* ifdef DEBUG */ if (argc > 1) { if (strcmp(argv[1], "--version") == 0) { version(); } else if (strcmp(argv[1], "--help") == 0) { help(); } else if (strcmp(argv[1], "--prog1") == 0) { copyToProgram(code1, sizeof(code1)/sizeof(code1[0]),program); goto run; } else if (strcmp(argv[1],"--prog2") == 0) { copyToProgram(code2, sizeof(code2)/sizeof(code2[0]),program); goto run; }else if (strcmp(argv[1],"--prog3") == 0) { copyToProgram(code3, sizeof(code3)/sizeof(code3[0]),program); goto run; }else { printf("unknown command line argument '%s', try './njvm --help'", argv[1]); } } else { run: // Started printf("Ninja Virtual Machine started\n"); printProgram(program); execute(program); // Stopped printf("Ninja Virtual Machine stopped\n"); return 0; } }