Commit with mistake
This commit is contained in:
parent
7390df9ee0
commit
33a2994c29
2
consts.c
2
consts.c
@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 2
|
#define VERSION 3
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
@ -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 */
|
||||||
|
|||||||
87
njvm.c
87
njvm.c
@ -7,9 +7,8 @@
|
|||||||
#include "codeReader.c"
|
#include "codeReader.c"
|
||||||
#include "SDA.c"
|
#include "SDA.c"
|
||||||
|
|
||||||
//Comment to disable debug
|
// Debug
|
||||||
|
int debug = 1;
|
||||||
#define DEBUG
|
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
@ -38,80 +37,135 @@ void execute(struct program program) {
|
|||||||
for (i = 0; i < *program.size; ++i) {
|
for (i = 0; i < *program.size; ++i) {
|
||||||
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:
|
||||||
|
if(debug == 1) printf("pushc\n");
|
||||||
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
break;
|
break;
|
||||||
case ADD:
|
case ADD:
|
||||||
|
if(debug == 1) printf("add\n");
|
||||||
push(stack, pop(stack) + pop(stack));
|
push(stack, pop(stack) + pop(stack));
|
||||||
break;
|
break;
|
||||||
case SUB:
|
case SUB:
|
||||||
|
if(debug == 1) printf("sub\n");
|
||||||
temp = pop(stack);
|
temp = pop(stack);
|
||||||
push(stack, pop(stack) - temp);
|
push(stack, pop(stack) - temp);
|
||||||
break;
|
break;
|
||||||
case MUL:
|
case MUL:
|
||||||
|
if(debug == 1) printf("mul\n");
|
||||||
push(stack, pop(stack) * pop(stack));
|
push(stack, pop(stack) * pop(stack));
|
||||||
break;
|
break;
|
||||||
case DIV:
|
case DIV:
|
||||||
|
if(debug == 1) printf("div\n");
|
||||||
temp = pop(stack);
|
temp = pop(stack);
|
||||||
push(stack, pop(stack) / temp);
|
push(stack, pop(stack) / temp);
|
||||||
break;
|
break;
|
||||||
case MOD:
|
case MOD:
|
||||||
|
if(debug == 1) printf("mod\n");
|
||||||
temp = pop(stack);
|
temp = pop(stack);
|
||||||
push(stack, pop(stack) % temp);
|
push(stack, pop(stack) % temp);
|
||||||
break;
|
break;
|
||||||
case RDINT:
|
case RDINT:
|
||||||
|
if(debug == 1) printf("rdint\n");
|
||||||
scanf("%i", &intInput);
|
scanf("%i", &intInput);
|
||||||
push(stack, intInput);
|
push(stack, intInput);
|
||||||
break;
|
break;
|
||||||
case WRINT:
|
case WRINT:
|
||||||
|
if(debug == 1) printf("wrint\n");
|
||||||
printf("%i", pop(stack));
|
printf("%i", 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, charInput);
|
||||||
break;
|
break;
|
||||||
case WRCHR:
|
case WRCHR:
|
||||||
|
if(debug == 1) printf("wrchr\n");
|
||||||
printf("%c", pop(stack));
|
printf("%c", pop(stack));
|
||||||
break;
|
break;
|
||||||
case PUSHG:
|
case PUSHG:
|
||||||
|
if(debug == 1) printf("pushg\n");
|
||||||
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
|
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
|
||||||
break;
|
break;
|
||||||
case POPG:
|
case POPG:
|
||||||
|
if(debug == 1) printf("popg\n");
|
||||||
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
|
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
|
||||||
break;
|
break;
|
||||||
case ASF:
|
case ASF:
|
||||||
|
if(debug == 1) printf("asf\n");
|
||||||
push(stack, *stack.current);
|
push(stack, *stack.current);
|
||||||
fp = *stack.current - 1;
|
fp = *stack.current - 1;
|
||||||
*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\n");
|
||||||
*stack.current = fp + 2;
|
*stack.current = fp + 2;
|
||||||
fp = pop(stack);
|
fp = pop(stack);
|
||||||
case POPL:
|
case POPL:
|
||||||
|
if(debug == 1) printf("popl\n");
|
||||||
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 PUSHL:
|
case PUSHL:
|
||||||
|
if(debug == 1) printf("pushl\n");
|
||||||
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\n");
|
||||||
|
if (pop(stack) != pop(stack)) push(stack, 1);
|
||||||
|
else push(stack, 0);
|
||||||
|
break;
|
||||||
|
case EQ:
|
||||||
|
if(debug == 1) printf("eq\n");
|
||||||
|
if (pop(stack) == pop(stack)) push(stack, 1);
|
||||||
|
else push(stack, 0);
|
||||||
|
break;
|
||||||
|
case LT:
|
||||||
|
if(debug == 1) printf("lt\n");
|
||||||
|
temp = pop(stack);
|
||||||
|
if (pop(stack) < temp) push(stack, 1);
|
||||||
|
else push(stack, 0);
|
||||||
|
break;
|
||||||
|
case LE:
|
||||||
|
if(debug == 1) printf("le\n");
|
||||||
|
temp = pop(stack);
|
||||||
|
if (pop(stack) <= temp) push(stack, 1);
|
||||||
|
else push(stack, 0);
|
||||||
|
break;
|
||||||
|
case GT:
|
||||||
|
if(debug == 1) printf("gt\n");
|
||||||
|
temp = pop(stack);
|
||||||
|
if (pop(stack) > temp) push(stack, 1);
|
||||||
|
else push(stack, 0);
|
||||||
|
break;
|
||||||
|
case GE:
|
||||||
|
if(debug == 1) printf("ge\n");
|
||||||
|
temp = pop(stack);
|
||||||
|
if (pop(stack) >= temp) push(stack, 1);
|
||||||
|
else push(stack, 0);
|
||||||
|
break;
|
||||||
|
case BRF:
|
||||||
|
if(debug == 1) printf("brf\n");
|
||||||
|
if(pop(stack)==0)
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
|
break;
|
||||||
|
case BRT:
|
||||||
|
if(debug == 1) printf("brt\n");
|
||||||
|
if(pop(stack)==1)
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
|
case JMP:
|
||||||
|
if(debug == 1) printf("jmp\n");
|
||||||
|
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
|
||||||
|
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
|
||||||
@ -130,8 +184,11 @@ int main(int argc, char *argv[]) {
|
|||||||
program.program = p;
|
program.program = p;
|
||||||
program.saveProgram = &saveProgram;
|
program.saveProgram = &saveProgram;
|
||||||
|
|
||||||
int debug = 0;
|
if (argc > 2) {
|
||||||
|
if (strcmp(argv[2], "--debug")) {
|
||||||
|
debug = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (debug){
|
if (debug){
|
||||||
tests();
|
tests();
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
programs/nja
BIN
programs/nja
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
|
||||||
Loading…
x
Reference in New Issue
Block a user