move programs to a separate folder

fix pushg, popg, pushl and popl
This commit is contained in:
Elias Bennour 2023-12-04 02:39:23 +01:00
parent 8a89c6bf70
commit 6ea2a7e735
15 changed files with 143 additions and 96 deletions

17
SDA.c
View File

@ -6,21 +6,22 @@
#include <stdio.h> #include <stdio.h>
struct sda { struct sda {
int *size;
unsigned int *sda; unsigned int *sda;
int size;
}; };
unsigned int getSDA(int i, struct sda s) { int getSDA(unsigned int offset, struct sda *sda) {
return s.sda[i]; return sda->sda[offset];
} }
void setSDA(int point, int val, struct sda s) { void setSDA(unsigned int offset, int value, struct sda *sda) {
s.sda[point] = val; sda->sda[offset] = value;
} }
void printSDA(struct sda s) { void printSDA(struct sda *sda) {
for (int i = 0; i < *s.size; i++) { printf("SDA:\n");
printf("%i\n", getSDA(i, s)); for (int i = 0; i < sda->size; ++i) {
printf("[%d] = %d\n", i, sda->sda[i]);
} }
} }

View File

@ -6,7 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "program.c" #include "program.c"
unsigned int 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;

132
njvm.c
View File

@ -7,15 +7,10 @@
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c" #include "SDA.c"
// Stack
struct stack stack;
#define SIZE 1000
// Program // Program
struct program program; struct program *program;
// SDA // SDA
struct sda sda;
unsigned fp; unsigned fp;
void version(void) { void version(void) {
@ -26,104 +21,129 @@ void help(void) {
printf("Usage: ./njvm [options] <code file>\n\t--debug\tstart virtual machine in debug mode\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n"); printf("Usage: ./njvm [options] <code file>\n\t--debug\tstart virtual machine in debug mode\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n");
} }
void execute(struct program program) { void execute(struct program *program, struct sda *sda) {
int i; struct stack stack;
stack.size = 1000;
stack.currentFrame = 0;
for (int i = 0; i < stack.size; ++i) {
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
frame->fp = malloc(sizeof(unsigned int) * stack.size);
frame->sp = malloc(sizeof(unsigned int) * stack.size);
frame->bp = NULL;
stack.frames[i] = frame;
}
struct stackFrame *currentFrame = NULL;
unsigned int tmp;
int intInput; int intInput;
unsigned int temp;
char charInput; char charInput;
for (i = 0; i < *program.size; ++i) {
switch (program.program[i] >> 24) { for (int i = 0; i < program->size; ++i) {
unsigned int instruction = program->program[i];
switch (instruction >> 24) {
case HALT: case HALT:
goto end; goto end;
case PUSHC: case PUSHC:
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i]))); push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
break; break;
case ADD: case ADD:
push(stack, pop(stack) + pop(stack)); push(&stack, pop(&stack) + pop(&stack));
break; break;
case SUB: case SUB:
temp = pop(stack); tmp = pop(&stack);
push(stack, pop(stack) - temp); push(&stack, pop(&stack) - tmp);
break; break;
case MUL: case MUL:
push(stack, pop(stack) * pop(stack)); push(&stack, pop(&stack) * pop(&stack));
break; break;
case DIV: case DIV:
temp = pop(stack); tmp = pop(&stack);
push(stack, pop(stack) / temp); push(&stack, pop(&stack) / tmp);
break; break;
case MOD: case MOD:
temp = pop(stack); tmp = pop(&stack);
push(stack, pop(stack) % temp); push(&stack, pop(&stack) % tmp);
break; break;
case RDINT: case RDINT:
scanf("%i", &intInput); scanf("%i", &intInput);
push(stack, intInput); push(&stack, intInput);
break; break;
case WRINT: case WRINT:
printf("%i", pop(stack)); printf("%i", pop(&stack));
break; break;
case RDCHR: case RDCHR:
scanf("%c", &charInput); scanf("%c", &charInput);
push(stack, charInput); push(&stack, charInput);
break; break;
case WRCHR: case WRCHR:
printf("%c", pop(stack)); printf("%c", pop(&stack));
break; break;
case PUSHG: case PUSHG:
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)); currentFrame = stack.frames[stack.currentFrame];
currentFrame->bp = currentFrame->sp;
*currentFrame->sp++ = getSDA(SIGN_EXTEND(IMMEDIATE(instruction)), sda);
break; break;
case POPG: case POPG:
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda); currentFrame = stack.frames[stack.currentFrame];
currentFrame->bp = currentFrame->sp;
setSDA(SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack), sda);
break; break;
case ASF: case ASF:
push(stack, *stack.current); currentFrame = stack.frames[stack.currentFrame];
fp = *stack.current; push(&stack, *currentFrame->sp);
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i])); *currentFrame->sp = stack.currentFrame;
currentFrame->bp = currentFrame->sp;
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
break; break;
case RSF: case RSF:
*stack.current = fp; stack.currentFrame = pop(&stack);
fp = pop(stack); currentFrame = stack.frames[stack.currentFrame];
currentFrame->bp = NULL;
break;
case PUSHL: case PUSHL:
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack); currentFrame = stack.frames[stack.currentFrame];
push(&stack, currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))]);
break; break;
case POPL: case POPL:
push(&stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); currentFrame = stack.frames[stack.currentFrame];
currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))] = pop(&stack);
break; break;
} }
} }
end: end:
return; return;
} }
// run prog2.bin
void tests(void) { void tests(void) {
printf("Runnig debug mode\n"); printf("Test started\n");
int temp = fromFile("./prog2.bin", program); struct sda *sda = malloc(sizeof(struct sda));
int sizeSDA = temp; unsigned int s[1000];
unsigned int s[sizeSDA]; sda->size = 1000;
sda.size = &temp; sda->sda = s;
sda.sda = s; fromFile("prog2.bin", program);
printProgram(program); execute(program, (struct sda *) &sda);
execute(program); printf("Test finished\n");
printSDA(sda);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Initialize the Stack // Initialize the Stack
struct stack stack;
int size = SIZE; int size = SIZE;
int current = 0; stack.size = size;
unsigned int s[SIZE]; stack.currentFrame = 0;
stack.size = &size;
stack.current = &current;
stack.stack = s;
// Initialize ProgrammSpeicher // Initialize ProgrammSpeicher
int psize = 1000; int psize = SIZE;
int saveProgram = 0;
unsigned int p[1000]; unsigned int p[1000];
program.size = &psize; program = malloc(sizeof(struct program));
program.program = p; program->size = psize;
program.saveProgram = &saveProgram; program->program = p;
// Initialize runtime variables // Initialize runtime variables
int debug = 0; int debug = 0;
@ -159,15 +179,17 @@ int main(int argc, char *argv[]) {
*/ */
if (run) { if (run) {
printf("Ninja Virtual Machine started\n"); printf("Ninja Virtual Machine started\n");
struct sda *sda = malloc(sizeof(struct sda));
unsigned int s[sizeSDA]; unsigned int s[sizeSDA];
sda.size = &sizeSDA; sda->size = sizeSDA;
sda.sda = s; sda->sda = s;
printProgram(program); printProgram(program);
execute(program); execute(program, (struct sda *) &sda);
if (debug == 1) printSDA(sda); if (debug == 1) printSDA(sda);
printf("Ninja Virtual Machine stopped\n"); printf("Ninja Virtual Machine stopped\n");
} else { } else {
printf("Error: no code file specified\n"); printf("Error: no code file specified\n");
return 1; return 1;
} }
return 0;
} }

View File

@ -9,23 +9,21 @@
#include <stdio.h> #include <stdio.h>
struct program { struct program {
int *size;
unsigned int *program; unsigned int *program;
int *saveProgram; int size;
}; };
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) { void copyToProgram(const unsigned int codeToCopy[], int size, struct program *program) {
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
program.program[i] = codeToCopy[i]; program->program[i] = codeToCopy[i];
} }
*program.size = size; program->size = size;
*program.saveProgram = 1;
} }
void printProgram(struct program program) { void printProgram(struct program *program) {
char c[10]; char c[10];
for (int i = 0; i < *program.size; i++) { for (int i = 0; i < program->size; i++) {
switch (program.program[i] >> 24) { switch (program->program[i] >> 24) {
case PUSHC: case PUSHC:
strcpy(c, "pushc"); strcpy(c, "pushc");
break; break;
@ -81,7 +79,7 @@ void printProgram(struct program program) {
strcpy(c, "ERROR"); strcpy(c, "ERROR");
break; break;
} }
IMMEDIATE(program.program[i]) ? printf("%03i:\t%s\t%i\n", i, c, SIGN_EXTEND(IMMEDIATE(program.program[i]))) : printf( IMMEDIATE(program->program[i]) ? printf("%03i:\t%s\t%i\n", i, c, SIGN_EXTEND(IMMEDIATE(program->program[i]))) : printf(
"%03i:\t%s\n", i, c); "%03i:\t%s\n", i, c);
} }
} }

11
programs/prog-test-1.asm Normal file
View File

@ -0,0 +1,11 @@
pushc 3
pushc 4
add
pushc 10
pushc 6
sub
mul
wrint
pushc 10
wrchr
halt

BIN
programs/prog-test-1.bin Normal file

Binary file not shown.

9
programs/prog-test-2.asm Normal file
View File

@ -0,0 +1,9 @@
pushc -2
rdint
mul
pushc 3
add
wrint
pushc '\n'
wrchr
halt

BIN
programs/prog-test-2.bin Normal file

Binary file not shown.

5
programs/prog-test-3.asm Normal file
View File

@ -0,0 +1,5 @@
rdchr
wrint
pushc '\n'
wrchr
halt

BIN
programs/prog-test-3.bin Normal file

Binary file not shown.

45
stack.c
View File

@ -3,38 +3,39 @@
// //
#ifndef STACK #ifndef STACK
#define STACK #define STACK
#define SIZE 1000
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
struct stack { struct stackFrame {
int* size; unsigned int *fp; // Frame pointer
int* current; unsigned int *sp; // Stack pointer
unsigned int *stack; unsigned int *bp; // Base pointer
}; };
void printStack(struct stack stack) { struct stack {
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); int size;
for (int i = 0; i < *stack.size; ++i) { int currentFrame;
printf("|%i|\n", stack.stack[i]); struct stackFrame *frames[SIZE];
};
void printStack(struct stack *stack) {
printf("Stack:\n");
for (int i = 0; i < stack->size; ++i) {
printf("[%d] = %d\n", i, stack->frames[stack->currentFrame]->sp[i]);
} }
} }
void push(struct stack s, unsigned int value) { void push(struct stack *stack, int value) {
if (*s.current >= *s.size) { struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
printf("Stack Overflow\n"); *currentFrame->sp++ = value;
exit(EXIT_FAILURE);
}
s.stack[*s.current] = value;
*s.current=*s.current + 1;
} }
unsigned int pop(struct stack s) { int pop(struct stack *stack) {
if (*s.current == 0) { struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
printf("Stack Underflow\n"); return *--currentFrame->sp;
exit(EXIT_FAILURE);
}
*s.current = *s.current -1;
return s.stack[*s.current];
} }
#endif #endif