Compare commits

...

11 Commits
master ... old

Author SHA1 Message Date
nils polek
509dc5e947 Added the correct version to --version funktion 2024-01-14 15:28:46 +00:00
nils polek
c6ae64250d Added a nja and njc goal to Makefile 2024-01-14 15:24:31 +00:00
nils polek
ffc04172ae Added a Makefile for better use 2024-01-14 13:47:08 +00:00
Nils Polek
f5cab7c013 Testing 2023-12-09 17:48:51 +01:00
de248a5943 update x 2023-12-09 17:19:43 +01:00
4994bd6071 add call and ret function
add drop (idk if it is working right)
add prog5 and prog6
2023-12-07 03:01:24 +01:00
0b042fb912 fix jmp not working
fix arithmetic operations
add prog3 and prog4
add debug to instructions
2023-12-07 00:05:36 +01:00
nils polek
77cae7467e Added Instruktions 2023-12-06 18:03:07 +01:00
6ea2a7e735 move programs to a separate folder
fix pushg, popg, pushl and popl
2023-12-04 02:39:23 +01:00
8a89c6bf70 add prog1 and prog2, asm and bin files 2023-12-04 01:21:36 +01:00
88271c91ca fix prog1.bin not working
add debug parameter
add nja-mac
2023-12-04 01:20:41 +01:00
43 changed files with 835 additions and 141 deletions

29
Makefile Normal file
View File

