aufgabe 2 stack overflow

This commit is contained in:
Nils Polek 2023-12-09 19:45:15 +01:00
parent fb8497fbee
commit 83dae77e31
8 changed files with 77 additions and 28 deletions

View File

@ -1,6 +1,6 @@
#ifndef CONSTS
#define CONSTS
#define VERSION 2
#define VERSION 4
#endif /* ifndef CONSTS
#define CONSTS

0
nja Normal file → Executable file
View File

39
njvm.c
View File

@ -88,10 +88,10 @@ void execute(struct program program) {
case RSF:
*stack.current = fp;
fp = pop(stack);
case PUSHL:
case POPL:
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
break;
case POPL:
case PUSHL:
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
break;
}
@ -131,41 +131,26 @@ int main(int argc, char *argv[]) {
program.program = p;
program.saveProgram = &saveProgram;
int debug = 0;
#ifdef DEBUG
if (debug){
tests();
#endif /* ifdef DEBUG */
}
if (argc > 1) {
if (strcmp(argv[1], "--version") == 0) {
version();
} else if (strcmp(argv[1], "--help") == 0) {
help();
} else if (strcmp(argv[1], "--prog1") == 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 {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
int temp = fromFile(argv[1], program);
int sizeSDA = temp;
unsigned int s[sizeSDA];
sda.size = &temp;
sda.sda = s;
execute(program);
}
} else {
run:
// Started
if (*program.saveProgram == 1) {
printf("Ninja Virtual Machine started\n");
printProgram(program);
execute(program);
printSDA(sda);
} else {
printf("Error: no code file specified\n");
return 1;
}
// Stopped
printf("Ninja Virtual Machine stopped\n");
return 0;
}
}

BIN
prog1.bin

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.