-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathassembler.cpp
More file actions
449 lines (341 loc) · 10.7 KB
/
Copy pathassembler.cpp
File metadata and controls
449 lines (341 loc) · 10.7 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/** Which of the favors of your Lord will you deny ? **/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define F first
#define S second
#define ALL(x) (x).begin(), (x).end()
#define READ freopen("in.txt", "r", stdin)
#define WRITE freopen("out.txt", "w", stdout)
#ifndef ONLINE_JUDGE
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
#else
#define DBG(x)
#define endl "\n"
#endif
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);
inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
/**
R : add , sub , and , or , sll , srl , nor
I : addi , subi , andi , ori , sw , lw , beq , bneq
J : j
**/
string lower(string s)
{
string ret;
for(auto c:s) ret += tolower(c);
return ret;
}
string remove_whitespace(string str)
{
string::iterator end_pos = std::remove(str.begin(), str.end(), ' ');
str.erase(end_pos, str.end());
return str;
}
vector<string> tokenize(string const str,char delim)
{
vector<string> ret;
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != string::npos)
{
end = str.find(delim, start);
ret.push_back(str.substr(start, end - start));
}
return ret;
}
string dec_to_hex(string d_s)
{
int decimal_value = stoi(d_s);
stringstream ss;
ss<< hex << decimal_value;
string res ( ss.str() );
return res;
}
string dec_to_hex_pad_trim(string d_s)
{
string t = dec_to_hex(d_s);
int n = t.size();
if(n==1)
{
t = "0"+t;
return t;
}
else if(n>2)
{
return t.substr(n-2,2);
}
return t;
}
int stringHexToDec(string s)
{
stringstream str;
str << s;
int value;
str >> hex >> value;
return value;
}
int32_t main()
{
optimizeIO();
READ;
WRITE;
vector<string>instructions = {"add" , "sub" , "and" , "or" , "sll" , "srl" , "nor" , "addi" , "subi" , "andi" , "ori" , "sw" , "lw" , "beq" , "bneq" , "j"};
vector<string>instructionsR = {"add" , "sub" , "and" , "or" , "sll" , "srl" , "nor"};
vector<string>instructionsI = {"addi" , "subi" , "andi" , "ori" , "sw" , "lw" , "beq" , "bneq"};
map<string,string>MAP_REG_ADD;
/// in hex
MAP_REG_ADD["$zero"] = "0";
MAP_REG_ADD["$t0"] = "1";
MAP_REG_ADD["$t1"] = "2";
MAP_REG_ADD["$t2"] = "3";
MAP_REG_ADD["$t3"] = "4";
MAP_REG_ADD["$t4"] = "5";
MAP_REG_ADD["$t5"] = "6";
MAP_REG_ADD["$sp"] = "7";
map<string,string>MAP_INS_OPC;
/// in hex
MAP_INS_OPC["add"] = "e";
MAP_INS_OPC["sub"] = "9";
MAP_INS_OPC["and"] = "8";
MAP_INS_OPC["or"] = "b";
MAP_INS_OPC["sll"] = "3";
MAP_INS_OPC["srl"] = "c";
MAP_INS_OPC["nor"] = "2";
MAP_INS_OPC["addi"] = "7";
MAP_INS_OPC["subi"] = "f";
MAP_INS_OPC["andi"] = "4";
MAP_INS_OPC["ori"] = "5";
MAP_INS_OPC["sw"] = "0";
MAP_INS_OPC["lw"] = "d";
MAP_INS_OPC["beq"] = "1";
MAP_INS_OPC["bneq"] = "a";
MAP_INS_OPC["j"] = "6";
map<string,string>MAP_LABEL_ADD;
map<string,int>MAP_INSTRUCTION_COUNTER;
vector<string>machine_code;
vector< pair<int,string> >line_label;
string ins,og;
int counter = 0;
while(cin>>ins)
{
// if(ins == "EXIT") break;
og = ins;
ins = lower(ins);
counter = machine_code.size();
if(find(ALL(instructionsR),ins) != instructionsR.end()) /// R type
{
string info;
getline(cin, info);
info = remove_whitespace(info);
vector <string> res = tokenize(info,',');
string dest_reg = res[0];
string src_reg_1 = res[1];
string src_reg_2 = res[2];
string opcode = MAP_INS_OPC[ins];
string sh_amt = "0";
string output = "";
output = MAP_INS_OPC[ins] + MAP_REG_ADD[src_reg_1] + MAP_REG_ADD[src_reg_2] + MAP_REG_ADD[dest_reg] + sh_amt;
// DBG(output);
if(ins == "sll" || ins == "srl")
{
sh_amt = dec_to_hex(src_reg_2);
output = MAP_INS_OPC[ins] + MAP_REG_ADD[src_reg_1] + "0" + MAP_REG_ADD[dest_reg] + sh_amt;
}
machine_code.push_back(output);
}
else if(find(ALL(instructionsI),ins) != instructionsI.end()) /// I type
{
string info;
getline(cin, info);
info = remove_whitespace(info);
vector <string> res = tokenize(info,',');
string dest_reg = res[0];
string src_reg = res[1];
string opcode = MAP_INS_OPC[ins];
string output = "";
if(ins=="addi" || ins=="subi" || ins=="andi" || ins=="ori")
{
string add_imm = res[2];
string t = dec_to_hex_pad_trim(add_imm);
output = MAP_INS_OPC[ins] + MAP_REG_ADD[src_reg] + MAP_REG_ADD[dest_reg] + t;
}
else if(ins=="sw" || ins=="lw")
{
/// address = 0 ?????
string one = res[0];
string other = res[1];
if(other[0]=='$')
{
output = MAP_INS_OPC[ins] + MAP_REG_ADD[src_reg] + MAP_REG_ADD[dest_reg] + "00";
}
else
{
vector <string> temp = tokenize(other,'(');
string offset_d = temp[0];
string left = temp[1];
vector <string> temp2 = tokenize(left,')');
other = temp2[0];
string t = dec_to_hex_pad_trim(offset_d);
output = MAP_INS_OPC[ins] + MAP_REG_ADD[other] + MAP_REG_ADD[one] + t ;
}
}
else if(ins=="beq" || ins=="bneq")
{
string one = res[0];
string other = res[1];
string label = res[2];
if(MAP_LABEL_ADD.find(label)==MAP_LABEL_ADD.end()) /// label not present
{
output = MAP_INS_OPC[ins] + MAP_REG_ADD[one] + MAP_REG_ADD[other];
line_label.push_back({counter,label});
MAP_INSTRUCTION_COUNTER[output] = counter;
}
else /// label already exist
{
int offsetCount = stringHexToDec(MAP_LABEL_ADD[label]) - counter - 1;
string offsetCountStr = dec_to_hex_pad_trim(to_string(offsetCount));
output = MAP_INS_OPC[ins] + MAP_REG_ADD[one] + MAP_REG_ADD[other] + offsetCountStr;
}
}
machine_code.push_back(output);
}
else if(ins == "j")
{
string target;
cin>>target;
string output = "";
if(MAP_LABEL_ADD.find(target)==MAP_LABEL_ADD.end())
{
output = MAP_INS_OPC[ins];
line_label.push_back({counter,target});
}
else
{
output = MAP_INS_OPC[ins] + MAP_LABEL_ADD[target] + "00";
}
machine_code.push_back(output);
}
else if(ins == "push")
{
string other;
cin>>other;
string output = "";
if(other[0]=='$')
{
/**
push $t0
=> sw $t0,0($sp)
=> subi $sp,$sp,1
**/
output = MAP_INS_OPC["sw"] + MAP_REG_ADD["$sp"] + MAP_REG_ADD[other] + "00";
machine_code.push_back(output);
output = MAP_INS_OPC["subi"] + MAP_REG_ADD["$sp"] + MAP_REG_ADD["$sp"] + "01";
machine_code.push_back(output);
}
else
{
vector <string> temp = tokenize(other,'(');
string offset_d = temp[0];
string left = temp[1];
vector <string> temp2 = tokenize(left,')');
other = temp2[0];
string t = dec_to_hex_pad_trim(offset_d);
output = MAP_INS_OPC["lw"] + MAP_REG_ADD[other] + MAP_REG_ADD["$t5"] + t ;
machine_code.push_back(output);
output = MAP_INS_OPC["sw"] + MAP_REG_ADD["$sp"] + MAP_REG_ADD["$t5"] + "00" ;
machine_code.push_back(output);
output = MAP_INS_OPC["subi"] + MAP_REG_ADD["$sp"] + MAP_REG_ADD["$sp"] + "01";
machine_code.push_back(output);
}
}
else if(ins == "pop")
{
string reg;
cin>>reg;
string output = "";
output = MAP_INS_OPC["addi"] + MAP_REG_ADD["$sp"] + MAP_REG_ADD["$sp"] + "01";
machine_code.push_back(output);
output = MAP_INS_OPC["lw"] + MAP_REG_ADD["$sp"] + MAP_REG_ADD[reg] + "00" ;
machine_code.push_back(output);
}
else
{
/// label
vector <string> res = tokenize(og,':');
string label = res[0];
string addr = dec_to_hex(to_string(counter));
if((int)addr.size()==1) addr = "0"+addr;
MAP_LABEL_ADD[label] = addr;
}
}
// DBG(line_label);
for(int i=0;i<(int)line_label.size();i++)
{
int code_idx = line_label[i].first;
string label_name = line_label[i].second;
string &str = machine_code[code_idx];
if(str==MAP_INS_OPC["j"]) /// jump instruction
{
str += MAP_LABEL_ADD[label_name];
str += "00";
}
else
{
int offsetCount = stringHexToDec(MAP_LABEL_ADD[label_name]) - MAP_INSTRUCTION_COUNTER[str] - 1;
string offsetCountStr = dec_to_hex_pad_trim(to_string(offsetCount));
str += offsetCountStr;
}
}
// DBG(machine_code);
cout<<"v2.0 raw"<<endl;
for(auto x:machine_code)
{
cout<<x<<" ";
}
return 0;
}
/**
Corner cases:
- label declared later
- jump to actual address ?
**/
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
os<<"{"<<p.first<<", "<<p.second<<"} ";
return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" " ;
}
os<<" ]";
return os;
}
template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
os<<"[ ";
for(T i:v)
{
os<<i<<" ";
}
os<<" ]";
return os;
}