/
instructions.c
365 lines (321 loc) · 7.14 KB
/
instructions.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* Author: Adam Van Hine
* Username: vanhineam
*
* Description: The instruction table and instruction logic for the WIC
* Interpreter. This is where we define the structs for the instruction type
* and the instruction table. We also add the ability to print the tables
* at any time.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "instructions.h"
#include "table.h"
#include "stack.h"
#define INSTR_TABLE_SIZE 100
typedef struct {
char opcode[OPCODE_SIZE];
char operand[OPERAND_SIZE];
} instructionType;
typedef struct
{
instructionType instructions[INSTR_TABLE_SIZE];
int instructionCount;
} instructionTable;
// These 4 are "private" making them only accessible via public function
static instructionTable instrTable;
static tableType symbolTable; //for variables and values
static tableType jumpTable; //for labels
static stackType stack; //for stack operations
// This one is public so main can use it.
void initialize()
{
instrTable.instructionCount = 0;
initializeTable(&symbolTable);
initializeTable(&jumpTable);
initializeStack(&stack);
}
void printSymbolTable()
{
printTable(&symbolTable);
}
void printInstructionTable()
{
int i;
for(i = 0; i < instrTable.instructionCount; i++)
{
if(hasOperand(instrTable.instructions[i].opcode))
{
printf("%s %s\n", instrTable.instructions[i].opcode,
instrTable.instructions[i].operand);
}
else
{
printf("%s\n", instrTable.instructions[i].opcode);
}
}
}
int fetchInstruction(int address, char * opcode, char * operand)
{
if(address > instrTable.instructionCount)
{
printf("Address is outside of range\n");
exit(1);
}
strncpy(opcode, instrTable.instructions[address].opcode, OPCODE_SIZE);
strncpy(operand, instrTable.instructions[address].operand, OPERAND_SIZE);
return 1;
}
void insertInstruction(int address, char * opcode, char * operand)
{
instructionType instr;
if(strcmp(operand, "label") == 0)
{
store(&jumpTable, opcode, address);
strncpy(instr.opcode, "nop", OPCODE_SIZE);
operand[0] = 0;
strncpy(instr.operand, operand, OPERAND_SIZE);
}
else
{
strncpy(instr.opcode, opcode, OPCODE_SIZE);
strncpy(instr.operand, operand, OPERAND_SIZE);
}
instrTable.instructions[address] = instr;
instrTable.instructionCount++;
}
int hasOperand(char * opcode)
{
if(strcmp(opcode, "get") == 0){return 1;}
else if(strcmp(opcode, "put") == 0){return 1;}
else if(strcmp(opcode, "push") == 0){return 1;}
else if(strcmp(opcode, "pop") == 0){return 1;}
else if(strcmp(opcode, "j") == 0){return 1;}
else if(strcmp(opcode, "jf") == 0){return 1;}
else if (strcmp(opcode, "nop") == 0){return 0;}
else if (strcmp(opcode, "add") == 0){return 0;}
else if (strcmp(opcode, "sub") == 0){return 0;}
else if (strcmp(opcode, "mul") == 0){return 0;}
else if (strcmp(opcode, "div") == 0){return 0;}
else if (strcmp(opcode, "and") == 0){return 0;}
else if (strcmp(opcode, "not") == 0){return 0;}
else if (strcmp(opcode, "or") == 0){return 0;}
else if (strcmp(opcode, "tsteq") == 0){return 0;}
else if (strcmp(opcode, "tstne") == 0){return 0;}
else if (strcmp(opcode, "tstlt") == 0){return 0;}
else if (strcmp(opcode, "tstle") == 0){return 0;}
else if (strcmp(opcode, "tstgt") == 0){return 0;}
else if (strcmp(opcode, "tstge") == 0){return 0;}
else if (strcmp(opcode, "halt") == 0){return 0;}
else { return 1; } // must be a label!!
}
//**********************************************************************
// Begin opcode function section.
//**********************************************************************
//Functions for various instructions go here.
int nop(int pc) { return pc + 1; }
int add(int pc){
int rop = stackPop(&stack);
int lop = stackPop(&stack);
stackPush(&stack, lop + rop);
return pc + 1;
}
int divide(int pc)
{
int rop = stackPop(&stack);
int lop = stackPop(&stack);
if(rop == 0)
{
fprintf(stderr, "Can't divide by 0\n");
exit(0);
}
else
{
stackPush(&stack, lop/rop);
}
return pc + 1;
}
int sub(int pc)
{
int rop = stackPop(&stack);
int lop = stackPop(&stack);
stackPush(&stack, lop - rop);
return pc + 1;
}
int mul(int pc)
{
int rop = stackPop(&stack);
int lop = stackPop(&stack);
stackPush(&stack, lop * rop);
return pc + 1;
}
int get(int pc, char * operand)
{
int val = 0;
char numstring[10];
char * endptr;
printf("enter %s > ", operand);
fgets(numstring, 10, stdin);
val = strtol(numstring, &endptr, 10);
if(*endptr != '\n')
{
printf("Some characters in the string are not base 10 digits.\n");
while(*endptr !='\n')
{
endptr++;
}
get(pc, operand);
}
else
{
store(&symbolTable, operand, val);
}
return pc + 1;
}
int put(int pc, char * operand)
{
int val = 0;
val = retrieve(&symbolTable, operand);
printf("%s = %d\n", operand, val);
return pc + 1;
}
int push(int pc, char * operand)
{
int numResult;
if(isdigit(*operand))
{
numResult = atoi(operand);
stackPush(&stack, numResult);
}
else
{
numResult = retrieve(&symbolTable, operand);
stackPush(&stack, numResult);
}
return pc + 1;
}
int pop(int pc, char * operand)
{
int var = stackPop(&stack);
store(&symbolTable, operand, var);
return pc + 1;
}
int not(int pc)
{
int val;
val = stackPop(&stack);
stackPush(&stack, !val);
return pc + 1;
}
int and(int pc)
{
int rop = stackPop(&stack);
int lop = stackPop(&stack);
stackPush(&stack, lop && rop);
return pc + 1;
}
int or(int pc)
{
int rop = stackPop(&stack);
int lop = stackPop(&stack);
stackPush(&stack, lop || rop);
return pc + 1;
}
int testeq(int pc)
{
int var = stackPop(&stack);
if(var == 0)
{
stackPush(&stack, 1);
}
else
{
stackPush(&stack, 0);
}
return pc + 1;
}
int testne(int pc)
{
int var = stackPop(&stack);
if(var != 0)
{
stackPush(&stack, 1);
}
else
{
stackPush(&stack, 0);
}
return pc + 1;
}
int testlt(int pc)
{
int var = stackPop(&stack);
if(var < 0)
{
stackPush(&stack, 1);
}
else
{
stackPush(&stack, 0);
}
return pc + 1;
}
int testle(int pc)
{
int var = stackPop(&stack);
if(var <= 0)
{
stackPush(&stack, 1);
}
else
{
stackPush(&stack, 0);
}
return pc + 1;
}
int testgt(int pc)
{
int var = stackPop(&stack);
if(var > 0)
{
stackPush(&stack, 1);
}
else
{
stackPush(&stack, 0);
}
return pc + 1;
}
int testge(int pc)
{
int var = stackPop(&stack);
if(var >= 0)
{
stackPush(&stack, 1);
}
else
{
stackPush(&stack, 0);
}
return pc + 1;
}
int jump(int pc, char * operand)
{
return retrieve(&jumpTable, operand);
}
int jf(int pc, char * operand)
{
if(stackPop(&stack) == 1)
{
return pc + 1;
}
else
{
return retrieve(&jumpTable, operand);
}
}
int halt(int pc)
{
return pc + 1;
}