fix jmp not working
fix arithmetic operations add prog3 and prog4 add debug to instructions
This commit is contained in:
parent
77cae7467e
commit
0b042fb912
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
|
||||||
|
|||||||
109
njvm.c
109
njvm.c
@ -12,6 +12,7 @@ struct program *program;
|
|||||||
|
|
||||||
// SDA
|
// SDA
|
||||||
unsigned 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", 0, __DATE__, __TIME__);
|
||||||
@ -41,57 +42,78 @@ void execute(struct program *program, struct sda *sda) {
|
|||||||
|
|
||||||
for (int i = 0; i < program->size; ++i) {
|
for (int i = 0; i < program->size; ++i) {
|
||||||
unsigned int instruction = program->program[i];
|
unsigned int instruction = program->program[i];
|
||||||
|
if (i >= program->size) {
|
||||||
|
printf("Error: Jump out of program memory\n");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
switch (instruction >> 24) {
|
switch (instruction >> 24) {
|
||||||
case HALT:
|
case HALT:
|
||||||
goto end;
|
goto end;
|
||||||
case PUSHC:
|
case PUSHC:
|
||||||
|
if (debug == 1) printf("PUSHC %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
|
push(&stack, SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
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:
|
||||||
tmp = pop(&stack);
|
tmp = pop(&stack);
|
||||||
|
if (debug == 1) printf("SUB\n");
|
||||||
|
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||||
push(&stack, pop(&stack) - tmp);
|
push(&stack, pop(&stack) - tmp);
|
||||||
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:
|
||||||
tmp = pop(&stack);
|
tmp = pop(&stack);
|
||||||
|
if (debug == 1) printf("DIV\n");
|
||||||
|
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||||
push(&stack, pop(&stack) / tmp);
|
push(&stack, pop(&stack) / tmp);
|
||||||
break;
|
break;
|
||||||
case MOD:
|
case MOD:
|
||||||
tmp = pop(&stack);
|
tmp = pop(&stack);
|
||||||
|
if (debug == 1) printf("MOD\n");
|
||||||
|
if (debug == 1) printf("tmp: %d\n", tmp);
|
||||||
push(&stack, pop(&stack) % tmp);
|
push(&stack, pop(&stack) % tmp);
|
||||||
break;
|
break;
|
||||||
case RDINT:
|
case RDINT:
|
||||||
scanf("%i", &intInput);
|
scanf("%i", &intInput);
|
||||||
|
if (debug == 1) printf("RDINT %d\n", 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:
|
||||||
scanf("%c", &charInput);
|
scanf("%c", &charInput);
|
||||||
|
if (debug == 1) printf("RDCHR %c\n", 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 %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
currentFrame = stack.frames[stack.currentFrame];
|
currentFrame = stack.frames[stack.currentFrame];
|
||||||
currentFrame->bp = currentFrame->sp;
|
currentFrame->bp = currentFrame->sp;
|
||||||
|
|
||||||
*currentFrame->sp++ = getSDA(SIGN_EXTEND(IMMEDIATE(instruction)), sda);
|
*currentFrame->sp++ = getSDA(SIGN_EXTEND(IMMEDIATE(instruction)), sda);
|
||||||
break;
|
break;
|
||||||
case POPG:
|
case POPG:
|
||||||
|
if (debug == 1) printf("POPG %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
currentFrame = stack.frames[stack.currentFrame];
|
currentFrame = stack.frames[stack.currentFrame];
|
||||||
currentFrame->bp = currentFrame->sp;
|
currentFrame->bp = currentFrame->sp;
|
||||||
|
|
||||||
setSDA(SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack), sda);
|
setSDA(SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack), sda);
|
||||||
break;
|
break;
|
||||||
case ASF:
|
case ASF:
|
||||||
|
if (debug == 1) printf("ASF %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
currentFrame = stack.frames[stack.currentFrame];
|
currentFrame = stack.frames[stack.currentFrame];
|
||||||
push(&stack, *currentFrame->sp);
|
push(&stack, *currentFrame->sp);
|
||||||
*currentFrame->sp = stack.currentFrame;
|
*currentFrame->sp = stack.currentFrame;
|
||||||
@ -100,47 +122,106 @@ void execute(struct program *program, struct sda *sda) {
|
|||||||
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
|
stack.currentFrame += SIGN_EXTEND(IMMEDIATE(instruction));
|
||||||
break;
|
break;
|
||||||
case RSF:
|
case RSF:
|
||||||
|
if (debug == 1) printf("RSF\n");
|
||||||
stack.currentFrame = pop(&stack);
|
stack.currentFrame = pop(&stack);
|
||||||
currentFrame = stack.frames[stack.currentFrame];
|
currentFrame = stack.frames[stack.currentFrame];
|
||||||
currentFrame->bp = NULL;
|
currentFrame->bp = NULL;
|
||||||
break;
|
break;
|
||||||
case PUSHL:
|
case PUSHL:
|
||||||
|
if (debug == 1) printf("PUSHL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
currentFrame = stack.frames[stack.currentFrame];
|
currentFrame = stack.frames[stack.currentFrame];
|
||||||
push(&stack, currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))]);
|
push(&stack, currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))]);
|
||||||
break;
|
break;
|
||||||
case POPL:
|
case POPL:
|
||||||
|
if (debug == 1) printf("POPL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
currentFrame = stack.frames[stack.currentFrame];
|
currentFrame = stack.frames[stack.currentFrame];
|
||||||
currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))] = pop(&stack);
|
currentFrame->fp[SIGN_EXTEND(IMMEDIATE(instruction))] = pop(&stack);
|
||||||
break;
|
break;
|
||||||
case EQ:
|
case EQ:
|
||||||
push(&stack,pop(&stack)==pop(&stack));
|
if (debug == 1) printf("EQ\n");
|
||||||
|
if (pop(&stack) == pop(&stack)) {
|
||||||
|
push(&stack, 1);
|
||||||
|
} else {
|
||||||
|
push(&stack, 0);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case NE:
|
case NE:
|
||||||
push(&stack,pop(&stack)!=pop(&stack));
|
if (debug == 1) printf("NE\n");
|
||||||
|
if (pop(&stack) != pop(&stack)) {
|
||||||
|
push(&stack, 1);
|
||||||
|
} else {
|
||||||
|
push(&stack, 0);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case LT:
|
case LT:
|
||||||
push(&stack, pop(&stack)<pop(&stack));
|
if (debug == 1) printf("LT\n");
|
||||||
|
tmp = pop(&stack);
|
||||||
|
if (pop(&stack) < tmp) {
|
||||||
|
push(&stack, 1);
|
||||||
|
} else {
|
||||||
|
push(&stack, 0);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case LE:
|
case LE:
|
||||||
push(&stack,pop(&stack)<=pop(&stack));
|
if (debug == 1) printf("LE\n");
|
||||||
|
tmp = pop(&stack);
|
||||||
|
if (pop(&stack) <= tmp) {
|
||||||
|
push(&stack, 1);
|
||||||
|
} else {
|
||||||
|
push(&stack, 0);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case GT:
|
case GT:
|
||||||
push(&stack, pop(&stack)>pop(&stack));
|
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;
|
break;
|
||||||
case GE:
|
case GE:
|
||||||
push(&stack,pop(&stack)>=pop(&stack));
|
if (debug == 1) printf("GE\n");
|
||||||
|
tmp = pop(&stack);
|
||||||
|
if (pop(&stack) >= tmp) {
|
||||||
|
push(&stack, 1);
|
||||||
|
} else {
|
||||||
|
push(&stack, 0);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case JMP:
|
case JMP:
|
||||||
i = SIGN_EXTEND(IMMEDIATE(instruction));
|
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;
|
break;
|
||||||
case BRF:
|
case BRF: // branch on false
|
||||||
|
if (debug == 1) printf("BRF %d\nStack: %d\n", SIGN_EXTEND(IMMEDIATE(instruction)), pop(&stack));
|
||||||
if (pop(&stack) == 0) {
|
if (pop(&stack) == 0) {
|
||||||
i = SIGN_EXTEND(IMMEDIATE(instruction));
|
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;
|
break;
|
||||||
case BRT:
|
case BRT:
|
||||||
|
if (debug == 1) printf("BRT %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
if (pop(&stack) == 1) {
|
if (pop(&stack) == 1) {
|
||||||
i = SIGN_EXTEND(IMMEDIATE(instruction));
|
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;
|
break;
|
||||||
}
|
}
|
||||||
@ -153,12 +234,12 @@ void execute(struct program *program, struct sda *sda) {
|
|||||||
// run prog2.bin
|
// run prog2.bin
|
||||||
void tests(void) {
|
void tests(void) {
|
||||||
printf("Test started\n");
|
printf("Test started\n");
|
||||||
struct sda *sda = malloc(sizeof(struct sda));
|
/*struct sda *sda = malloc(sizeof(struct sda));
|
||||||
unsigned int s[1000];
|
unsigned int s[1000];
|
||||||
sda->size = 1000;
|
sda->size = 1000;
|
||||||
sda->sda = s;
|
sda->sda = s;
|
||||||
fromFile("prog2.bin", program);
|
fromFile("prog2.bin", program);
|
||||||
execute(program, (struct sda *) &sda);
|
execute(program, (struct sda *) &sda);*/
|
||||||
printf("Test finished\n");
|
printf("Test finished\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +258,6 @@ int main(int argc, char *argv[]) {
|
|||||||
program->program = p;
|
program->program = p;
|
||||||
|
|
||||||
// Initialize runtime variables
|
// Initialize runtime variables
|
||||||
int debug = 0;
|
|
||||||
int run = 0;
|
int run = 0;
|
||||||
int sizeSDA;
|
int sizeSDA;
|
||||||
|
|
||||||
@ -214,9 +294,8 @@ int main(int argc, char *argv[]) {
|
|||||||
unsigned int s[sizeSDA];
|
unsigned int s[sizeSDA];
|
||||||
sda->size = sizeSDA;
|
sda->size = sizeSDA;
|
||||||
sda->sda = s;
|
sda->sda = s;
|
||||||
printProgram(program);
|
if (debug == 1) printProgram(program);
|
||||||
execute(program, (struct sda *) &sda);
|
execute(program, (struct sda *) &sda);
|
||||||
if (debug == 1) printSDA(sda);
|
|
||||||
printf("Ninja Virtual Machine stopped\n");
|
printf("Ninja Virtual Machine stopped\n");
|
||||||
} else {
|
} else {
|
||||||
printf("Error: no code file specified\n");
|
printf("Error: no code file specified\n");
|
||||||
|
|||||||
Binary file not shown.
61
programs/prog3.asm
Normal file
61
programs/prog3.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 // 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
BIN
programs/prog3.bin
Normal file
Binary file not shown.
63
programs/prog4.asm
Normal file
63
programs/prog4.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/prog4.bin
Normal file
BIN
programs/prog4.bin
Normal file
Binary file not shown.
5
stack.c
5
stack.c
@ -38,4 +38,9 @@ int pop(struct stack *stack) {
|
|||||||
return *--currentFrame->sp;
|
return *--currentFrame->sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user