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>
struct sda {
int *size;
unsigned int *sda;
int size;
};
unsigned int getSDA(int i, struct sda s) {
return s.sda[i];
int getSDA(unsigned int offset, struct sda *sda) {
return sda->sda[offset];
}
void setSDA(int point, int val, struct sda s) {
s.sda[point] = val;
void setSDA(unsigned int offset, int value, struct sda *sda) {
sda->sda[offset] = value;
}
void printSDA(struct sda s) {
for (int i = 0; i < *s.size; i++) {
printf("%i\n", getSDA(i, s));
void printSDA(struct sda *sda) {
printf("SDA:\n");
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 "program.c"
unsigned int fromFile(char *path, struct program program) {
unsigned int fromFile(char *path, struct program *program) {
unsigned int countInstructions;
unsigned int staticVars;
FILE *fptr;

132
njvm.c
View File

@ -7,15 +7,10 @@
#include "codeReader.c"
#include "SDA.c"
// Stack
struct stack stack;
#define SIZE 1000
// Program
struct program program;
struct program *program;
// SDA
struct sda sda;
unsigned fp;
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");
}
void execute(struct program program) {
int i;
void execute(struct program *program, struct sda *sda) {
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;
unsigned int temp;
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:
goto end;
case PUSHC:
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
break;
case ADD:
push(stack, pop(stack) + pop(stack));
push(&stack, pop(&stack) + pop(&stack));
break;
case SUB:
temp = pop(stack);
push(stack, pop(stack) - temp);
tmp = pop(&stack);
push(&stack, pop(&stack) - tmp);
break;
case MUL:
push(stack, pop(stack) * pop(stack));
push(&stack, pop(&stack) * pop(&stack));
break;
case DIV:
temp = pop(stack);
push(stack, pop(stack) / temp);
tmp = pop(&stack);
push(&stack, pop(&stack) / tmp);
break;
case MOD:
temp = pop(stack);
push(stack, pop(stack) % temp);
tmp = pop(&stack);
push(&stack, pop(&stack) % tmp);
break;
case RDINT:
scanf("%i", &intInput);
push(stack, intInput);
push(&stack, intInput);
break;
case WRINT:
printf("%i", pop(stack));
printf("%i", pop(&stack));
break;
case RDCHR:
scanf("%c", &charInput);
push(stack, charInput);
push(&stack, charInput);
break;
case WRCHR:
printf("%c", pop(stack));
printf("%c", pop(&stack));
break;
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;
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;
case ASF:
push(stack, *stack.current);
fp = *stack.current;
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
currentFrame = stack.frames[stack.currentFrame];
push(&stack, *currentFrame->sp);
*currentFrame->sp = stack.currentFrame;
currentFrame->bp = currentFrame->sp;
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
break;
case RSF:
*stack.current = fp;
fp = pop(stack);
stack.currentFrame = pop(&stack);
currentFrame = stack.frames[stack.currentFrame];
currentFrame->bp = NULL;
break;
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;
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;
}
}
end:
return;
}
// run prog2.bin
void tests(void) {
printf("Runnig debug mode\n");
int temp = fromFile("./prog2.bin", program);
int sizeSDA = temp;
unsigned int s[sizeSDA];
sda.size = &temp;
sda.sda = s;
printProgram(program);
execute(program);
printSDA(sda);
printf("Test started\n");
struct sda *sda = malloc(sizeof(struct sda));
unsigned int s[1000];
sda->size = 1000;
sda->sda = s;
fromFile("prog2.bin", program);
execute(program, (struct sda *) &sda);
printf("Test finished\n");
}
int main(int argc, char *argv[]) {
// Initialize the Stack
struct stack stack;
int size = SIZE;
int current = 0;
unsigned int s[SIZE];
stack.size = &size;
stack.current = &current;
stack.stack = s;
stack.size = size;
stack.currentFrame = 0;
// Initialize ProgrammSpeicher
int psize = 1000;
int saveProgram = 0;
int psize = SIZE;
unsigned int p[1000];
program.size = &psize;
program.program = p;
program.saveProgram = &saveProgram;
program = malloc(sizeof(struct program));
program->size = psize;
program->program = p;
// Initialize runtime variables
int debug = 0;
@ -159,15 +179,17 @@ int main(int argc, char *argv[]) {
*/
if (run) {
printf("Ninja Virtual Machine started\n");
struct sda *sda = malloc(sizeof(struct sda));
unsigned int s[sizeSDA];
sda.size = &sizeSDA;
sda.sda = s;
sda->size = sizeSDA;
sda->sda = s;
printProgram(program);
execute(program);
execute(program, (struct sda *) &sda);
if (debug == 1) printSDA(sda);
printf("Ninja Virtual Machine stopped\n");
} else {
printf("Error: no code file specified\n");
return 1;
}
return 0;
}

View File

@ -9,23 +9,21 @@
#include <stdio.h>
struct program {
int *size;
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) {
program.program[i] = codeToCopy[i];
program->program[i] = codeToCopy[i];
}
*program.size = size;
*program.saveProgram = 1;
program->size = size;
}
void printProgram(struct program program) {
void printProgram(struct program *program) {
char c[10];
for (int i = 0; i < *program.size; i++) {
switch (program.program[i] >> 24) {
for (int i = 0; i < program->size; i++) {
switch (program->program[i] >> 24) {
case PUSHC:
strcpy(c, "pushc");
break;
@ -81,7 +79,7 @@ void printProgram(struct program program) {
strcpy(c, "ERROR");
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);
}
}

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