-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtual Machine.c
191 lines (164 loc) · 5.24 KB
/
Virtual Machine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_HEIGHT 2000
#define MAX_CODE_LENGTH 500
#define MAX_LEXI_LEVELS 3
typedef struct
{
int op;
int l;
int m;
} instruction;
int stack[MAX_STACK_HEIGHT];
int execute_instruction(instruction next, int *pc, int *bp, int *sp); // This is my completed function prototype
int base(int l, int base); // This is the prototype for the base function if Kika wants to make it
void print_a(FILE *ofp, int line, int op, int l, int m, char *opcodes); //Jacob can use these three or change them as needed
void print_e (FILE *ofp, instruction *array, int pc_a, int pc_b, int bp, int sp, int *stack, int * start);
void print_s (FILE *ofp, int *stack, int sp, int *index);
char *opcodes[] = {"", "LIT", "OPR", "LOD", "STO", "CAL", "INC", "JMP", "JPC", "SIO", "SIO", "SIO"};
int main(int argc, char * argv[])
{
int bp = 1; //base pointer
int sp = 0; //stack pointer
int pc = 0; //program counter
int ir = 0; //instruction register
int halted = 0;
instruction * array = malloc(sizeof(instruction) * MAX_CODE_LENGTH);
if(argc != 2) // i.e. if there isn't a file passed to Main
{
printf("\nError while opening the input file, EXITING PROGRAM\n");
return -1;
}
FILE* ifp = fopen(argv[1], "r"); // input file
FILE* ofp = fopen("trace.txt", "w"); // output file
int i = 0;
int j = 0;
int ic;
fprintf(ofp, "Line\tOP\t\tL\t\tM\n");
while(fscanf(ifp, "%d %d %d", &array[i].op, &array[i].l, &array[i].m) != EOF)
{
if (opcodes[array[i].op] != '\0')
print_a(ofp, i, array[i].op, array[i].l, array[i].m, opcodes[array[i].op]);
i++;
}
ic = i;
fprintf(ofp, "\n\t\t\t\t\t\t\t\tPC\t\tBP\t\tSP\t\tstack\n");
fprintf(ofp, "Initial Values\t\t\t\t\t\t\t%d\t\t%d\t\t%d\n", pc, bp, sp);
/*
Here is the Main Instruction -> Print stack loop
Above here should be all of the code loading the program into the array
*/
while (!halted)
{
halted = execute_instruction(array[pc], &pc, &bp, &sp);
/*
Jacob, this is where you will put the calls to your print function for the stack trace
*/
}
fclose(ifp);
if (halted == 2) // here we check to see if we exited the program abnormally to give an appropriate error.
{
printf("Invalid modifier for an Op 2 instruction!\n");
return 1;
} else if (halted == 3)
{
printf("Invalid modifier for an Op 9 instruction!\n");
return 1;
} else if (halted == 4)
{
printf("Invalid Op code on this instruction!\n");
return 1;
}
return 0;
}
int execute_instruction(instruction next, int *pc, int *bp, int *sp)
{
*pc++;
switch (next.op)
{
case 1: *sp++;
stack[*sp] = next.m;
break;
case 3: *sp++;
stack[*sp] = stack[base(next.l, *bp) + next.m];
break;
case 4: stack[base(next.l,*bp) + next.m] = stack[*sp];
*sp--;
break;
case 5: stack[*sp+1]=0;
stack[*sp+2]=base(next.l, *bp);
stack[*sp+3]=*bp;
stack[*sp+4]=*pc;
*bp=*sp+1;
*pc=next.m;
break;
case 6: *sp+=next.m;
break;
case 7: *pc=next.m;
break;
case 8: if (stack[*sp]==0)
*pc=next.m;
break;
case 2: switch(next.m)
{
case 0: *sp = *bp-1;
*pc = stack[*sp+4];
*bp = stack[*sp+3];
break;
case 1: stack[*sp] = -stack[*sp];
break;
case 2: *sp--;
stack[*sp] = stack[*sp] + stack[*sp+1];
break;
case 3: *sp--;
stack[*sp] = stack[*sp] - stack[*sp+1];
break;
case 4: *sp--;
stack[*sp] = stack[*sp] * stack[*sp+1];
break;
case 5: *sp--;
stack[*sp] = stack[*sp] / stack[*sp+1];
break;
case 6: stack[*sp] = stack[*sp] & 1;
break;
case 7: *sp--;
stack[*sp] = stack[*sp] % stack[*sp+1];
break;
case 8: *sp--;
stack[*sp] = stack[*sp] == stack[*sp+1];
break;
case 9: *sp--;
stack[*sp] = stack[*sp] != stack[*sp+1];
break;
case 10: *sp--;
stack[*sp] = stack[*sp] < stack[*sp+1];
break;
case 11: *sp--;
stack[*sp] = stack[*sp] <= stack[*sp+1];
break;
case 12: *sp--;
stack[*sp] = stack[*sp] > stack[*sp+1];
break;
case 13: *sp--;
stack[*sp] = stack[*sp] >= stack[*sp+1];
break;
default: return 2; //Invalid Op 2 modifier
}
break;
case 9: switch(next.m)
{
case 0: printf("%d\n", stack[*sp]);
*sp--;
break;
case 1: *sp++;
printf("./");
scanf("%d", stack[*sp]);
break;
case 2: return 1; // Halted normally
default: return 3; // Invalid Op 9 modifier
}
break;
default: return 4; // Invalid Op
}
return 0;
}