85 Commits

Author SHA1 Message Date
a8999f4ec2 update debug 2023-12-09 22:43:53 +01:00
c0a5e694c0 update debug
fix call not jumping
2023-12-09 22:42:46 +01:00
Nils Polek
61a138fdee versin v 2023-12-09 22:40:18 +01:00
55af679c39 fix jump not working right 2023-12-09 22:10:02 +01:00
27400289f5 update debug 2023-12-09 22:00:13 +01:00
10a6883750 update debug mode 2023-12-09 21:55:12 +01:00
Nils Polek
33a2994c29 Commit with mistake 2023-12-09 21:40:42 +01:00
Nils Polek
7390df9ee0 Aufgabe 2 Fertig 2023-12-09 20:35:15 +01:00
Nils Polek
83dae77e31 aufgabe 2 stack overflow 2023-12-09 19:45:15 +01:00
nils polek
fb8497fbee changed path 2023-12-03 19:39:50 +01:00
dc16504cd7 Aufgabe 2 fertig 2023-12-03 19:30:38 +01:00
0e52973475 Aufgabe 2 fertig 2023-12-03 18:14:27 +01:00
dc33e46e24 update help 2023-12-03 14:59:02 +01:00
c16fe6349b add error message if no program selected
fix error with negative numbers
2023-12-03 14:24:22 +01:00
1363db7c11 Finished cleaning up the code 2023-10-30 12:51:42 +01:00
dfedf2e717 test 2023-10-30 11:59:56 +01:00
4d547cca6a #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "instruktion.c"
#include "code.c"
#include "stack.c"

//Comment to disable debug

// #define DEBUG

// Stack
struct stack stack;
#define SIZE 1000

unsigned int* programmSpeicher;

void copyToProgramm(unsigned int codeToCopy[]){
  programmSpeicher = codeToCopy;
}

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 printProgramm(){
  int i = 0;
  char c[10];
  while (programmSpeicher[i] != 0)
  {
    switch (programmSpeicher[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;
      default:
        strcpy(c,"halt");
      break;
    }
    IMMEDIATE(programmSpeicher[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(programmSpeicher[i])) : printf("%03i:\t%s\n",i,c);
       i++;
  }
  printf("%03i:\thalt\n",i);
}
void execute(void) {
    int i = 0;
    int intInput;
    int temp;
    char charInput;
    while (1) {

        switch (programmSpeicher[i] >> 24) {
            case HALT:
                  goto end;
                break;
            case PUSHC:
                  push(stack,IMMEDIATE(programmSpeicher[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;
        }
        i++;
    }
end:
  return;
}
#ifdef DEBUG

void tests(void){
  printf("Runnig debug mode\n");
  copyToProgramm(code1);
  printProgramm();
  execute();
}

#endif /* ifdef DEBUG */

int main(int argc, char *argv[]) {
  int size = SIZE;
  int current = 0;
  unsigned int s[SIZE];
  stack.size = &size;
  stack.current = &current;
  stack.stack = s;
    #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) {
            copyToProgramm(code1);
            goto run;
        } else if (strcmp(argv[1],"--prog2") == 0) {
            copyToProgramm(code2);
            goto run;
        }else if (strcmp(argv[1],"--prog3") == 0) {
            copyToProgramm(code3);
            goto run;
        }else {
            printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
        }
    } else {
  run:
//    Started
        printf("Ninja Virtual Machine started\n");
        printProgramm();
        execute();
//    Stopped
        printf("Ninja Virtual Machine stopped\n");
        return 0;
    }
}
2023-10-30 00:24:55 +01:00
6740f41edc stack organized 2023-10-30 00:17:17 +01:00
9890324ea5 Structurized the code 2023-10-26 22:36:54 +02:00
efe7663637 Print program 2023-10-26 17:26:59 +02:00
4e629a6ca7 orginized projekt 2023-10-26 14:49:57 +02:00
d947afdf03 Test 2023-10-26 14:03:31 +02:00
d05c415295 Now HALT works correctly 2023-10-22 13:51:32 +02:00
d23727f6da Added the 3 progamms 2023-10-21 21:31:04 +02:00
62ede37645 Operations now work 2023-10-21 21:09:01 +02:00
9f7b69e5f7 Added the option to test and basic math to execute() 2023-10-21 19:14:48 +02:00
nplk84
7d7b67f7f2 Stack overflow errors get detected correctly 2023-10-16 11:56:38 +02:00
nplk84
655da552de Added Keys 2023-10-15 19:08:12 +02:00
4f23944408 fix some warnings 2023-10-15 19:05:43 +02:00
49b064d74d remove config file
update CMakeLists.txt
update gitignore file
2023-10-15 19:00:46 +02:00
nplk84
23690576ea Added Keys 2023-10-15 18:23:32 +02:00
nplk84
8a7d900d9a Added the Stack 2023-10-12 14:42:31 +02:00
nplk84
1b742e0007 Added the Stack 2023-10-12 12:57:51 +02:00
nplk84
a64471ed41 Aufgabe 1 Beendet 2023-10-12 12:16:59 +02:00
nplk84
d5f1f95be3 Initial commit 2023-10-12 10:57:34 +02:00