-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.cpp
More file actions
301 lines (282 loc) · 5.4 KB
/
assembler.cpp
File metadata and controls
301 lines (282 loc) · 5.4 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
#include<bits/stdc++.h>
using namespace std;
string dec_to_bin(int decimal)
{
string binary = "";
for(int i = decimal;i > 0;i/=2)
binary = to_string(i%2) + binary;
if(binary.length() < 8)
binary = string(8-binary.length(),'0').append(binary);
return binary;
}
struct mnemonics{
string name;
string binary;
int size;
}MOT[15];
struct symbol{
string name;
string type;
int location;
int size;
int section_id;
string is_global;
};
struct section{
int id;
string name;
int size;
};
vector<symbol> symbol_table;
vector<section> section_table;
int lc = 0;
int sec_id = 0;
int var_lc;
ifstream in_f;
ofstream out_f;
string word;
string str;
int pointer,size;
int search_MOT(string opcode)
{
int index = -1;
for(int i = 0;i < 15;i++)
{
if(MOT[i].name == opcode)
{
index = i;
break;
}
}
return index;
}
int search_symbol_table(string variable)
{
int location = -1;
for(vector<symbol>::const_iterator i = symbol_table.begin();i != symbol_table.end();++i)
{
if(i->name == variable)
{
location = i->location;
break;
}
}
return location;
}
int get_size(string data)
{
int size = 0;
for(int i = 0;i < data.length();i++)
{
if(data[i] == ',')
size += 4;
}
size += 4;
return size;
}
string get_data(string data)
{
string final;
string temp_str = "";
for(int i = 0;i < data.length();i++)
{
if(data[i] == ',')
{
final += dec_to_bin(stoi(temp_str.c_str()))+",";
temp_str = "";
}
else
temp_str += data[i];
}
final += dec_to_bin(stoi(temp_str.c_str()))+",";
temp_str = "";
final.erase(final.length()-1,1);
return final;
}
void store_symbol_table()
{
out_f.open("symbol.csv");
out_f << "Name,Type,Location,Size,SectionID,IsGlobal\n";
for(vector<symbol>::const_iterator i = symbol_table.begin();i != symbol_table.end();++i)
{
out_f << i->name<<",";
out_f << i->type<<",";
out_f << i->location<<",";
out_f << i->size<<",";
out_f << i->section_id<<",";
out_f << i->is_global<<"\n";
}
out_f.close();
}
void store_sec()
{
out_f.open("section.csv");
out_f << "ID,Name,Size\n";
for(vector<section>::const_iterator i = section_table.begin();i != section_table.end();++i)
{
out_f << i->id<<",";
out_f << i->name<<",";
out_f << i->size<<"\n";
}
out_f.close();
}
void pass1()
{
in_f.open("input.txt");
while(in_f >> word)
{
pointer = search_MOT(word);
if(pointer == -1)
{
str = word;
if(word.find(":") != -1)
{
symbol_table.push_back({str.erase(word.length()-1,1),"label",lc,-1,sec_id,"false"});
}
else if(word == "section")
{
in_f >> word;
sec_id++;
section_table.push_back({sec_id,word,0});
if(sec_id != 1)
{
section_table[sec_id-2].size = lc;
lc = 0;
}
}
else if(word == "global")
{
in_f >> word;
symbol_table.push_back({word,"label",-1,-1,-1,"true"});
}
else if(word == "extern")
{
in_f >> word;
symbol_table.push_back({word,"external",-1,-1,-1,"false"});
}
else
{
in_f >> word;
in_f >> word;
size = get_size(word);
symbol_table.push_back({str,"var",lc,size,sec_id,"false"});
lc += size;
}
}
else
{
if(!(pointer == 9 || pointer == 14))
in_f >> word;
if(pointer==2 || pointer==10 || pointer == 11)
in_f >> word;
lc += MOT[pointer].size;
}
}
section_table[sec_id-1].size = lc;
store_symbol_table();
store_sec();
in_f.close();
}
void pass2()
{
in_f.open("input.txt");
out_f.open("output.txt");
while(in_f >> word)
{
pointer = search_MOT(word);
if(pointer == -1)
{
str = word;
if(word.find(":") != -1)
{
out_f << "";
}
else if(word == "global")
{
in_f >> word;
out_f <<"global "<<word<<endl;
}
else if(word == "extern")
{
in_f >> word;
out_f <<"extern "<<word<<endl;
}
else if(word == "section")
{
in_f >> word;
out_f <<"section ."<<word<<endl;
lc = 0;
}
else
{
in_f >> word;
in_f >> word;
out_f <<dec_to_bin(lc)<<" "<<get_data(word)<<endl;
size = get_size(word);
lc += size;
}
}
else
{
out_f <<dec_to_bin(lc)<<" "<<MOT[pointer].binary;
if(pointer==0||pointer==1)
{
in_f >> word;
out_f <<" "<<word;
}
else if(pointer==5 || pointer==6 || pointer==7 || pointer==8 || pointer==13)
{
in_f >> word;
var_lc = search_symbol_table(word);
if(var_lc == -1)
out_f <<" "<<dec_to_bin(stoi(word.c_str()));
else
out_f <<" "<<dec_to_bin(var_lc);
}
else if(pointer==2 || pointer==10)
{
in_f >> word;
out_f <<" "<<word;
in_f >> word;
var_lc = search_symbol_table(word);
if(var_lc == -1)
out_f <<" "<<dec_to_bin(stoi(word.c_str()));
else
out_f <<" "<<dec_to_bin(var_lc);
}
else if(pointer == 11)
{
in_f >> word;
out_f <<" "<<word;
in_f >> word;
out_f <<" "<<word;
}
lc += MOT[pointer].size;
out_f << "\n";
}
}
out_f.close();
in_f.close();
}
int main()
{
MOT[0] = {"ADD","00000001",1};
MOT[1] = {"INC","00000010",1};
MOT[2] = {"CMP","00000011",5};
MOT[3] = {"JNC","00000100",1};
MOT[4] = {"JNZ","00000101",1};
MOT[5] = {"ADDI","00000110",5};
MOT[6] = {"JE","00000111",5};
MOT[7] = {"JMP","00001000",5};
MOT[8] = {"LOAD","00001001",5};
MOT[9] = {"LOADI","00001010",1};
MOT[10] = {"MVI","00001011",5};
MOT[11] = {"MOV","00001100",1};
MOT[12] = {"STOP","00001101",1};
MOT[13] = {"STORE","00001110",5};
MOT[14] = {"STORI","00001111",1};
pass1();
lc = 0;
pass2();
return 0;
}