Print program

This commit is contained in:
nilsplk 2023-10-26 17:26:59 +02:00
parent 4e629a6ca7
commit efe7663637

68
njvm.c
View File

@ -13,15 +13,10 @@
#define DEBUG #define DEBUG
unsigned int* programmSpeicher;
void copyToProgramm(unsigned int codeToCopy[]){
unsigned int programmSpeicher[1000]; programmSpeicher = codeToCopy;
void copyToProgramm(const unsigned int codeToCopy[], int length){
for (int i = 0; i < length; i++) {
programmSpeicher[i] = codeToCopy[i];
}
} }
// Stack // Stack
@ -66,13 +61,57 @@ void useption(int argc, char *argv[]) {
} }
} }
} }
void printProgramm(){
int i = 0;
char c[10];
while (programmSpeicher[i] != 0)
{
switch (programmSpeicher[i] >> 24) {
case PUSHC:
strcpy(c,"pushc");
break;
case ADD:
strcpy(c,"add");
break;
case SUB:
strcpy(c,"sub");
break;
case MUL:
strcpy(c,"mul");
break;
case DIV:
strcpy(c,"div");
break;
case MOD:
strcpy(c,"mod");
break;
case RDINT:
strcpy(c,"rdint");
break;
case WRINT:
strcpy(c,"wrint");
break;
case RDCHR:
strcpy(c,"rdchr");
break;
case WRCHR:
strcpy(c,"wrchr");
break;
default:
strcpy(c,"halt");
break;
}
IMMEDIATE(programmSpeicher[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(programmSpeicher[i])) : printf("%03i:\t%s\n",i,c);
i++;
}
printf("%03i:\thalt\n",i);
}
void execute(void) { void execute(void) {
int i = 0; int i = 0;
int intInput; int intInput;
int temp; int temp;
char charInput; char charInput;
while (programmSpeicher[i] != 0) { while (1) {
switch (programmSpeicher[i] >> 24) { switch (programmSpeicher[i] >> 24) {
case HALT: case HALT:
@ -104,20 +143,20 @@ void execute(void) {
stackPush(intInput); stackPush(intInput);
break; break;
case WRINT: case WRINT:
printf("%i\n",stackPop()); printf("%i",stackPop());
break; break;
case RDCHR: case RDCHR:
scanf("%c",&charInput); scanf("%c",&charInput);
stackPush(charInput); stackPush(charInput);
break; break;
case WRCHR: case WRCHR:
printf("%c\n",stackPop()); printf("%c",stackPop());
break; break;
} }
i++; i++;
} }
end: end:
printf("Finished\n"); return;
} }
#ifdef DEBUG #ifdef DEBUG
@ -132,7 +171,8 @@ void printStack(void){
void tests(void){ void tests(void){
printf("Runnig debug mode\n"); printf("Runnig debug mode\n");
copyToProgramm(code1,sizeof(code1)/sizeof(code1[0])); copyToProgramm(code1);
printProgramm();
execute(); execute();
} }