@ -0,0 +1,29 @@
# Makefile for a simple C program
# Compiler
CC = gcc
# Compiler flags
CFLAGS = -g -Wall -std=c99 -pedantic
# Source file
SRC = njvm.c
# Executable name
TARGET = njvm
# Default target
all:
$(CC) $(CFLAGS) -o $(TARGET) $(SRC)
# Clean up
clean:
rm -f $(OBJ) $(TARGET)
run: all
./$(TARGET)
nja: ./nja/nja$(V)
./nja/nja$(V) $(IN) $(OUT)
njc: ./njc/njc$(V)
./njc/njc$(V) $(IN) $(OUT)

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,13 +6,13 @@
#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;
fptr = fopen(path, "r+b"); fptr = fopen(path, "r");
if (fptr == NULL) { if (fptr == NULL) {
printf("Error: cannot open code file %s", path); printf("Error: cannot open code file %s\n", path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
unsigned int buffer[4]; unsigned int buffer[4];

View File

@ -1,7 +1,5 @@
#ifndef CONSTS #ifndef CONSTS
#define CONSTS #define CONSTS
#define VERSION 2 #define VERSION 4
#endif /* ifndef CONSTS #endif
#define CONSTS
#define VERSION 2; */

View File

@ -22,5 +22,17 @@
#define RSF 14 #define RSF 14
#define PUSHL 15 #define PUSHL 15
#define POPL 16 #define POPL 16
#define EQ 17
#define NE 18
#define LT 19
#define LE 20
#define GT 21
#define GE 22
#define JMP 23
#define BRF 24
#define BRT 25
#define CALL 26
#define RET 27
#define DROP 28
#endif /* ifndef INSREUKTION */ #endif /* ifndef INSREUKTION */

BIN
nja-mac Executable file

Binary file not shown.

0
nja → nja/nja2 Normal file → Executable file
View File

BIN
nja/nja3 Executable file

Binary file not shown.

13
nja/nja4 Executable file
View File

@ -0,0 +1,13 @@
--2024-01-14 15:04:22-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a4/nja?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 56947 (56K) [application/octet-stream]
Saving to: nja?inline=false
0K .......... .......... .......... .......... .......... 89% 1.02M 0s
50K ..... 100% 18.3M=0.05s
2024-01-14 15:04:22 (1.13 MB/s) - nja?inline=false saved [56947/56947]

13
nja/nja5 Executable file
View File

@ -0,0 +1,13 @@
--2024-01-14 15:04:59-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a5/nja?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 56947 (56K) [application/octet-stream]
Saving to: nja?inline=false
0K .......... .......... .......... .......... .......... 89% 1.07M 0s
50K ..... 100% 18.4M=0.05s
2024-01-14 15:05:00 (1.18 MB/s) - nja?inline=false saved [56947/56947]

13
nja/nja6 Executable file
View File

@ -0,0 +1,13 @@
--2024-01-14 15:05:08-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a6/nja?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 56947 (56K) [application/octet-stream]
Saving to: nja?inline=false.1
0K .......... .......... .......... .......... .......... 89% 1.04M 0s
50K ..... 100% 25.8M=0.05s
2024-01-14 15:05:08 (1.15 MB/s) - nja?inline=false.1 saved [56947/56947]

13
nja/nja7 Executable file
View File

@ -0,0 +1,13 @@
--2024-01-14 15:05:15-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a7/nja?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 57195 (56K) [application/octet-stream]
Saving to: nja?inline=false.2
0K .......... .......... .......... .......... .......... 89% 1.23M 0s
50K ..... 100% 11.7M=0.04s
2024-01-14 15:05:16 (1.36 MB/s) - nja?inline=false.2 saved [57195/57195]

13
nja/nja8 Executable file
View File

@ -0,0 +1,13 @@
--2024-01-14 15:05:26-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a8/nja?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 57195 (56K) [application/octet-stream]
Saving to: nja?inline=false.3
0K .......... .......... .......... .......... .......... 89% 1.22M 0s
50K ..... 100% 14.5M=0.04s
2024-01-14 15:05:27 (1.35 MB/s) - nja?inline=false.3 saved [57195/57195]

14
njc/njc4 Executable file
View File

@ -0,0 +1,14 @@
--2024-01-14 15:07:13-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a4/njc?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 137095 (134K) [application/octet-stream]
Saving to: njc?inline=false
0K .......... .......... .......... .......... .......... 37% 1.23M 0s
50K .......... .......... .......... .......... .......... 74% 2.63M 0s
100K .......... .......... .......... ... 100% 9.98M=0.06s
2024-01-14 15:07:14 (2.13 MB/s) - njc?inline=false saved [137095/137095]

14
njc/njc5 Executable file
View File

@ -0,0 +1,14 @@
--2024-01-14 15:07:20-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a5/njc?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 137095 (134K) [application/octet-stream]
Saving to: njc?inline=false.1
0K .......... .......... .......... .......... .......... 37% 1.21M 0s
50K .......... .......... .......... .......... .......... 74% 2.51M 0s
100K .......... .......... .......... ... 100% 11.2M=0.06s
2024-01-14 15:07:21 (2.08 MB/s) - njc?inline=false.1 saved [137095/137095]

14
njc/njc6 Executable file
View File

@ -0,0 +1,14 @@
--2024-01-14 15:07:28-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a6/njc?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 137095 (134K) [application/octet-stream]
Saving to: njc?inline=false.2
0K .......... .......... .......... .......... .......... 37% 1.02M 0s
50K .......... .......... .......... .......... .......... 74% 2.03M 0s
100K .......... .......... .......... ... 100% 11.4M=0.07s
2024-01-14 15:07:28 (1.74 MB/s) - njc?inline=false.2 saved [137095/137095]

15
njc/njc7 Executable file
View File

@ -0,0 +1,15 @@
--2024-01-14 15:07:49-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a7/njc?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 166681 (163K) [application/octet-stream]
Saving to: njc?inline=false.6
0K .......... .......... .......... .......... .......... 30% 1.04M 0s
50K .......... .......... .......... .......... .......... 61% 2.13M 0s
100K .......... .......... .......... .......... .......... 92% 11.2M 0s
150K .......... .. 100% 14.2M=0.08s
2024-01-14 15:07:49 (2.11 MB/s) - njc?inline=false.6 saved [166681/166681]

15
njc/njc8 Executable file
View File

@ -0,0 +1,15 @@
--2024-01-14 15:07:46-- https://git.thm.de/arin07/KSP_public/-/raw/master/aufgaben/a8/njc?inline=false
Loaded CA certificate '/etc/ssl/certs/ca-certificates.crt'
Resolving git.thm.de (git.thm.de)... 212.201.6.138
Connecting to git.thm.de (git.thm.de)|212.201.6.138|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 166681 (163K) [application/octet-stream]
Saving to: njc?inline=false.5
0K .......... .......... .......... .......... .......... 30% 1.20M 0s
50K .......... .......... .......... .......... .......... 61% 2.53M 0s
100K .......... .......... .......... .......... .......... 92% 6.70M 0s
150K .......... .. 100% 8.97M=0.07s
2024-01-14 15:07:47 (2.31 MB/s) - njc?inline=false.5 saved [166681/166681]

BIN
njvm-3 Executable file

Binary file not shown.

344
njvm.c
View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "consts.c"
#include "instruktion.c" #include "instruktion.c"
#include "code.c" #include "code.c"
#include "stack.c" #include "stack.c"
@ -7,165 +8,338 @@
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c" #include "SDA.c"
//Comment to disable debug
#define DEBUG
// Stack
struct stack stack;
#define SIZE 1000
// Program // Program
struct program program; struct program *program;
// SDA // SDA
struct sda sda; int fp;
unsigned fp; int debug = 0;
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", VERSION, __DATE__, __TIME__);
} }
void help(void) { 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;
struct stack callStack;
callStack.size = 1000;
callStack.currentFrame = 0;
for (int i = 0; i < stack.size; ++i) {
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
frame->fp = malloc(sizeof(int) * stack.size);
frame->sp = malloc(sizeof(int) * stack.size);
frame->bp = NULL;
stack.frames[i] = frame;
}
for (int i = 0; i < callStack.size; ++i) {
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
frame->fp = malloc(sizeof(int) * stack.size);
frame->sp = malloc(sizeof(int) * stack.size);
frame->bp = NULL;
callStack.frames[i] = frame;
}
struct stackFrame *currentFrame = NULL;
//struct stackFrame *currentCallFrame = 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];
if (i >= program->size) {
printf("Error: Jump out of program memory\n");
goto end;
}
switch (instruction >> 24) {
case HALT: case HALT:
goto end; goto end;
case PUSHC: case PUSHC:
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i]))); if (debug == 1) printf("PUSHC %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
break; break;
case ADD: case ADD:
push(stack, pop(stack) + pop(stack)); if (debug == 1) printf("ADD\n");
push(&stack, pop(&stack) + pop(&stack));
break; break;
case SUB: case SUB:
temp = pop(stack); tmp = pop(&stack);
push(stack, pop(stack) - temp); if (debug == 1) printf("SUB\n");
if (debug == 1) printf("tmp: %d\n", tmp);
push(&stack, pop(&stack) - tmp);
break; break;
case MUL: case MUL:
push(stack, pop(stack) * pop(stack)); if (debug == 1) printf("MUL\n");
push(&stack, pop(&stack) * pop(&stack));
break; break;
case DIV: case DIV:
temp = pop(stack); tmp = pop(&stack);
push(stack, pop(stack) / temp); if (debug == 1) printf("DIV\n");
if (debug == 1) printf("tmp: %d\n", tmp);
push(&stack, pop(&stack) / tmp);
break; break;
case MOD: case MOD:
temp = pop(stack); tmp = pop(&stack);
push(stack, pop(stack) % temp); if (debug == 1) printf("MOD\n");
if (debug == 1) printf("tmp: %d\n", tmp);
push(&stack, pop(&stack) % tmp);
break; break;
case RDINT: case RDINT:
scanf("%i", &intInput); scanf("%i", &intInput);
push(stack, intInput); if (debug == 1) printf("RDINT %d\n", intInput);
push(&stack, intInput);
break; break;
case WRINT: case WRINT:
printf("%i", pop(stack)); if (debug == 1) printf("WRINT\n");
printf("%i", pop(&stack));
break; break;
case RDCHR: case RDCHR:
scanf("%c", &charInput); scanf("%c", &charInput);
push(stack, charInput); if (debug == 1) printf("RDCHR %c\n", charInput);
push(&stack, charInput);
break; break;
case WRCHR: case WRCHR:
printf("%c", pop(stack)); if (debug == 1) printf("WRCHR\n");
printf("%c", pop(&stack));
break; break;
case PUSHG: case PUSHG:
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)); if (debug == 1) printf("PUSHG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
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); if (debug == 1) printf("POPG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
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); if (debug == 1) printf("ASF %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
fp = *stack.current; currentFrame = stack.frames[stack.currentFrame];
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i])); push(&stack, *currentFrame->sp);
*currentFrame->sp = stack.currentFrame;
currentFrame->bp = currentFrame->sp;
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
break; break;
case RSF: case RSF:
*stack.current = fp; if (debug == 1) printf("RSF\n");
fp = pop(stack); stack.currentFrame = 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); printf("PUSHL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
currentFrame = stack.frames[stack.currentFrame];
currentFrame->bp = currentFrame->sp;
int x = SIGN_EXTEND(IMMEDIATE(instruction));
push(&stack, currentFrame->fp[x]);
break; break;
case POPL: case POPL:
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); if (debug == 1) printf("POPL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
currentFrame = stack.frames[stack.currentFrame];
currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))] = pop(&stack);
break;
case EQ:
if (debug == 1) printf("EQ\n");
if (pop(&stack) == pop(&stack)) {
push(&stack, 1);
} else {
push(&stack, 0);
}
break;
case NE:
if (debug == 1) printf("NE\n");
if (pop(&stack) != pop(&stack)) {
push(&stack, 1);
} else {
push(&stack, 0);
}
break;
case LT:
if (debug == 1) printf("LT\n");
tmp = pop(&stack);
if (pop(&stack) < tmp) {
push(&stack, 1);
} else {
push(&stack, 0);
}
break;
case LE:
if (debug == 1) printf("LE\n");
tmp = pop(&stack);
if (pop(&stack) <= tmp) {
push(&stack, 1);
} else {
push(&stack, 0);
}
break;
case GT:
if (debug == 1) printf("GT\n");
if (debug == 1) printf("peek(1): %d\n", peek(&stack, 1));
if (debug == 1) printf("peek(2): %d\n", peek(&stack, 2));
if (debug == 1) printf("peek(1) > peek(2): %d\n", peek(&stack, 2) > peek(&stack, 1));
tmp = pop(&stack);
if (pop(&stack) > tmp) {
push(&stack, 1);
} else {
push(&stack, 0);
}
break;
case GE:
if (debug == 1) printf("GE\n");
tmp = pop(&stack);
if (pop(&stack) >= tmp) {
push(&stack, 1);
} else {
push(&stack, 0);
}
break;
case JMP:
if (debug == 1) printf("JMP %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
if (debug == 1) printf("JMP %d\n", j);
if (j-- >= program->size) {
printf("Error: Jump out of program memory\n");
goto end;
}
i = j;
break;
case BRF: // branch on false
if (debug == 1) printf("BRF %d\nStack: %d\n", SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack));
if (pop(&stack) == 0) {
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
if (j-- >= program->size) {
printf("Error: BRF out of program memory\n");
goto end;
}
i = j;
}
break;
case BRT:
if (debug == 1) printf("BRT %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
if (pop(&stack) == 1) {
int j = SIGN_EXTEND(IMMEDIATE(program->program[i]));
if (j-- >= program->size) {
printf("Error: BRT out of program memory\n");
goto end;
}
i = j;
}
break;
case CALL:
tmp = SIGN_EXTEND(IMMEDIATE(program->program[i]));
if (j-- >= program->size) {
printf("Error: Call out of program memory\n");
goto end;
}
push(&callStack, i + 1);
i = tmp;
break;
case RET:
if (debug == 1) printf("RET\n");
tmp = pop(&callStack);
if (tmp-- >= program->size) {
printf("Error: Return out of program memory\n");
goto end;
}
i = tmp;
break;
case DROP:
tmp = SIGN_EXTEND(IMMEDIATE(instruction));
if (debug == 1) printf("DROP %d\n", tmp);
for (int b = 0; b < tmp; ++b) {
pop(&stack);
}
break; break;
} }
} }
end: end:
return; return;
} }
#ifdef DEBUG // run prog2.bin
void tests(void) { void tests(void) {
printf("Runnig debug mode\n"); printf("Test started\n");
int temp = fromFile("./prog1.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);*/
printf("Test finished\n");
} }
#endif /* ifdef DEBUG */
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
int run = 0;
int sizeSDA;
#ifdef DEBUG
tests();
#endif /* ifdef DEBUG */
if (argc > 1) { if (argc > 1) {
if (strcmp(argv[1], "--version") == 0) { for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--debug") == 0) {
debug = 1;
} else if (strcmp(argv[i], "--version") == 0) {
version(); version();
} else if (strcmp(argv[1], "--help") == 0) { return 0;
} else if (strcmp(argv[i], "--help") == 0) {
help(); help();
} else if (strcmp(argv[1], "--prog1") == 0) { return 0;
copyToProgram(code1, sizeof(code1) / sizeof(code1[0]), program);
goto run;
} else if (strcmp(argv[1], "--prog2") == 0) {
copyToProgram(code2, sizeof(code2) / sizeof(code2[0]), program);
goto run;
} else if (strcmp(argv[1], "--prog3") == 0) {
copyToProgram(code3, sizeof(code3) / sizeof(code3[0]), program);
goto run;
} else { } else {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]); sizeSDA = fromFile(argv[i], program);
run = 1;
} }
} else { }
run: }
// Started
if (*program.saveProgram == 1) { /*
* Debug mode
*/
if (debug) {
tests();
}
/*
* Run program
*/
if (run) {
printf("Ninja Virtual Machine started\n"); printf("Ninja Virtual Machine started\n");
printProgram(program); struct sda *sda = malloc(sizeof(struct sda));
execute(program); unsigned int s[sizeSDA];
printSDA(sda); sda->size = sizeSDA;
sda->sda = s;
if (debug == 1) printProgram(program);
execute(program, (struct sda *) &sda);
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;
} }
// Stopped
printf("Ninja Virtual Machine stopped\n");
return 0; return 0;
} }
}

