add error message if no program selected

fix error with negative numbers
This commit is contained in:
Elias Bennour 2023-12-03 14:24:22 +01:00
parent 1363db7c11
commit c16fe6349b
2 changed files with 45 additions and 36 deletions

13
njvm.c
View File

@ -5,10 +5,9 @@
#include "stack.c" #include "stack.c"
#include "program.c" #include "program.c"
//Comment to disable debug //Comment to disable debug
#define DEBUG //#define DEBUG
// Stack // Stack
struct stack stack; struct stack stack;
@ -36,7 +35,7 @@ void execute(struct program program) {
goto end; goto end;
break; break;
case PUSHC: case PUSHC:
push(stack,IMMEDIATE(program.program[i])); push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
break; break;
case ADD: case ADD:
push(stack, pop(stack) + pop(stack)); push(stack, pop(stack) + pop(stack));
@ -75,6 +74,7 @@ void execute(struct program program) {
end: end:
return; return;
} }
#ifdef DEBUG #ifdef DEBUG
void tests(void){ void tests(void){
@ -96,9 +96,11 @@ int main(int argc, char *argv[]) {
// Initialize ProgrammSpeicher // Initialize ProgrammSpeicher
int psize = 1000; int psize = 1000;
int saveProgram = 0;
unsigned int p[1000]; unsigned int p[1000];
program.size = &psize; program.size = &psize;
program.program = p; program.program = p;
program.saveProgram = &saveProgram;
#ifdef DEBUG #ifdef DEBUG
@ -124,9 +126,14 @@ int main(int argc, char *argv[]) {
} else { } else {
run: run:
// Started // Started
if (*program.saveProgram == 1) {
printf("Ninja Virtual Machine started\n"); printf("Ninja Virtual Machine started\n");
printProgram(program); printProgram(program);
execute(program); execute(program);
} else {
printf("Error: no program selected\n");
return 1;
}
// Stopped // Stopped
printf("Ninja Virtual Machine stopped\n"); printf("Ninja Virtual Machine stopped\n");
return 0; return 0;

View File

@ -11,6 +11,7 @@
struct program { struct program {
int *size; int *size;
unsigned int *program; unsigned int *program;
int *saveProgram;
}; };
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) { 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.program[i] = codeToCopy[i];
} }
*program.size = size; *program.size = size;
*program.saveProgram = 1;
} }
void printProgram(struct program program) { void printProgram(struct program program) {