aufgabe 2 stack overflow
This commit is contained in:
parent
fb8497fbee
commit
83dae77e31
2
consts.c
2
consts.c
@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 2
|
#define VERSION 4
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
39
njvm.c
39
njvm.c
@ -88,10 +88,10 @@ void execute(struct program program) {
|
|||||||
case RSF:
|
case RSF:
|
||||||
*stack.current = fp;
|
*stack.current = fp;
|
||||||
fp = pop(stack);
|
fp = pop(stack);
|
||||||
case PUSHL:
|
case POPL:
|
||||||
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
|
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
|
||||||
break;
|
break;
|
||||||
case POPL:
|
case PUSHL:
|
||||||
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
|
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -131,41 +131,26 @@ int main(int argc, char *argv[]) {
|
|||||||
program.program = p;
|
program.program = p;
|
||||||
program.saveProgram = &saveProgram;
|
program.saveProgram = &saveProgram;
|
||||||
|
|
||||||
|
int debug = 0;
|
||||||
|
|
||||||
#ifdef DEBUG
|
if (debug){
|
||||||
tests();
|
tests();
|
||||||
#endif /* ifdef DEBUG */
|
}
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
if (strcmp(argv[1], "--version") == 0) {
|
if (strcmp(argv[1], "--version") == 0) {
|
||||||
version();
|
version();
|
||||||
} else if (strcmp(argv[1], "--help") == 0) {
|
} else if (strcmp(argv[1], "--help") == 0) {
|
||||||
help();
|
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 {
|
} 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 {
|
} 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;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
||||||
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.
Loading…
x
Reference in New Issue
Block a user