BIN
prog1.bin

Binary file not shown.

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;
@ -77,11 +75,44 @@ void printProgram(struct program program) {
case POPL: case POPL:
strcpy(c, "popl"); strcpy(c, "popl");
break; break;
case EQ:
strcpy(c,"eq");
break;
case NE:
strcpy(c,"ne");
break;
case LT:
strcpy(c,"lt");
break;
case GT:
strcpy(c,"gt");
break;
case GE:
strcpy(c,"ge");
break;
case JMP:
strcpy(c,"jmp");
break;
case BRF:
strcpy(c, "brf");
break;
case BRT:
strcpy(c,"brt");
break;
case CALL:
strcpy(c,"call");
break;
case RET:
strcpy(c,"ret");
break;
case DROP:
strcpy(c,"drop");
break;
default: default:
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.

31
programs/prog1.asm Normal file
View File

@ -0,0 +1,31 @@
//
// prog1.asm -- an assembler example with global variables
//
// global Integer x;
// global Integer y;
// x = 2;
// y = x + 3;
// x = 7 * y + x;
// writeInteger(x + -33);
// writeCharacter('\n');
pushc 2
popg 0
pushg 0
pushc 3
add
popg 1
pushc 7
pushg 1
mul
pushg 0
add
popg 0
pushg 0
pushc -33
add
wrint
pushc '\n'
wrchr
halt

BIN
programs/prog1.bin Normal file

Binary file not shown.

33
programs/prog2.asm Normal file
View File

@ -0,0 +1,33 @@
//
// prog2.asm -- an assembler example with local variables
//
// local Integer x;
// local Integer y;
// x = 2;
// y = x + 3;
// x = 7 * y + x;
// writeInteger(x + -33);
// writeCharacter('\n');
asf 2
pushc 2
popl 0
pushl 0
pushc 3
add
popl 1
pushc 7
pushl 1
mul
pushl 0
add
popl 0
pushl 0
pushc -33
add
wrint
pushc '\n'
wrchr
rsf
halt

BIN
programs/prog2.bin Normal file

Binary file not shown.

61
programs/prog3.asm Normal file
View File

@ -0,0 +1,61 @@
//
// prog1.asm -- an assembler example with global variables
//
//
// compute the gcd of two positive numbers
//
// global Integer x;
// global Integer y;
// x = readInteger();
// y = readInteger();
// while (x != y) {
// if (x > y) {
// x = x - y;
// } else {
// y = y - x;
// }
// }
// writeInteger(x);
// writeCharacter('\n');
// x = readInteger();
rdint
popg 0
// y = readInteger();
rdint
popg 1
// while ...
L1:
// x != y
pushg 0 // 4
pushg 1
ne
brf L2
// if ...
pushg 0
pushg 1
gt
brf L3
// x = x - y
pushg 0
pushg 1
sub
popg 0
jmp L4
L3:
// y = y - x
pushg 1 // 17
pushg 0
sub
popg 1
L4:
jmp L1 // 21
L2:
// writeInteger(x);
pushg 0 // 22
wrint
// writeCharacter('\n');
pushc '\n'
wrchr
halt

BIN
programs/prog3.bin Normal file

Binary file not shown.

63
programs/prog4.asm Normal file
View File

@ -0,0 +1,63 @@
//
// prog2.asm -- an assembler example with local variables
//
//
// compute the gcd of two positive numbers
//
// local Integer x;
// local Integer y;
// x = readInteger();
// y = readInteger();
// while (x != y) {
// if (x > y) {
// x = x - y;
// } else {
// y = y - x;
// }
// }
// writeInteger(x);
// writeCharacter('\n');
asf 2
// x = readInteger();
rdint
popl 0
// y = readInteger();
rdint
popl 1
// while ...
L1:
// x != y
pushl 0
pushl 1
ne
brf L2
// if ...
pushl 0
pushl 1
gt
brf L3
// x = x - y
pushl 0
pushl 1
sub
popl 0
jmp L4
L3:
// y = y - x
pushl 1
pushl 0
sub
popl 1
L4:
jmp L1
L2:
// writeInteger(x);
pushl 0
wrint
// writeCharacter('\n');
pushc '\n'
wrchr
rsf
halt

BIN
programs/prog4.bin Normal file

Binary file not shown.

35
programs/prog5.asm Normal file
View File

@ -0,0 +1,35 @@
//
// prog01.asm -- call/ret without args, and without ret value
//
asf 3
pushc 11
wrint
pushc '\n'
wrchr
call proc
pushc 44
wrint
pushc '\n'
wrchr
rsf
halt
proc:
asf 2
pushc 22
wrint
pushc '\n'
wrchr
call proctwo
rsf
ret
proctwo:
asf 2
pushc 33
wrint
pushc '\n'
wrchr
rsf
ret

BIN
programs/prog5.bin Normal file

Binary file not shown.

28
programs/prog6.asm Normal file
View File

@ -0,0 +1,28 @@
//
// prog02.asm -- call/ret with args, but without ret value
//
asf 3
pushc 11
pushc 33
call proc
drop 2
rsf
halt
proc:
asf 2
pushl -4
wrint
pushc '\n'
wrchr
pushc 22
wrint
pushc '\n'
wrchr
pushl -3
wrint
pushc '\n'
wrchr
rsf
ret

BIN
programs/prog6.bin Normal file

Binary file not shown.

28
programs/prog7.asm Normal file
View File

@ -0,0 +1,28 @@
//
// prog02.asm -- call/ret with args, but without ret value
//
asf 3
pushc 11
pushc 33
call proc
drop 2
rsf
halt
proc:
asf 2
pushl 4
wrint
pushc '\n'
wrchr
pushc 22
wrint
pushc '\n'
wrchr
pushl 3
wrint
pushc '\n'
wrchr
rsf
ret

BIN
programs/prog7.bin Normal file

Binary file not shown.

50
stack.c
View File

@ -3,38 +3,46 @@
// //
#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; int *fp; // Frame pointer
int* current; int *sp; // Stack pointer
unsigned int *stack; 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, unsigned int value) {
if (*s.current >= *s.size) { struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
printf("Stack Overflow\n"); *(currentFrame->sp) = value;
exit(EXIT_FAILURE); currentFrame->sp++;
}
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"); currentFrame->sp--;
exit(EXIT_FAILURE); return *(currentFrame->sp);
} }
*s.current = *s.current -1;
return s.stack[*s.current]; int peek(struct stack *stack, int steps) { // peek is pop without removing the value
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
return *(currentFrame->sp - steps);
} }
#endif #endif