From 9890324ea56142046b3b787d5eba8aea94d66f40 Mon Sep 17 00:00:00 2001 From: nilsplk Date: Thu, 26 Oct 2023 22:36:54 +0200 Subject: [PATCH] Structurized the code --- .gitignore | 3 ++- code.c | 2 +- instruktion.c | 2 ++ njvm.c | 39 ++++++++++++++++++++++----------------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 670b3fc..339d536 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ cmake-build-debug njvm njvm.dSYM -njvm.exe \ No newline at end of file +njvm.exe +njvm2 diff --git a/code.c b/code.c index 537184b..ba90001 100644 --- a/code.c +++ b/code.c @@ -18,7 +18,7 @@ unsigned int code1[] = { (HALT) }; unsigned int code2[] = { - (PUSHC << 24) | IMMEDIATE(-2), + (PUSHC << 24) | IMMEDIATE(SIGN_EXTEND(-2)), (RDINT << 24), (MUL << 24), (PUSHC << 24) | IMMEDIATE(3), diff --git a/instruktion.c b/instruktion.c index f679004..2a61dee 100644 --- a/instruktion.c +++ b/instruktion.c @@ -2,6 +2,8 @@ #define IMMEDIATE(x) ((x) & 0x00FFFFFF) +#define SIGN_EXTEND(i) ((i) & 0x00800000 ? (i) | 0xFF000000 : (i)) + #define HALT 0 #define PUSHC 1 #define ADD 2 diff --git a/njvm.c b/njvm.c index 111ed01..bb18621 100644 --- a/njvm.c +++ b/njvm.c @@ -11,7 +11,7 @@ //Comment to disable debug -#define DEBUG +// #define DEBUG unsigned int* programmSpeicher; @@ -49,18 +49,6 @@ void version(void) { 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 useption(int argc, char *argv[]) { - if (argc > 1) { - if (strcmp(argv[1], "--version") == 0) { - version(); - } else if (strcmp(argv[1], "--help") == 0) { - help(); - } else { - printf("unknown command line argument '%s', try './njvm --help'", argv[1]); - } - } -} void printProgramm(){ int i = 0; char c[10]; @@ -182,12 +170,29 @@ int main(int argc, char *argv[]) { #ifdef DEBUG tests(); #endif /* ifdef DEBUG */ - if (argc > 1) useption(argc, argv); - else { - + 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;