-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.cpp
376 lines (368 loc) · 10.7 KB
/
code.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
#include <bits/stdc++.h>
#include <string>
#include <stdlib.h>
#include <fstream>
#include "R.h"
#include "I.h"
#include "S.h"
#include "U.h"
#include "SB.h"
#include "J.h"
#include <unordered_map>
using namespace std;
int PC = 0;
int PC_2 = 0; // for label
unsigned int dat = 268435456;
int factor = 0;
// R format
vector<string> Rf;
// I format
vector<string> If;
// S format
vector<string> Sf;
// UJ format
vector<string> Uf;
// SB format
vector<string> Sbf;
// J format
vector<string> Jf;
// Function to determine id .data type is word or other
void data_func(string data_type)
{
if (data_type == "byte")
factor = 1;
else if (data_type == "half")
factor = 2;
else if (data_type == "word")
factor = 4;
else
factor = 8;
}
// Function to convert PC into hexdecimal
string hexa_convert(int y)
{
int ty = 0;
string an = "";
if (y == 0)
ty++;
while (y > 0)
{
int r = y % 16;
y = y / 16;
if (r >= 10)
{
if (r == 10)
an = 'A' + an;
else if (r == 11)
an = 'B' + an;
else if (r == 12)
an = 'C' + an;
else if (r == 13)
an = 'D' + an;
else if (r == 14)
an = 'E' + an;
else if (r == 15)
an = 'F' + an;
}
else
{
an = char(r + '0') + an;
}
}
if (ty)
return an = "0";
return an;
}
int main()
{
// Opening the assembly file
ifstream file("assembly_code.asm");
if (!file.is_open())
{
cout << "Error opening file." << endl;
return 0;
}
// Taking every line of assembly code as strings in vector
vector<string> lines;
string s;
while (getline(file, s))
{
lines.push_back(s);
}
// Completed taking file as input
file.close();
// Adding R format instructions
Rf.push_back("add");
Rf.push_back("and");
Rf.push_back("or");
Rf.push_back("sll");
Rf.push_back("slt");
Rf.push_back("sra");
Rf.push_back("srl");
Rf.push_back("sub");
Rf.push_back("xor");
Rf.push_back("mul");
Rf.push_back("div");
Rf.push_back("rem");
// Adding I format Instruction
If.push_back("addi");
If.push_back("andi");
If.push_back("ori");
If.push_back("lb");
If.push_back("ld");
If.push_back("lh");
If.push_back("lw");
If.push_back("jalr");
// Adding S format
Sf.push_back("sb");
Sf.push_back("sw");
Sf.push_back("sd");
Sf.push_back("sh");
// Addig Sb format
Sbf.push_back("beq");
Sbf.push_back("bne");
Sbf.push_back("bge");
Sbf.push_back("blt");
// Adding U format
Uf.push_back("auipc");
Uf.push_back("lui");
// Adding UJ format
Jf.push_back("jal");
// END of inserting Mnemonics
int flag = 0;
// Creating output file
ofstream output("ans.mc");
int counter_2 = 0;
// Initializing map for label
unordered_map<string, int> m_label;
for (int i = 0; i < lines.size(); i++)
{
string ans = "";
string Mnemonic = "";
int j = 0;
// Getting the opcode until space and avoiding space in from of the opcode
while ((lines[i][j] != ' ' || Mnemonic == "") && j < lines[i].size())
{
// Checking if the instruction is comment or not
if (lines[i][j] == '#')
{
flag++;
break;
}
Mnemonic = Mnemonic + lines[i][j];
j++;
}
// Checking if its a comment
if (flag)
{
flag = 0;
continue;
}
// If not a comment then proceeding with instruction
if (Mnemonic == ".data:")
{
i++;
// the assumption that .data is in a separate line and anything following it is on the next line
int j = 0;
while ((lines[i] != ".text:") && i < lines.size()) // the assumption that while starting the text segment it should start exactly with the line ".text"
// meaning no other char before or after this exact word
{
string data_type = "";
while (lines[i][j] != '.')
{
j++;
}
j++;
while (lines[i][j] != ' ')
{
data_type += lines[i][j];
j++;
}
if (data_type == "asciiz" || data_type == "string")
{
string str = "";
while (lines[i][j] != '\"')
{
j++;
}
j++;
while (lines[i][j] != '\"')
{
int y = lines[i][j];
string h = hexa_convert(y);
output << "0x" << hexa_convert(dat) << " 0x" << h << "\n";
dat++;
j++;
}
dat++;
}
else
{
data_func(data_type);
vector<string> arr;
while (lines[i][j] == ' ')
j++;
while (j < lines[i].size())
{
string id = "";
while (lines[i][j] != ' ' && j < lines[i].size())
{
id = id + lines[i][j];
j++;
}
arr.push_back(id);
j++;
}
for (int t = 0; t < arr.size(); t++)
{
int yt = stoi(arr[t]);
string h = hexa_convert(yt);
output << "0x" << hexa_convert(dat) << " 0x" << h << "\n";
dat += factor;
}
}
i++;
j = 0;
}
}
else
{
// Creating map function for labels
if (!counter_2)
{
for (int ot = i; ot < lines.size(); ot++)
{
string lab = "";
if (lines[ot][lines[ot].size() - 1] == ':') // assuming that label is in a separate line
{
for (int re = 0; re < lines[ot].size(); re++)
{
if (lines[ot][re] != ' ' && lines[ot][re] != ':')
{
if (lines[ot][re] == '#')
break;
lab = lab + lines[ot][re];
}
}
m_label[lab] = PC_2;
}
else
{
int flag_32 = 0;
for (int yu = 0; yu < lines[ot].size(); yu++)
{
if (lines[ot][yu] == '#')
flag_32++;
}
if (!flag_32)
PC_2 += 4;
}
}
counter_2++;
}
// Checking if its R_format Mnemonics
if (ans == "")
{
for (int k = 0; k < Rf.size(); k++)
{
if (Mnemonic == Rf[k])
{
ans = R(lines[i], j, Mnemonic);
break;
}
}
if (ans != "")
{
output << "0x" << hexa_convert(PC) << " " << ans << "\n";
PC += 4;
}
}
// Checking if its I format Mnemonic
if (ans == "")
{
for (int k = 0; k < If.size(); k++)
{
if (Mnemonic == If[k])
{
ans = I(lines[i], j, Mnemonic);
break;
}
}
if (ans != "")
{
output << "0x" << hexa_convert(PC) << " " << ans << "\n";
PC += 4;
}
}
// Checking if its S format Mnemonic
if (ans == "")
{
for (int k = 0; k < Sf.size(); k++)
{
if (Mnemonic == Sf[k])
{
ans = S(lines[i], j, Mnemonic);
break;
}
}
if (ans != "")
{
output << "0x" << hexa_convert(PC) << " " << ans << "\n";
PC += 4;
}
}
// Checking if its SB format Mnemonics
if (ans == "")
{
for (int k = 0; k < Sbf.size(); k++)
{
if (Mnemonic == Sbf[k])
{
ans = SB(lines[i], j, Mnemonic, m_label, PC);
break;
}
}
if (ans != "")
{
output << "0x" << hexa_convert(PC) << " " << ans << "\n";
PC += 4;
}
}
// Checking if its U format Mnemonics
if (ans == "")
{
for (int k = 0; k < Uf.size(); k++)
{
if (Mnemonic == Uf[k])
{
ans = U(lines[i], j, Mnemonic);
break;
}
}
if (ans != "")
{
output << "0x" << hexa_convert(PC) << " " << ans << "\n";
PC += 4;
}
}
// Checking if its UJ format Mnemonics
if (ans == "")
{
for (int k = 0; k < Jf.size(); k++)
{
if (Mnemonic == Jf[k])
{
ans = J(lines[i], j, Mnemonic, m_label, PC);
break;
}
}
if (ans != "")
{
output << "0x" << hexa_convert(PC) << " " << ans << "\n";
PC += 4;
}
}
}
}
output << "0x" << hexa_convert(PC) << " Termination of code";
}