-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
379 lines (335 loc) · 7.62 KB
/
main.cpp
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
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
const int LABEL_COUNT = 100; ///< default number of labels
const int OPCODE_COUNT = 18; ///< default number of opcodes
const int MAX_CHARS = 5; ///< maximum number of characters
const int STACK_SIZE = 1000; ///< maximum capacity of array
const std::string OPCODE_LIST = "const get put ld st add sub mul\
div cmp jpos jz j jl jle jg jge halt";
struct Opcode
{
string name;
int mem;
Opcode()
{
name = "";
mem = 0;
}
};
struct Symbol
{
string name;
int mem;
Symbol()
{
name = "";
mem = 0;
}
};
//splits the list of opcodes into individual ones
void splitOpcodes(string inst, Opcode *opcodes)
{
int i = 0;
istringstream ss(inst);
string substring;
while (ss >> substring)
{
opcodes[i].name = substring;
opcodes[i].mem = i;
i++;
}
}
//checks if the passed word is an opcode
bool isOpCode(string &word, Opcode *opcodes)
{
for (int i = 0; i < 18; i++)
{
if (word == opcodes[i].name)
{
return true;
}
}
return false;
}
//returns the location of the opcode
int getOpcode(string &word, Opcode *opcodes)
{
for (int i = 0; i < OPCODE_COUNT; i++)
{
if (word == opcodes[i].name)
{
return i;
}
}
return -1;
}
//checks if the first chracter is an ASCII number
bool isNumber(string &str)
{
char temp = str[0];
if ((temp >= 48 && temp <= 57) && (str != "" || str != " "))
{
return true;
}
return false;
}
//returns the location in memory of the label
int getLocation(string &name, Symbol *labels, int nextLabel)
{
for (int i = 0; i < LABEL_COUNT; i++)
{
if (name == labels[i].name)
{
return labels[i].mem;
}
else if (isNumber(name))
{
return stoi(name);
}
else if (name == "" || name == " ")
{
return 0;
}
}
return -1;
}
int main(int argc, char *argv[])
{
ifstream iFile;
ofstream oFile;
string line;
Opcode opcodes[OPCODE_COUNT];
Symbol labels[LABEL_COUNT];
string columnCodes[STACK_SIZE];
int secondmem[STACK_SIZE] = {0};
int memory[STACK_SIZE] = {0};
int firstmem[STACK_SIZE] = {0};
int cnt = 0;
splitOpcodes(OPCODE_LIST, opcodes);
std::cout << "Running program..." << std::endl;
iFile.open(argv[1]);
if (!(iFile.is_open()))
{
cout << "** Program terminated **" << endl;
return EXIT_SUCCESS;
}
stringstream ss(line);
string characters;
int label = 0;
int i = 0;
while (getline(iFile, line))
{
// deletes anything that comes after a semicolon
int commentPos = line.find(';');
if (commentPos != std::string::npos)
{
line.erase(commentPos);
}
if (line.size() == 0)
{
continue;
}
stringstream ss(line);
string columnOne, columnTwo, columnThree;
// skips past any line that starts with a semicolon
if (line[0] == ';')
{
continue;
}
else
{
// if a line starts with space or tab it puts the
// second column into the array
if (line[0] == ' ' || line[0] == '\t')
{
while (ss >> columnTwo)
{
columnCodes[i] += columnTwo + " ";
}
i++;
}
else
{
// puts first column into an array
ss >> columnOne;
labels[label].name = columnOne;
labels[label].mem = i;
while (ss >> columnTwo)
{
columnCodes[i] += columnTwo + " ";
}
label++;
i++;
}
}
}
iFile.close();
//outputs the code into the new output file
oFile.open("out.txt");
for (int j = 0; j < i; j++)
{
oFile << columnCodes[j] << endl;
}
oFile.close();
// pass two
iFile.open("out.txt");
string keeper;
string opHolder;
string labelHolder;
int memCount = 0;
//reads from the new file
while (getline(iFile, keeper))
{
stringstream stream(keeper);
while (stream >> opHolder)
{
if (isOpCode(opHolder, opcodes))
{
int opLocation = getOpcode(opHolder, opcodes);
stream >> labelHolder;
if (!isOpCode(labelHolder, opcodes))
{
int labelLocation = getLocation(labelHolder, labels, label);
memory[memCount] = STACK_SIZE * opLocation + labelLocation;
memCount++;
}
}
labelHolder = "";
}
}
iFile.close();
int reg = 0;
int ip = 0;
//gets the memory and checks it with each command
while (ip >= 0)
{
int address = memory[ip] % 1000;
int code = memory[ip] / 1000;
int input = 0;
if (code == 0)
{
break;
}
else if (code == 1)
{
cin >> reg;
if (!cin)
{
cout << "break" << endl;
cout << "ERROR: invalid input!" << endl
<< "** Program terminated with an error code **";
exit(0);
}
cout << "read: " << reg << endl;
ip++;
}
else if (code == 2)
{
cout << "result: " << reg << endl;
ip++;
}
else if (code == 3)
{
reg = memory[address];
ip++;
}
else if (code == 4)
{
memory[address] = reg;
ip++;
}
else if (code == 5)
{
reg = reg + memory[address];
ip++;
}
else if (code == 6)
{
reg -= memory[address];
ip++;
}
else if (code == 7)
{
reg = reg * memory[address];
ip++;
}
else if (code == 8)
{
reg = reg / memory[address];
ip++;
}
else if (code == 9)
{
reg = reg - memory[address];
ip++;
}
else if (code == 10)
{
if (reg > 0)
{
ip = address;
}
else
ip++;
}
else if (code == 11)
{
if (reg == 0)
{
ip = address;
}
else
ip++;
}
else if (code == 12)
{
ip = address;
}
else if (code == 13)
{
if (reg < 0)
{
ip = address;
}
else
ip++;
}
else if (code == 14)
{
if (reg <= 0)
{
ip = address;
}
else
ip++;
}
else if (code == 15)
{
if (reg > 0)
{
ip = address;
}
else
ip++;
}
else if (code == 16)
{
if (reg >= 0)
{
ip = address;
}
else
ip++;
}
else
{
cout << "** Program terminated **";
ip = -1;
}
}
cnt++;
return EXIT_SUCCESS;
}