Aufgabe 2 fertig

This commit is contained in:
Nils 2023-12-03 18:14:27 +01:00
parent 8b3770df80
commit 0e52973475
7 changed files with 118 additions and 32 deletions

View File

@ -5,4 +5,5 @@ set(CMAKE_C_STANDARD 99)
add_compile_options(-g -Wall -pedantic) add_compile_options(-g -Wall -pedantic)
add_executable(ninja njvm.c) add_executable(ninja njvm.c
SDA.c)

28
SDA.c Normal file
View File

@ -0,0 +1,28 @@
//
// Created by Nils on 03.12.2023
//
#ifndef SDA
#define SDA
#include <stdio.h>
struct sda {
int *size;
unsigned int *sda;
};
unsigned int getSDA(int i, struct sda s) {
return s.sda[i];
}
void setSDA(int point, int val, struct sda s) {
s.sda[point] = val;
}
void printSDA(struct sda s) {
for (int i; i < *s.size; i++) {
printf("%i", get(i, s));
}
}
#endif

View File

@ -1,10 +1,12 @@
#ifndef CODEREADER #ifndef CODEREADER
#define CODEREADER #define CODEREADER
#include "consts.c" #include "consts.c"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "program.c" #include "program.c"
void fromFile(char *path, struct program program){
unsigned int fromFile(char *path, struct program program) {
unsigned int countInstructions; unsigned int countInstructions;
unsigned int staticVars; unsigned int staticVars;
FILE *fptr; FILE *fptr;
@ -22,11 +24,14 @@ void fromFile(char *path, struct program program){
} }
if (buffer[1] != VERSION) { if (buffer[1] != VERSION) {
printf("Error: wrong version"); printf("Error: wrong version");
exit(EXIT_FAILURE);
} }
countInstructions = buffer[2]; countInstructions = buffer[2];
staticVars = buffer[3]; staticVars = buffer[3];
unsigned int instBuffer[countInstructions]; unsigned int instBuffer[countInstructions];
fread(instBuffer, 4, countInstructions, fptr); fread(instBuffer, 4, countInstructions, fptr);
copyToProgram(instBuffer, countInstructions, program); copyToProgram(instBuffer, countInstructions, program);
return staticVars;
} }
#endif /* ifdef CODEREADER */ #endif /* ifdef CODEREADER */

View File

@ -16,5 +16,11 @@
#define WRINT 8 #define WRINT 8
#define RDCHR 9 #define RDCHR 9
#define WRCHR 10 #define WRCHR 10
#define PUSHG 11
#define POPG 12
#define ASF 13
#define RSF 14
#define PUSHL 15
#define POPL 16
#endif /* ifndef INSREUKTION */ #endif /* ifndef INSREUKTION */

34
njvm.c
View File

@ -4,10 +4,12 @@
#include "code.c" #include "code.c"
#include "stack.c" #include "stack.c"
#include "program.c" #include "program.c"
#include "codeReader.c"
#include "SDA.c"
//Comment to disable debug //Comment to disable debug
//#define DEBUG #define DEBUG
// Stack // Stack
struct stack stack; struct stack stack;
@ -16,6 +18,10 @@ struct stack stack;
// Program // Program
struct program program; struct program program;
// SDA
struct sda sda;
unsigned fp;
void version(void) { void version(void) {
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
} }
@ -33,7 +39,6 @@ void execute(struct program program) {
switch (program.program[i] >> 24) { switch (program.program[i] >> 24) {
case HALT: case HALT:
goto end; goto end;
break;
case PUSHC: case PUSHC:
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i]))); push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
break; break;
@ -69,6 +74,26 @@ void execute(struct program program) {
case WRCHR: case WRCHR:
printf("%c", pop(stack)); printf("%c", pop(stack));
break; break;
case PUSHG:
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
break;
case POPG:
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
break;
case ASF:
push(stack, *stack.current);
fp = *stack.current;
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
break;
case RSF:
*stack.current = fp;
fp = pop(stack);
case PUSHL:
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
break;
case POPL:
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
break;
} }
} }
end: end:
@ -79,7 +104,10 @@ void execute(struct program program) {
void tests(void) { void tests(void) {
printf("Runnig debug mode\n"); printf("Runnig debug mode\n");
copyToProgram(code1,sizeof(code1)/sizeof(code1[0]),program); int sizeSDA = fromFile("C:\\Users\\Nilss\\CLionProjects\\njvm\\prog1.bin", program);
unsigned int s[sizeSDA];
sda.size = &sizeSDA;
sda.sda = s;
printProgram(program); printProgram(program);
} }

BIN
prog1.bin Normal file

Binary file not shown.

View File

@ -59,6 +59,24 @@ void printProgram(struct program program) {
case HALT: case HALT:
strcpy(c, "halt"); strcpy(c, "halt");
break; break;
case PUSHG :
strcpy(c, "pushg");
break;
case POPG:
strcpy(c, "popg");
break;
case ASF:
strcpy(c, "asf");
break;
case RSF:
strcpy(c, "rsf");
break;
case PUSHL:
strcpy(c, "pushl");
break;
case POPL:
strcpy(c, "popl");
break;
default: default:
strcpy(c, "ERROR"); strcpy(c, "ERROR");
break; break;