add call and ret function
add drop (idk if it is working right) add prog5 and prog6
This commit is contained in:
parent
0b042fb912
commit
4994bd6071
2
consts.c
2
consts.c
@ -1,6 +1,6 @@
|
|||||||
#ifndef CONSTS
|
#ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
#define VERSION 3
|
#define VERSION 4
|
||||||
|
|
||||||
#endif /* ifndef CONSTS
|
#endif /* ifndef CONSTS
|
||||||
#define CONSTS
|
#define CONSTS
|
||||||
|
|||||||
@ -31,5 +31,8 @@
|
|||||||
#define JMP 23
|
#define JMP 23
|
||||||
#define BRF 24
|
#define BRF 24
|
||||||
#define BRT 25
|
#define BRT 25
|
||||||
|
#define CALL 26
|
||||||
|
#define RET 27
|
||||||
|
#define DROP 28
|
||||||
|
|
||||||
#endif /* ifndef INSREUKTION */
|
#endif /* ifndef INSREUKTION */
|
||||||
|
|||||||
47
njvm.c
47
njvm.c
@ -11,7 +11,7 @@
|
|||||||
struct program *program;
|
struct program *program;
|
||||||
|
|
||||||
// SDA
|
// SDA
|
||||||
unsigned fp;
|
int fp;
|
||||||
int debug = 0;
|
int debug = 0;
|
||||||
|
|
||||||
void version(void) {
|
void version(void) {
|
||||||
@ -26,16 +26,28 @@ void execute(struct program *program, struct sda *sda) {
|
|||||||
struct stack stack;
|
struct stack stack;
|
||||||
stack.size = 1000;
|
stack.size = 1000;
|
||||||
stack.currentFrame = 0;
|
stack.currentFrame = 0;
|
||||||
|
struct stack callStack;
|
||||||
|
callStack.size = 1000;
|
||||||
|
callStack.currentFrame = 0;
|
||||||
|
|
||||||
for (int i = 0; i < stack.size; ++i) {
|
for (int i = 0; i < stack.size; ++i) {
|
||||||
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
||||||
frame->fp = malloc(sizeof(unsigned int) * stack.size);
|
frame->fp = malloc(sizeof(int) * stack.size);
|
||||||
frame->sp = malloc(sizeof(unsigned int) * stack.size);
|
frame->sp = malloc(sizeof(int) * stack.size);
|
||||||
frame->bp = NULL;
|
frame->bp = NULL;
|
||||||
stack.frames[i] = frame;
|
stack.frames[i] = frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < callStack.size; ++i) {
|
||||||
|
struct stackFrame *frame = malloc(sizeof(struct stackFrame));
|
||||||
|
frame->fp = malloc(sizeof(int) * stack.size);
|
||||||
|
frame->sp = malloc(sizeof(int) * stack.size);
|
||||||
|
frame->bp = NULL;
|
||||||
|
callStack.frames[i] = frame;
|
||||||
|
}
|
||||||
|
|
||||||
struct stackFrame *currentFrame = NULL;
|
struct stackFrame *currentFrame = NULL;
|
||||||
|
//struct stackFrame *currentCallFrame = NULL;
|
||||||
unsigned int tmp;
|
unsigned int tmp;
|
||||||
int intInput;
|
int intInput;
|
||||||
char charInput;
|
char charInput;
|
||||||
@ -130,7 +142,9 @@ void execute(struct program *program, struct sda *sda) {
|
|||||||
case PUSHL:
|
case PUSHL:
|
||||||
if (debug == 1) printf("PUSHL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
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))]);
|
currentFrame->bp = currentFrame->sp;
|
||||||
|
int x = SIGN_EXTEND(IMMEDIATE(instruction));
|
||||||
|
push(&stack, currentFrame->fp[x]);
|
||||||
break;
|
break;
|
||||||
case POPL:
|
case POPL:
|
||||||
if (debug == 1) printf("POPL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
if (debug == 1) printf("POPL %d\n", SIGN_EXTEND(IMMEDIATE(instruction)));
|
||||||
@ -224,6 +238,31 @@ void execute(struct program *program, struct sda *sda) {
|
|||||||
i = j;
|
i = j;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case CALL:
|
||||||
|
tmp = SIGN_EXTEND(IMMEDIATE(program->program[i]));
|
||||||
|
if (j-- >= program->size) {
|
||||||
|
printf("Error: Call out of program memory\n");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
push(&callStack, i + 1);
|
||||||
|
i = tmp;
|
||||||
|
break;
|
||||||
|
case RET:
|
||||||
|
if (debug == 1) printf("RET\n");
|
||||||
|
tmp = pop(&callStack);
|
||||||
|
if (tmp-- >= program->size) {
|
||||||
|
printf("Error: Return out of program memory\n");
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
i = tmp;
|
||||||
|
break;
|
||||||
|
case DROP:
|
||||||
|
tmp = SIGN_EXTEND(IMMEDIATE(instruction));
|
||||||
|
if (debug == 1) printf("DROP %d\n", tmp);
|
||||||
|
for (int b = 0; b < tmp; ++b) {
|
||||||
|
pop(&stack);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -99,6 +99,15 @@ void printProgram(struct program *program) {
|
|||||||
case BRT:
|
case BRT:
|
||||||
strcpy(c,"brt");
|
strcpy(c,"brt");
|
||||||
break;
|
break;
|
||||||
|
case CALL:
|
||||||
|
strcpy(c,"call");
|
||||||
|
break;
|
||||||
|
case RET:
|
||||||
|
strcpy(c,"ret");
|
||||||
|
break;
|
||||||
|
case DROP:
|
||||||
|
strcpy(c,"drop");
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
strcpy(c, "ERROR");
|
strcpy(c, "ERROR");
|
||||||
break;
|
break;
|
||||||
|
|||||||
35
programs/prog5.asm
Normal file
35
programs/prog5.asm
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
//
|
||||||
|
// prog01.asm -- call/ret without args, and without ret value
|
||||||
|
//
|
||||||
|
|
||||||
|
asf 3
|
||||||
|
pushc 11
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
call proc
|
||||||
|
pushc 44
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
rsf
|
||||||
|
halt
|
||||||
|
|
||||||
|
proc:
|
||||||
|
asf 2
|
||||||
|
pushc 22
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
call proctwo
|
||||||
|
rsf
|
||||||
|
ret
|
||||||
|
|
||||||
|
proctwo:
|
||||||
|
asf 2
|
||||||
|
pushc 33
|
||||||
|
wrint
|
||||||
|
pushc '\n'
|
||||||
|
wrchr
|
||||||
|
rsf
|
||||||
|
ret
|
||||||
BIN
programs/prog5.bin
Normal file
BIN
programs/prog5.bin
Normal file
Binary file not shown.
28
programs/prog6.asm
Normal file
28
programs/prog6.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/prog6.bin
Normal file
BIN
programs/prog6.bin
Normal file
Binary file not shown.
14
stack.c
14
stack.c
@ -10,9 +10,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct stackFrame {
|
struct stackFrame {
|
||||||
unsigned int *fp; // Frame pointer
|
int *fp; // Frame pointer
|
||||||
unsigned int *sp; // Stack pointer
|
int *sp; // Stack pointer
|
||||||
unsigned int *bp; // Base pointer
|
int *bp; // Base pointer
|
||||||
};
|
};
|
||||||
|
|
||||||
struct stack {
|
struct stack {
|
||||||
@ -28,14 +28,16 @@ void printStack(struct stack *stack) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(struct stack *stack, int value) {
|
void push(struct stack *stack, unsigned int value) {
|
||||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||||
*currentFrame->sp++ = value;
|
*(currentFrame->sp) = value;
|
||||||
|
currentFrame->sp++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pop(struct stack *stack) {
|
int pop(struct stack *stack) {
|
||||||
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
struct stackFrame *currentFrame = stack->frames[stack->currentFrame];
|
||||||
return *--currentFrame->sp;
|
currentFrame->sp--;
|
||||||
|
return *(currentFrame->sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
int peek(struct stack *stack, int steps) { // peek is pop without removing the value
|
int peek(struct stack *stack, int steps) { // peek is pop without removing the value
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user