-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.cpp
273 lines (248 loc) · 4.69 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
#include<bits/stdc++.h>
#define vec(i) vector<(i)>
#define pb push_back
#define forn(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++ )
using namespace std;
struct MOT
{
string str;
string op;
string cls;
};
struct REG
{
string str;
int ind;
};
struct CON
{
string str;
int ind;
};
struct SYMTAB
{
int index;
string str;
int add;
};
struct LITTAB
{
int index;
string str;
int add;
};
MOT mot[18];
REG reg[4];
CON con[7];
LITTAB lit[30];
SYMTAB sym[30];
void init()
{
forn(i,17)
cin>>mot[i].str>>mot[i].op>>mot[i].cls;
forn(i,4)
cin>>reg[i].str>>reg[i].ind;
forn(i,6)
cin>>con[i].str>>reg[i].ind;
}
int stoi_dc(string ss)
{
int ans = 0;
for(int i = 1 ; i < ss.size() -1 ; i++)
{
ans = ans * 10 + ss[i] - '0';
}
return ans;
}
int is_mot(string str)
{
for(int i = 1 ; i <= 17 ; i++)
if(mot[i].str == str)
return i;
return 0;
}
int is_reg(string str)
{
for(int i = 1 ; i <= 4 ; i++)
if(reg[i].str == str)
return i;
return 0;
}
int is_con(string str)
{
for(int i = 1 ; i <= 6 ; i++)
if(con[i].str == str)
return i;
return 0;
}
int is_sym(string str,int n)
{
for(int i = 1 ; i <= n ; i++)
if(sym[i].str == str)
return i;
return 0;
}
bool Valid(int n)
{
for(int i = 1 ; i <= n ; i++)
if(sym[i].add == -1)
return false;
return true;
}
int is_lit(string str,int n)
{
for(int i = 1 ; i <= n ; i++)
if(lit[i].str == str)
return i;
return 0;
}
void sym_print(int n)
{
for(int i = 1 ; i < n ; i++)
cout<<sym[i].index<<" "<<sym[i].str<<" "<<sym[i].add<<endl;
}
void lit_print(int n)
{
for(int i = 1 ; i < n ; i++)
cout<<lit[i].index<<" "<<lit[i].str<<" "<<lit[i].add<<endl;
}
bool is_AD(vector<string> v)
{
forn(i,v.size())
if(v[i] == "START" || v[i] == "END" || v[i] == "LTORG" || v[i] == "EQU" || v[i] == "ORIGIN")
return true;
return false;
}
int main()
{
freopen("Mot_input","r",stdin);
freopen("pass1_out","w",stdout);
init();
cin.clear();
freopen("pass1_input","r",stdin);
int LC = 0 ,n ,index = 0,sc = 1,lc = 1;
string ss;
while(getline(cin,ss))
{
vector<string> v;
istringstream iss(ss);
copy(istream_iterator<string>(iss),istream_iterator<string>(), back_inserter(v));
if(!is_AD(v))
cout<<++LC<<" ";
for(int i = 0 ; i < v.size() ; i++)
{
if(v[i] != ",")
{
index = is_mot(v[i]);
if(!index)
{
index = is_reg(v[i]);
if(!index)
{
index = is_con(v[i]);
if(!index)
{
index = is_lit(v[i],lc);
if(v[i][0] == '=' && (!index || lit[index].index != -1))
{
lit[lc].index = lc;
lit[lc].str = v[i];
lit[lc].add = -1;
lc++;
}
index = is_sym(v[i],sc);
if(isalpha(v[i][0]) && (!index) && (i == 0 || i == v.size()-1 ))
{
sym[sc].index = sc;
sym[sc].str = v[i];
if(i == 0)
sym[sc].add = LC ;
else
{
sym[sc].add = -1;
}
sc++;
}
}
else
cout<<con[index].ind <<" ";
}
else
cout<<reg[index].ind<<" ";
}
else
cout<<"("<<mot[index].cls<<","<<mot[index].op<<")"<<" ";
if(i > 0 && v[i-1] == "START" && isdigit(v[i][0]))
{
cout<<"(C,"<<v[i]<<")"<<" ";
LC = stoi(v[i]) - 1;
}
if(v[i] == "LTORG" || v[i] == "END")
{
for(int i = 1 ; i <= lc ; i++)
{
if(lit[i].add == -1)
lit[i].add = LC++;
}
}
index = is_sym(v[i],sc);
if( index )
{
if(i == 0)
sym[index].add = LC ;
else
cout<<"(S,"<<sym[index].index<<")"<<" ";
}
index = is_lit(v[i],lc);
if(index)
{
cout<<"(L,"<<lit[index].index<<")"<<" ";
}
if(i > 0 && v[i - 1] == "DC" && isdigit(v[i][1]))
{
cout<<"(C,"<<v[i]<<")"<<" ";
LC += stoi_dc(v[i]) - 1;
}
if(i > 0 && v[i - 1] == "DS" && isdigit(v[i][0]))
{
cout<<"(C,"<<v[i]<<")"<<" ";
}
if(v[i] == "EQU")
{
int right = is_sym(v[i+1],sc),left = is_sym(v[i-1],sc);
if(right && left)
sym[left].add = sym[right].add;
}
}
}
cout<<endl;
}
cin.clear();cout.clear();
freopen("pass2_input","w",stdout);
sym_print(sc);
lit_print(lc);
cin.clear();cout.clear();
freopen("pass1_out","r+",stdin);
freopen("pass2_out","w",stdout);
while(getline(cin,ss))
{
vector<string> v;
istringstream iss(ss);
copy(istream_iterator<string>(iss),istream_iterator<string>(),back_inserter(v));
for(int i = 0 ; i < v.size() ; i++)
{
if(isdigit(v[i][0]))
cout<<v[i]<<" ";
else if(v[i][1] == 'D' || v[i][1] == 'I')
cout<<v[i][4]<<v[i][5]<<" ";
else if(v[i][1] == 'L')
cout<<lit[v[i][3] - '0'].add<<" ";
else if(v[i][1] == 'S')
cout<<sym[v[i][3] - '0'].add<<" ";
else
cout<<"---"<<" ";
}
cout<<endl;
}
return 0;
}