Finished cleaning up the code

This commit is contained in:
nilsplk 2023-10-30 12:51:42 +01:00
parent dfedf2e717
commit 1363db7c11
2 changed files with 66 additions and 70 deletions

28
njvm.c
View File

@ -1,6 +1,5 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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,9 +71,8 @@ void execute(void) {
printf("%c",pop(stack));
break;
}
i++;
}
end:
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;

View File

@ -3,65 +3,67 @@
*/
#ifndef PROGRAM
#define PROGRAM
#include <string.h>
#include "instruktion.c"
#include <stdio.h>
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;
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) {
for (int i = 0; i < size; ++i) {
program.program[i] = codeToCopy[i];
}
*program.size = size;
}
void printProgram(struct program program){
printf("%i\n",*program.size);
void printProgram(struct program program) {
char c[10];
for (int i = 0; i > *program.size; i++) {
for (int i = 0; i < *program.size; i++) {
switch (program.program[i] >> 24) {
case PUSHC:
strcpy(c,"pushc");
strcpy(c, "pushc");
break;
case ADD:
strcpy(c,"add");
strcpy(c, "add");
break;
case SUB:
strcpy(c,"sub");
strcpy(c, "sub");
break;
case MUL:
strcpy(c,"mul");
strcpy(c, "mul");
break;
case DIV:
strcpy(c,"div");
strcpy(c, "div");
break;
case MOD:
strcpy(c,"mod");
strcpy(c, "mod");
break;
case RDINT:
strcpy(c,"rdint");
strcpy(c, "rdint");
break;
case WRINT:
strcpy(c,"wrint");
strcpy(c, "wrint");
break;
case RDCHR:
strcpy(c,"rdchr");
strcpy(c, "rdchr");
break;
case WRCHR:
strcpy(c,"wrchr");
strcpy(c, "wrchr");
break;
case HALT:
strcpy(c,"halt");
strcpy(c, "halt");
break;
default:
strcpy(c,"ERROR");
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);
printf("afslaf");
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 */