Compare commits
32 Commits
old
...
37843d9356
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37843d9356 | ||
|
|
6cc2ff87ca | ||
|
|
f29f500d0c | ||
|
|
23e787eb5e | ||
|
|
94fb404c71 | ||
| 847f68bb29 | |||
| da06afd43b | |||
| 9421d34756 | |||
|
|
60bdfd7843 | ||
| bdb0ce63b8 | |||
| dcb665f5a6 | |||
|
|
82dc5f28cc | ||
| 8be99c5244 | |||
| 742f050b24 | |||
| 14025d3b08 | |||
| 3f12378110 | |||
| 2cb94eaf76 | |||
| 9d64b87a09 | |||
| b87ebbe841 | |||
| 829735eb8c | |||
| 44f70a1777 | |||
| 5a7338f91c | |||
| a8999f4ec2 | |||
| c0a5e694c0 | |||
|
|
61a138fdee | ||
| 55af679c39 | |||
| 27400289f5 | |||
| c0d25212af | |||
| 10a6883750 | |||
|
|
33a2994c29 | ||
|
|
7390df9ee0 | ||
|
|
83dae77e31 |
35
Makefile
Normal file
35
Makefile
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Makefile for a simple C program
|
||||||
|
|
||||||
|
# Compiler
|
||||||
|
CC = gcc
|
||||||
|
|
||||||
|
# program to Run
|
||||||
|
F = prog.bin
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
debug: all
|
||||||
|
./$(TARGET) --debug $(F)
|
||||||
|
|
||||||
|
run: all
|
||||||
|
./$(TARGET) $(F)
|
||||||
|
|
||||||
|
nja: ./nja/nja$(V)
|
||||||
|
./nja/nja$(V) $(IN) $(OUT)
|
||||||
|
|
||||||
|
njc: ./njc/njc$(V)
|
||||||
|
./njc/njc$(V) $(IN) $(OUT)
|
||||||
12
SDA.c
12
SDA.c
@@ -4,24 +4,24 @@
|
|||||||
#ifndef SDA
|
#ifndef SDA
|
||||||
#define SDA
|
#define SDA
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "stackslot.c"
|
||||||
struct sda {
|
struct sda {
|
||||||
int *size;
|
int *size;
|
||||||
unsigned int *sda;
|
ObjRef *sda;
|
||||||
};
|
};
|
||||||
|
|
||||||
unsigned int getSDA(int i, struct sda s) {
|
ObjRef getSDA(int i, struct sda s) {
|
||||||
return s.sda[i];
|
return s.sda[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSDA(int point, int val, struct sda s) {
|
void setSDA(int point, ObjRef val, struct sda s) {
|
||||||
s.sda[point] = val;
|
s.sda[point] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printSDA(struct sda s) {
|
void printSDA(struct sda s) {
|
||||||
for (int i = 0; i < *s.size; i++) {
|
for (int i = 0; i < *s.size; i++) {
|
||||||
printf("%i\n", getSDA(i, s));
|
printf("%i\n", *(int *)getSDA(i, s)->data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ 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", path);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|||||||
2
consts.c
2
consts.c
@@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 2
|
#define VERSION 5
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
40
debugMenu.c
Normal file
40
debugMenu.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef DEBUGMENU
|
||||||
|
#define DEBUGMENU
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "stack.c"
|
||||||
|
#include "stack.c"
|
||||||
|
|
||||||
|
void inspect(struct stack s, int fp){
|
||||||
|
char input[20];
|
||||||
|
printf("DEBUG [inspect]: stack, datam object?");
|
||||||
|
fgets(input,20,stdin);
|
||||||
|
if (input[0] == 's') printStack(s, fp);
|
||||||
|
if (input[0] == 'd'){/* todo */ }
|
||||||
|
if (input[0] == 'o'){/* todo */}
|
||||||
|
}
|
||||||
|
void list(){
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
void breakpoint(){
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
|
void debugMenu(int fp, struct stack stack, int* debug){
|
||||||
|
char input[20];
|
||||||
|
while (true) {
|
||||||
|
printf("DEBUG: inspect, list, breakpoint, run, step, quit?");
|
||||||
|
fgets(input, 20, stdin);
|
||||||
|
printf("%s",input);
|
||||||
|
if(input[0] == 'i') {inspect(stack,fp);}
|
||||||
|
if(input[0] == 'l') list();
|
||||||
|
if(input[0] == 'b') breakpoint();
|
||||||
|
if(input[0] == 's') break;
|
||||||
|
if(input[0] == 'r') {*debug = 0; break;};
|
||||||
|
if(input[0] == 'q') exit(0);
|
||||||
|
strcpy(input, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* ifndef DEBUGMENU */
|
||||||
@@ -22,5 +22,20 @@
|
|||||||
#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
|
||||||
|
#define PUSHR 29
|
||||||
|
#define POPR 30
|
||||||
|
#define DUP 31
|
||||||
|
|
||||||
#endif /* ifndef INSREUKTION */
|
#endif /* ifndef INSREUKTION */
|
||||||
|
|||||||
0
nja → nja/nja2
Normal file → Executable file
0
nja → nja/nja2
Normal file → Executable file
14
njc/njc4
Executable file
14
njc/njc4
Executable 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
14
njc/njc5
Executable 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
14
njc/njc6
Executable 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
15
njc/njc7
Executable 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
15
njc/njc8
Executable 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]
|
||||||
|
|
||||||
252
njvm.c
252
njvm.c
@@ -1,4 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "instruktion.c"
|
#include "instruktion.c"
|
||||||
#include "code.c"
|
#include "code.c"
|
||||||
@@ -6,15 +7,19 @@
|
|||||||
#include "program.c"
|
#include "program.c"
|
||||||
#include "codeReader.c"
|
#include "codeReader.c"
|
||||||
#include "SDA.c"
|
#include "SDA.c"
|
||||||
|
#include "reg.c"
|
||||||
|
#include "debugMenu.c"
|
||||||
|
|
||||||
//Comment to disable debug
|
// Debug
|
||||||
|
int debug = 0;
|
||||||
#define DEBUG
|
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
#define SIZE 1000
|
#define SIZE 1000
|
||||||
|
|
||||||
|
//Register
|
||||||
|
struct reg reg;
|
||||||
|
|
||||||
// Program
|
// Program
|
||||||
struct program program;
|
struct program program;
|
||||||
|
|
||||||
@@ -35,93 +40,203 @@ void execute(struct program program) {
|
|||||||
int intInput;
|
int intInput;
|
||||||
unsigned int temp;
|
unsigned int temp;
|
||||||
char charInput;
|
char charInput;
|
||||||
|
StackSlot tempSlot;
|
||||||
for (i = 0; i < *program.size; ++i) {
|
for (i = 0; i < *program.size; ++i) {
|
||||||
|
if (debug == 1) debugMenu(fp,stack,&debug);
|
||||||
switch (program.program[i] >> 24) {
|
switch (program.program[i] >> 24) {
|
||||||
case HALT:
|
case HALT:
|
||||||
|
if (debug == 1) printf("halt\n");
|
||||||
goto end;
|
goto end;
|
||||||
case PUSHC:
|
case PUSHC:
|
||||||
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
|
||||||
|
push(stack, stackSlotWithObjRef(getIntObj(SIGN_EXTEND(IMMEDIATE(program.program[i])))));
|
||||||
break;
|
break;
|
||||||
case ADD:
|
case ADD:
|
||||||
push(stack, pop(stack) + pop(stack));
|
if (debug == 1) printf("add: %i + %i\n",peek(stack, 2),peek(stack, 1));
|
||||||
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) + getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case SUB:
|
case SUB:
|
||||||
temp = pop(stack);
|
if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
push(stack, pop(stack) - temp);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) - temp)));
|
||||||
break;
|
break;
|
||||||
case MUL:
|
case MUL:
|
||||||
push(stack, pop(stack) * pop(stack));
|
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) * getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case DIV:
|
case DIV:
|
||||||
temp = pop(stack);
|
if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
push(stack, pop(stack) / temp);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)) / temp)));
|
||||||
break;
|
break;
|
||||||
case MOD:
|
case MOD:
|
||||||
temp = pop(stack);
|
if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
push(stack, pop(stack) % temp);
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
push(stack, stackSlotWithObjRef(getIntObj(getIntValfromStackSlot(pop(stack)))));
|
||||||
break;
|
break;
|
||||||
case RDINT:
|
case RDINT:
|
||||||
|
if (debug == 1) printf("rdint\n");
|
||||||
scanf("%i", &intInput);
|
scanf("%i", &intInput);
|
||||||
push(stack, intInput);
|
push(stack, stackSlotWithObjRef(getIntObj(intInput)));
|
||||||
|
if (debug == 1) printf("pushed %i\n", intInput);
|
||||||
break;
|
break;
|
||||||
case WRINT:
|
case WRINT:
|
||||||
printf("%i", pop(stack));
|
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
|
||||||
|
printf("%i", getIntValfromStackSlot(pop(stack)));
|
||||||
break;
|
break;
|
||||||
case RDCHR:
|
case RDCHR:
|
||||||
|
if (debug == 1) printf("rdchr\n");
|
||||||
scanf("%c", &charInput);
|
scanf("%c", &charInput);
|
||||||
push(stack, charInput);
|
push(stack, stackSlotWithObjRef(getIntObj(charInput)));
|
||||||
|
if (debug == 1) printf("pushed %c\n", charInput);
|
||||||
break;
|
break;
|
||||||
case WRCHR:
|
case WRCHR:
|
||||||
printf("%c", pop(stack));
|
if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
|
||||||
|
printf("%c", getIntValfromStackSlot(pop(stack)));
|
||||||
break;
|
break;
|
||||||
case PUSHG:
|
case PUSHG:
|
||||||
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
|
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
push(stack, stackSlotWithObjRef(getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)));
|
||||||
break;
|
break;
|
||||||
case POPG:
|
case POPG:
|
||||||
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
|
if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack).u.objRef, sda);
|
||||||
break;
|
break;
|
||||||
case ASF:
|
case ASF:
|
||||||
push(stack, *stack.current);
|
if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
push(stack, stackSlotWitchNumber(fp));
|
||||||
fp = *stack.current;
|
fp = *stack.current;
|
||||||
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
break;
|
break;
|
||||||
case RSF:
|
case RSF:
|
||||||
|
if (debug == 1) printf("rsf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
*stack.current = fp;
|
*stack.current = fp;
|
||||||
fp = pop(stack);
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
case PUSHL:
|
tempSlot = pop(stack) ;
|
||||||
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
|
fp = getIntValfromStackSlot(tempSlot);
|
||||||
break;
|
break;
|
||||||
case POPL:
|
case POPL:
|
||||||
|
if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
|
||||||
|
break;
|
||||||
|
case PUSHL:
|
||||||
|
if (debug == 1) printf("pushl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
|
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
|
||||||
break;
|
break;
|
||||||
|
case NE:
|
||||||
|
if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) != getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWitchNumber(1));
|
||||||
|
else push(stack, stackSlotWitchNumber(0));
|
||||||
|
break;
|
||||||
|
case EQ:
|
||||||
|
if (debug == 1) printf("eq: %i == %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) == getIntValfromStackSlot(pop(stack))) push(stack, stackSlotWitchNumber(1));
|
||||||
|
else push(stack, stackSlotWitchNumber(0));
|
||||||
|
break;
|
||||||
|
case LT:
|
||||||
|
if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) < temp) push(stack, stackSlotWitchNumber(1));
|
||||||
|
else push(stack, stackSlotWitchNumber(0));
|
||||||
|
break;
|
||||||
|
case LE:
|
||||||
|
if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) <= temp) push(stack, stackSlotWitchNumber(1));
|
||||||
|
else push(stack, stackSlotWitchNumber(0));
|
||||||
|
break;
|
||||||
|
case GT:
|
||||||
|
if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) > temp) push(stack, stackSlotWitchNumber(1));
|
||||||
|
else push(stack, stackSlotWitchNumber(0));
|
||||||
|
break;
|
||||||
|
case GE:
|
||||||
|
if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1));
|
||||||
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) >= temp) push(stack, stackSlotWitchNumber(1));
|
||||||
|
else push(stack, stackSlotWitchNumber(0));
|
||||||
|
break;
|
||||||
|
case BRF:
|
||||||
|
if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) == 0) {
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case BRT:
|
||||||
|
if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
|
if (getIntValfromStackSlot(pop(stack)) == 1) {
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case JMP:
|
||||||
|
if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
|
break;
|
||||||
|
case CALL:
|
||||||
|
if (debug == 1) printf("call: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
|
push(stack, stackSlotWitchNumber(i));
|
||||||
|
if (debug == 1) printf("push: %i\n", i + 1);
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i])) - 1;
|
||||||
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
|
break;
|
||||||
|
case RET:
|
||||||
|
if (debug == 1) printf("ret\n");
|
||||||
|
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
|
||||||
|
i = getIntValfromStackSlot(pop(stack));
|
||||||
|
if (debug == 1) printf("new i: %i\n", i);
|
||||||
|
break;
|
||||||
|
case DROP:
|
||||||
|
if(debug ==1) printf("drop\n");
|
||||||
|
*stack.current = *stack.current - SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
|
break;
|
||||||
|
case DUP:
|
||||||
|
if (debug==1) printf("dup\n");
|
||||||
|
temp = getIntValfromStackSlot(pop(stack));
|
||||||
|
push(stack, stackSlotWitchNumber(temp));
|
||||||
|
push(stack, stackSlotWitchNumber(temp));
|
||||||
|
break;
|
||||||
|
case POPR:
|
||||||
|
if (debug==1) printf("popr") ;
|
||||||
|
pushR(reg, getIntValfromStackSlot(pop(stack)));
|
||||||
|
if(debug == 1) printStackR(reg);
|
||||||
|
break;
|
||||||
|
case PUSHR:
|
||||||
|
if(debug == 1) printf("pushr");
|
||||||
|
push(stack, stackSlotWitchNumber(popR(reg)));
|
||||||
|
if(debug == 1) printStackR(reg);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
|
|
||||||
void tests(void) {
|
void tests(void) {
|
||||||
printf("Runnig debug mode\n");
|
|
||||||
int temp = fromFile("./prog1.bin", program);
|
|
||||||
int sizeSDA = temp;
|
|
||||||
unsigned int s[sizeSDA];
|
|
||||||
sda.size = &temp;
|
|
||||||
sda.sda = s;
|
|
||||||
printProgram(program);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* ifdef DEBUG */
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// Initialize the Stack
|
// Initialize the Stack
|
||||||
int size = SIZE;
|
int size = SIZE;
|
||||||
int current = 0;
|
int current = 0;
|
||||||
unsigned int s[SIZE];
|
StackSlot s[SIZE];
|
||||||
stack.size = &size;
|
stack.size = &size;
|
||||||
stack.current = ¤t;
|
stack.current = ¤t;
|
||||||
stack.stack = s;
|
stack.stack = s;
|
||||||
|
|
||||||
|
// Initialize the registery
|
||||||
|
int rSize = SIZE;
|
||||||
|
int rCurrent = 0;
|
||||||
|
unsigned int r[SIZE];
|
||||||
|
reg.size = &rSize;
|
||||||
|
reg.current = &rCurrent;
|
||||||
|
reg.stack = r;
|
||||||
|
|
||||||
// Initialize ProgrammSpeicher
|
// Initialize ProgrammSpeicher
|
||||||
int psize = 1000;
|
int psize = 1000;
|
||||||
@@ -131,41 +246,46 @@ int main(int argc, char *argv[]) {
|
|||||||
program.program = p;
|
program.program = p;
|
||||||
program.saveProgram = &saveProgram;
|
program.saveProgram = &saveProgram;
|
||||||
|
|
||||||
|
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) {
|
||||||
version();
|
if (strcmp(argv[i], "--debug") == 0) {
|
||||||
} else if (strcmp(argv[1], "--help") == 0) {
|
debug = 1;
|
||||||
help();
|
} else if (strcmp(argv[i], "--version") == 0) {
|
||||||
} else if (strcmp(argv[1], "--prog1") == 0) {
|
version();
|
||||||
copyToProgram(code1, sizeof(code1) / sizeof(code1[0]), program);
|
return 0;
|
||||||
goto run;
|
} else if (strcmp(argv[i], "--help") == 0) {
|
||||||
} else if (strcmp(argv[1], "--prog2") == 0) {
|
help();
|
||||||
copyToProgram(code2, sizeof(code2) / sizeof(code2[0]), program);
|
return 0;
|
||||||
goto run;
|
} else if (strcmp(argv[i], "--stack") == 0) {
|
||||||
} else if (strcmp(argv[1], "--prog3") == 0) {
|
i++;
|
||||||
copyToProgram(code3, sizeof(code3) / sizeof(code3[0]), program);
|
// TODO: implement stack size
|
||||||
goto run;
|
} else if(strcmp(argv[i], "--heap") == 0) {
|
||||||
} else {
|
i++;
|
||||||
printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
|
// TODO: implement heap size
|
||||||
|
} else {
|
||||||
|
sizeSDA = fromFile(argv[i], program);
|
||||||
|
run = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
run:
|
|
||||||
// Started
|
if (debug) {
|
||||||
if (*program.saveProgram == 1) {
|
tests();
|
||||||
printf("Ninja Virtual Machine started\n");
|
}
|
||||||
printProgram(program);
|
|
||||||
execute(program);
|
if (run) {
|
||||||
printSDA(sda);
|
printf("Ninja Virtual Machine started\n");
|
||||||
} else {
|
ObjRef s[sizeSDA];
|
||||||
printf("Error: no code file specified\n");
|
sda.size = &sizeSDA;
|
||||||
return 1;
|
sda.sda = s;
|
||||||
}
|
if (debug == 1) printProgram(program);
|
||||||
// Stopped
|
execute(program);
|
||||||
printf("Ninja Virtual Machine stopped\n");
|
printf("Ninja Virtual Machine stopped\n");
|
||||||
return 0;
|
} else {
|
||||||
|
printf("Error: no code file specified\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
program.c
45
program.c
@@ -77,6 +77,51 @@ 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 LE:
|
||||||
|
strcpy(c, "le");
|
||||||
|
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;
|
||||||
|
case PUSHR:
|
||||||
|
strcpy(c, "pushr");
|
||||||
|
break;
|
||||||
|
case POPR:
|
||||||
|
strcpy(c, "popr");
|
||||||
|
break;
|
||||||
|
case DUP:
|
||||||
|
strcpy(c, "dup");
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
strcpy(c, "ERROR");
|
strcpy(c, "ERROR");
|
||||||
break;
|
break;
|
||||||
|
|||||||
BIN
programs/nja
Executable file
BIN
programs/nja
Executable file
Binary file not shown.
28
programs/prog02.asm
Normal file
28
programs/prog02.asm
Normal 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/prog02.bin
Normal file
BIN
programs/prog02.bin
Normal file
Binary file not shown.
32
programs/prog04.asm
Normal file
32
programs/prog04.asm
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
//
|
||||||
|
// prog04.asm -- call/ret with args, and with ret value
|
||||||
|
//
|
||||||
|
|
||||||
|
asf 3
|
||||||
|
pushc 11
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
pushc 11
|
||||||
|
pushc 33
|
||||||
|
call proc
|
||||||
|
drop 2
|
||||||
|
pushr
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
pushc 33
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
rsf
|
||||||
|
halt
|
||||||
|
|
||||||
|
proc:
|
||||||
|
asf 2
|
||||||
|
pushl -3
|
||||||
|
pushl -4
|
||||||
|
sub
|
||||||
|
popr
|
||||||
|
rsf
|
||||||
|
ret
|
||||||
31
programs/prog1.asm
Normal file
31
programs/prog1.asm
Normal 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
|
||||||
31
programs/prog1.asm?inline=false
Normal file
31
programs/prog1.asm?inline=false
Normal 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
BIN
programs/prog1.bin
Normal file
Binary file not shown.
33
programs/prog2.asm
Normal file
33
programs/prog2.asm
Normal 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
BIN
programs/prog2.bin
Normal file
Binary file not shown.
9
programs/prog3.asm
Normal file
9
programs/prog3.asm
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
pushc 1
|
||||||
|
asf 2
|
||||||
|
asf 4
|
||||||
|
pushc 23
|
||||||
|
asf 3
|
||||||
|
rsf
|
||||||
|
rsf
|
||||||
|
rsf
|
||||||
|
halt
|
||||||
BIN
programs/prog3.bin
Normal file
BIN
programs/prog3.bin
Normal file
Binary file not shown.
61
programs/prog4.asm
Normal file
61
programs/prog4.asm
Normal 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
|
||||||
|
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
|
||||||
|
pushg 0
|
||||||
|
sub
|
||||||
|
popg 1
|
||||||
|
L4:
|
||||||
|
jmp L1
|
||||||
|
L2:
|
||||||
|
// writeInteger(x);
|
||||||
|
pushg 0
|
||||||
|
wrint
|
||||||
|
// writeCharacter('\n');
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
BIN
programs/prog4.bin
Normal file
BIN
programs/prog4.bin
Normal file
Binary file not shown.
63
programs/prog5.asm
Normal file
63
programs/prog5.asm
Normal 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/prog5.bin
Normal file
BIN
programs/prog5.bin
Normal file
Binary file not shown.
61
programs/prog6.asm
Normal file
61
programs/prog6.asm
Normal 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
|
||||||
|
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
|
||||||
|
pushg 0
|
||||||
|
sub
|
||||||
|
popg 1
|
||||||
|
L4:
|
||||||
|
jmp L1
|
||||||
|
L2:
|
||||||
|
// writeInteger(x);
|
||||||
|
pushg 0
|
||||||
|
wrint
|
||||||
|
// writeCharacter('\n');
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
halt
|
||||||
17
programs/prog7.asm
Normal file
17
programs/prog7.asm
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// prog04.asm -- call/ret with args, and with ret value
|
||||||
|
//
|
||||||
|
|
||||||
|
pushc 12
|
||||||
|
rdint
|
||||||
|
mul
|
||||||
|
wrint
|
||||||
|
call proc
|
||||||
|
halt
|
||||||
|
|
||||||
|
proc:
|
||||||
|
asf 1
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
rsf
|
||||||
|
ret
|
||||||
BIN
programs/prog7.bin
Normal file
BIN
programs/prog7.bin
Normal file
Binary file not shown.
48
reg.c
Executable file
48
reg.c
Executable file
@@ -0,0 +1,48 @@
|
|||||||
|
//
|
||||||
|
// Created by Nils on 29.10.2023.
|
||||||
|
//
|
||||||
|
#ifndef REG
|
||||||
|
#define REG
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct reg {
|
||||||
|
int* size;
|
||||||
|
int* current;
|
||||||
|
unsigned int *stack;
|
||||||
|
};
|
||||||
|
|
||||||
|
void printStackR(struct reg stack) {
|
||||||
|
printf("Regurn reg\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||||
|
for (int i = *stack.current-1; i > 0; --i) {
|
||||||
|
printf("||%i||\n", stack.stack[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pushR(struct reg 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int popR(struct reg s) {
|
||||||
|
if (*s.current == 0) {
|
||||||
|
printf("Stack Underflow\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
*s.current = *s.current -1;
|
||||||
|
return s.stack[*s.current];
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int peekR(struct reg s, int steps) {
|
||||||
|
if (*s.current - steps < 0) {
|
||||||
|
printf("Stack Underflow\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
return s.stack[*s.current - steps];
|
||||||
|
}
|
||||||
|
#endif
|
||||||
31
stack.c
31
stack.c
@@ -6,21 +6,32 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "stackslot.c"
|
||||||
|
|
||||||
struct stack {
|
struct stack {
|
||||||
int* size;
|
int* size;
|
||||||
int* current;
|
int* current;
|
||||||
unsigned int *stack;
|
StackSlot *stack;
|
||||||
};
|
};
|
||||||
|
|
||||||
void printStack(struct stack stack) {
|
void printStack(struct stack stack, int fp) {
|
||||||
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
|
||||||
for (int i = 0; i < *stack.size; ++i) {
|
for (int i = *stack.current -1; i >= 0; --i) {
|
||||||
printf("|%i|\n", stack.stack[i]);
|
printf("%i\t",i);
|
||||||
|
if(stack.stack[i].isObjRef){
|
||||||
|
printf("|%p|", stack.stack[i].u.objRef);
|
||||||
|
if(stack.stack[i].u.objRef->size == sizeof(int))
|
||||||
|
printf("(%i)",*(int *)stack.stack[i].u.objRef->data);
|
||||||
|
}else {
|
||||||
|
printf("|%i|", getIntValfromStackSlot(stack.stack[i]));
|
||||||
|
}
|
||||||
|
if(fp == i) printf("<-\tFP\n");
|
||||||
|
else if(*stack.current == i) printf("<-\tSP\n");
|
||||||
|
else printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(struct stack s, unsigned int value) {
|
void push(struct stack s, StackSlot value) {
|
||||||
if (*s.current >= *s.size) {
|
if (*s.current >= *s.size) {
|
||||||
printf("Stack Overflow\n");
|
printf("Stack Overflow\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -29,7 +40,7 @@ void push(struct stack s, unsigned int value) {
|
|||||||
*s.current=*s.current + 1;
|
*s.current=*s.current + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int pop(struct stack s) {
|
StackSlot pop(struct stack s) {
|
||||||
if (*s.current == 0) {
|
if (*s.current == 0) {
|
||||||
printf("Stack Underflow\n");
|
printf("Stack Underflow\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -37,4 +48,12 @@ unsigned int pop(struct stack s) {
|
|||||||
*s.current = *s.current -1;
|
*s.current = *s.current -1;
|
||||||
return s.stack[*s.current];
|
return s.stack[*s.current];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int peek(struct stack s, int steps) {
|
||||||
|
if (*s.current - steps < 0) {
|
||||||
|
printf("Stack Underflow\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
return getIntValfromStackSlot(s.stack[*s.current - steps]);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
57
stackslot.c
Normal file
57
stackslot.c
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#ifndef STACKSLOT
|
||||||
|
#define STACKSLOT
|
||||||
|
typedef int Object;
|
||||||
|
typedef struct {
|
||||||
|
unsigned int size;
|
||||||
|
unsigned char data[1];
|
||||||
|
} *ObjRef;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
bool isObjRef;
|
||||||
|
union {
|
||||||
|
ObjRef objRef;
|
||||||
|
int number;
|
||||||
|
} u;
|
||||||
|
} StackSlot;
|
||||||
|
|
||||||
|
ObjRef getIntObj(int val){
|
||||||
|
ObjRef intObject;
|
||||||
|
unsigned int objSize = sizeof(unsigned int) + sizeof(int);
|
||||||
|
if ((intObject = malloc(objSize)) == NULL) {
|
||||||
|
perror("malloc");
|
||||||
|
}
|
||||||
|
*(int *)intObject->data=val;
|
||||||
|
intObject->size=sizeof(int);
|
||||||
|
return intObject;
|
||||||
|
}
|
||||||
|
int getValFromIntObj(ObjRef iref){
|
||||||
|
return *(int *)iref->data;
|
||||||
|
}
|
||||||
|
int getIntValfromStackSlot(StackSlot s){
|
||||||
|
if(s.isObjRef){
|
||||||
|
return *(int *)s.u.objRef->data;
|
||||||
|
}
|
||||||
|
return s.u.number;
|
||||||
|
}
|
||||||
|
void setValIntObj(ObjRef iref, int val){
|
||||||
|
iref->size=sizeof(int);
|
||||||
|
*(int *)iref->data=val;
|
||||||
|
}
|
||||||
|
StackSlot stackSlotWithObjRef(ObjRef val){
|
||||||
|
StackSlot *stackSlot;
|
||||||
|
stackSlot=malloc(sizeof(StackSlot));
|
||||||
|
stackSlot->isObjRef=true;
|
||||||
|
stackSlot->u.objRef=val;
|
||||||
|
return *stackSlot;
|
||||||
|
}
|
||||||
|
StackSlot stackSlotWitchNumber(int val){
|
||||||
|
StackSlot *stackSlot;
|
||||||
|
stackSlot= malloc(sizeof(StackSlot));
|
||||||
|
stackSlot->isObjRef=false;
|
||||||
|
stackSlot->u.number=val;
|
||||||
|
return *stackSlot;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user