Operations now work

This commit is contained in:
nilsplk 2023-10-21 21:09:01 +02:00
parent 9f7b69e5f7
commit 62ede37645

41
njvm.c
View File

@ -68,35 +68,48 @@ void useption(int argc, char *argv[]) {
void execute(void) { void execute(void) {
int i = 0; int i = 0;
int intInput;
int temp;
char charInput;
while (programmSpeicher[i] != 0) { while (programmSpeicher[i] != 0) {
switch (programmSpeicher[i] >> 24) { switch (programmSpeicher[i] >> 24) {
case HALT: case HALT:
break; break;
case PUSHC: case PUSHC:
stackPush(IMMEDIATE(programmSpeicher[i]));
break; break;
case ADD: case ADD:
stackPush(stackPop()+stackPop()); stackPush(stackPop()+stackPop());
break; break;
case SUB: case SUB:
stackPush(stackPop()-stackPop()); temp = stackPop();
stackPush(stackPop() - temp);
break; break;
case MUL: case MUL:
stackPush(stackPop()*stackPop()); stackPush(stackPop()*stackPop());
break; break;
case DIV: case DIV:
stackPush(stackPop()/stackPop()); temp = stackPop();
stackPush(stackPop()/temp);
break; break;
case MOD: case MOD:
stackPush(stackPop()%stackPop()); temp = stackPop();
stackPush(stackPop()%temp);
break; break;
case RDINT: case RDINT:
scanf("%i",&intInput);
stackPush(intInput);
break; break;
case WRINT: case WRINT:
printf("%i\n",stackPop());
break; break;
case RDCHR: case RDCHR:
scanf("%c",&charInput);
stackPush(charInput);
break; break;
case WRCHR: case WRCHR:
printf("%c\n",stackPop());
break; break;
} }
i++; i++;
@ -104,8 +117,28 @@ void execute(void) {
} }
#ifdef DEBUG #ifdef DEBUG
void printStck(){
if(current >= 0) {return;}
printf("----\n");
for (int i = 0; i < current; i++) {
printf("|%i| \n",stack[i]);
}
printf("----\n");
}
void tests(){ void tests(){
printf("test\n"); printf("Runnig debug mode\n");
stackPush(10);
stackPush(15);
stackPush(13);
printStck();
current = 0;
programmSpeicher[0] = (RDINT << 24);
programmSpeicher[1] = (RDINT << 24);
programmSpeicher[2] = (MOD << 24);
programmSpeicher[3] = (WRINT << 24);
execute();
printStck();
} }
#endif /* ifdef DEBUG */ #endif /* ifdef DEBUG */