-
Notifications
You must be signed in to change notification settings - Fork 2
/
assembler.cpp
360 lines (349 loc) · 10 KB
/
assembler.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <regex>
using namespace std;
map<string, string> opCodesTwo, opCodesOne, opCodesBr, opCodesSpecial, labels, functions;
map<string, int> variables;
vector<string> varVector;
vector<string> program;
vector<string> memory(2000, "0000000000000000");
vector<string> jmpVector;
std::string TrimLeft(const std::string &str)
{
auto pos = str.find_first_not_of(' ');
return str.substr(pos != std::string::npos ? pos : 0);
}
string stringToBinary(int s, int size)
{
string binary = "";
while (s)
{
if (s & 1)
binary = '1' + binary;
else
binary = '0' + binary;
s /= 2;
}
while (binary.length() != size)
binary = '0' + binary;
return binary;
}
string getRegAddMode(string s, int &count)
{
int index = 7;
int address = index;
int indirect = 0;
if (s[0] == '@')
{
indirect |= 8;
s = s.substr(1);
}
if (s[0] == '#')
{
count++;
address |= 16;
s = s.substr(1);
int number = stoi(s);
if (number < 0)
{
string binary = stringToBinary(number * -1, 15);
int index = 14;
while (index >= 0)
{
if (binary[index] == '1')
break;
index--;
}
index--;
while (index >= 0)
{
if (binary[index] == '1')
binary[index] = '0';
else
binary[index] = '1';
index--;
}
memory[count] = '1' + binary;
}
else
memory[count] = stringToBinary(number, 16);
}
else if (variables[s] != 0)
{
address |= 48;
count++;
memory[count] = s;
}
else
{
index = s.find('R');
address = s[index + 1] - '0';
if (s[4] == '+')
address |= 16;
if (s[0] == '-' && s[1] == '(')
address |= 32;
if (s[0] >= '0' && s[0] <= '9' || (s[1] >= '0' && s[1] <= '9' && s[0] == '-'))
{
address |= 48;
count++;
if (s[0] >= '0' && s[0] <= '9')
{
string indexed_val = s.substr(0, s.find('('));
memory[count] = stringToBinary(stoi(indexed_val), 16);
}
else
{
string indexed_val = s.substr(1, s.find('('));
string binary = stringToBinary(stoi(indexed_val), 15);
int ind = 14;
while (ind >= 0)
{
if (binary[ind] == '1')
break;
ind--;
}
ind--;
while (ind >= 0)
{
if (binary[ind] == '1')
binary[ind] = '0';
else
binary[ind] = '1';
ind--;
}
memory[count] = '1' + binary;
}
}
}
address |= indirect;
return stringToBinary(address, 6);
}
pair<string, int> splitInstruction(string line, int &numberOfWords)
{
line = TrimLeft(line);
string src = "";
string dst = "";
string IR = "";
pair<string, int> out("", -1);
string op = line.substr(0, line.find(' '));
if (op[op.length() - 1] == ';')
op = op.substr(0, line.find(';'));
if (op.length() == 0 || line[0] == ';' || op == "DEFINE")
return out;
string temp = TrimLeft(line.substr(op.length()));
if (temp[0] == ':')
op.push_back(':');
if (op[op.length() - 1] == ':')
{
op.erase(op.length() - 1);
labels[op] = to_string(numberOfWords + 1);
line = TrimLeft(line.substr(line.find(':')));
line.erase(0);
op = line.substr(0, line.find(' '));
}
else if (opCodesTwo[op] == "" && opCodesOne[op] == "" && opCodesBr[op] == "" && opCodesSpecial[op] == "")
{
functions[op] = to_string(numberOfWords + 1);
line = TrimLeft(line.substr(op.length()));
op = line.substr(0, line.find(' '));
}
if (op.length() == 0 || line[0] == ';' || op == "DEFINE")
return out;
numberOfWords++;
out.second = numberOfWords;
if (opCodesTwo[op] != "")
{
line = TrimLeft(line.substr(op.length()));
src = line.substr(0, line.find(','));
string srcMode = getRegAddMode(src, numberOfWords);
line = TrimLeft(line.substr(line.find(',') + 1));
dst = line.substr(0, line.find(' '));
string dstMode = getRegAddMode(dst, numberOfWords);
IR = opCodesTwo[op] + srcMode + dstMode;
}
else if (opCodesOne[op] != "")
{
line = TrimLeft(line.substr(op.length()));
src = line.substr(0, line.find(' '));
string srcMode = getRegAddMode(src, numberOfWords);
IR = opCodesOne[op] + "00" + srcMode;
}
else if (opCodesBr[op] != "")
{
line = TrimLeft(line.substr(op.length()));
src = line.substr(0, line.find(' '));
IR = opCodesBr[op];
if (labels[src] != "")
{
string binary = stringToBinary((stoi(labels[src]) - (numberOfWords + 1)) * -1, 7);
int index = 6;
while (index >= 0)
{
if (binary[index] == '1')
break;
index--;
}
index--;
while (index >= 0)
{
if (binary[index] == '1')
binary[index] = '0';
else
binary[index] = '1';
index--;
}
IR += '1' + binary;
}
else
jmpVector.push_back(src);
}
else if (opCodesSpecial[op] != "")
{
if (op == "JSR")
{
numberOfWords++;
line = TrimLeft(line.substr(op.length()));
src = line.substr(0, line.find(' '));
memory[numberOfWords] = src;
}
IR = opCodesSpecial[op] + "00000110";
}
out.first = IR;
return out;
}
bool fillVar(string line)
{
line = TrimLeft(line);
if (line.length() == 0 || line[0] == ';')
return 1;
if (line.find("DEFINE") == string::npos)
return 0;
line = TrimLeft(line.substr(6));
string name = line.substr(0, line.find(' '));
line = line.substr(line.find(' '));
line = TrimLeft(line);
string val = line.substr(0, line.find(' '));
variables[name] = stoi(val);
varVector.push_back(name);
return 1;
}
int main(int argc, char **argv)
{
string inputFileName = argv[1];
// 2 OPERAND OP CODES
opCodesTwo["MOV"] = "0001";
opCodesTwo["ADD"] = "0010";
opCodesTwo["ADC"] = "0011";
opCodesTwo["SUB"] = "0100";
opCodesTwo["SBC"] = "0101";
opCodesTwo["AND"] = "0110";
opCodesTwo["OR"] = "0111";
opCodesTwo["XOR"] = "1000";
opCodesTwo["CMP"] = "1001";
// 1 OPERAND OP CODES
opCodesOne["INC"] = "10110001";
opCodesOne["DEC"] = "10110010";
opCodesOne["CLR"] = "10110011";
opCodesOne["INV"] = "10110100";
opCodesOne["LSR"] = "10110101";
opCodesOne["ROR"] = "10110110";
opCodesOne["ASR"] = "10110111";
opCodesOne["LSL"] = "10111000";
opCodesOne["ROL"] = "10111001";
// opCodesOne["JSR"] = "11110011"; /* need to be handled*/
// BRANCH OP CODES
opCodesBr["BR"] = "11010001";
opCodesBr["BEQ"] = "11010010";
opCodesBr["BNE"] = "11010011";
opCodesBr["BLO"] = "11010100";
opCodesBr["BLS"] = "11010101";
opCodesBr["BHI"] = "11010110";
opCodesBr["BHS"] = "11010111";
//Special Operations
opCodesSpecial["HLT"] = "11110001";
opCodesSpecial["NOP"] = "11110010";
opCodesSpecial["JSR"] = "11110011";
opCodesSpecial["RTS"] = "11110100";
opCodesSpecial["IRET"] = "11110101";
string line;
ifstream input_file(inputFileName);
if (input_file.is_open())
{
while (getline(input_file, line))
{
int index = 0;
line = TrimLeft(line);
for (auto c : line)
{
line[index] = toupper(c);
index++;
}
program.push_back(line);
}
input_file.close();
}
else
{
cout << "PLEASE ENTER A VALID PATH FOR INPUT FILE !" << endl;
return -1;
}
for (int i = program.size() - 1; i >= 0; i--)
if (!fillVar(program[i]))
break;
int numberOfWords = -1;
for (int i = 0; i < program.size(); i++)
{
auto p = splitInstruction(program[i], numberOfWords);
if (p.second != -1)
memory[p.second] = p.first;
}
for (int i = varVector.size() - 1; i >= 0; i--)
{
numberOfWords++;
memory[numberOfWords] = stringToBinary(variables[varVector[i]], 16);
variables[varVector[i]] = numberOfWords;
}
for (int i = 0; i <= numberOfWords; i++)
{
if (variables[memory[i]] != 0)
{
memory[i] = stringToBinary(variables[memory[i]] - (i + 1), 16);
}
}
for (int i = 0; i <= numberOfWords; i++)
{
if (functions[memory[i]] != "")
{
memory[i] = stringToBinary(stoi(functions[memory[i]]), 16);
}
}
int index = 0;
for (int i = 0; i <= numberOfWords; i++)
{
if (memory[i].length() == 8)
{
int offset = stoi(labels[jmpVector[index]]) - (i + 1);
string offset_str = stringToBinary(offset, 8);
memory[i] += offset_str;
index++;
}
}
ofstream outputFile("memory.mem");
if (outputFile.is_open())
{
outputFile << "// instance=/integration/ramMemory/ram\n"
<< "// format=mti addressradix=d dataradix=s version=2.0 wordsperline=1\n";
for (int i = 0; i < memory.size(); i++)
outputFile
<< i << ": " << memory[i] << "\n";
outputFile.close();
}
return 0;
}