add error message if no program selected
fix error with negative numbers
This commit is contained in:
parent
1363db7c11
commit
c16fe6349b
13
njvm.c
13
njvm.c
@ -5,10 +5,9 @@
|
||||
#include "stack.c"
|
||||
#include "program.c"
|
||||
|
||||
|
||||
//Comment to disable debug
|
||||
|
||||
#define DEBUG
|
||||
//#define DEBUG
|
||||
|
||||
// Stack
|
||||
struct stack stack;
|
||||
@ -36,7 +35,7 @@ void execute(struct program program) {
|
||||
goto end;
|
||||
break;
|
||||
case PUSHC:
|
||||
push(stack,IMMEDIATE(program.program[i]));
|
||||
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||
break;
|
||||
case ADD:
|
||||
push(stack, pop(stack) + pop(stack));
|
||||
@ -75,6 +74,7 @@ void execute(struct program program) {
|
||||
end:
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
void tests(void){
|
||||
@ -96,9 +96,11 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Initialize ProgrammSpeicher
|
||||
int psize = 1000;
|
||||
int saveProgram = 0;
|
||||
unsigned int p[1000];
|
||||
program.size = &psize;
|
||||
program.program = p;
|
||||
program.saveProgram = &saveProgram;
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
@ -124,9 +126,14 @@ int main(int argc, char *argv[]) {
|
||||
} else {
|
||||
run:
|
||||
// Started
|
||||
if (*program.saveProgram == 1) {
|
||||
printf("Ninja Virtual Machine started\n");
|
||||
printProgram(program);
|
||||
execute(program);
|
||||
} else {
|
||||
printf("Error: no program selected\n");
|
||||
return 1;
|
||||
}
|
||||
// Stopped
|
||||
printf("Ninja Virtual Machine stopped\n");
|
||||
return 0;
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
struct program {
|
||||
int *size;
|
||||
unsigned int *program;
|
||||
int *saveProgram;
|
||||
};
|
||||
|
||||
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) {
|
||||
@ -18,6 +19,7 @@ void copyToProgram(const unsigned int codeToCopy[], int size, struct program pro
|
||||
program.program[i] = codeToCopy[i];
|
||||
}
|
||||
*program.size = size;
|
||||
*program.saveProgram = 1;
|
||||
}
|
||||
|
||||
void printProgram(struct program program) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user