-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassembler.cpp
More file actions
557 lines (449 loc) · 12.5 KB
/
Copy pathassembler.cpp
File metadata and controls
557 lines (449 loc) · 12.5 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
pair<string, string> files_names(int, char**);
struct parser
{
// attributes
map<string, string> op, // operations
reg; // registers
map<int, int> sym; // symbols table, holds variables and labels
vector<int> address; // maps assembly file addresses to binary file addresses
int current_address; // current address in binary file
string word; // binary instruction
vector<string> extra_words; // extra words required for the instruction
// string err_msg; // error handling
// methods
parser(int);
void symbols_table(string, int); // initializes and stores variables and labels
void line(string, int, bool = true); // assembly instruction to binary
string operand(string, int);
void error(string, int); // throws an exception when an error is found
};
string whitespace(string); // removes whitespace at the start and end of the string
bool is_between(char, char, char);
bool is_number(string, bool = true);
bool is_valid_name(string);
string capitalize(string); // change all lowercase letters to uppercase ones in a string
string bin(unsigned int, int = 16); // returns binary value of a decimal number (as string)
int pwr(int, int);
int s_int(string);
int main(int argc, char* argv[])
{
pair<string, string> files = files_names(argc, argv);
ifstream fin(files.first);
ofstream fout(files.second);
vector<string> program;
// program.push_back("");
while (fin.peek() != EOF) // read code
{
string instruction;
getline(fin, instruction);
program.push_back(instruction);
}
parser parse(program.size());
for (int line_num = 0; line_num < program.size(); ++ line_num) // find and store variables
{
try
{
parse.line(program[line_num], line_num);
}
catch(string e)
{
cout << e << '\n';
return 1;
}
}
parse.current_address = 0;
for (int line_num = 0; line_num < program.size(); ++ line_num) // assembly to binary
{
try
{
parse.line(program[line_num], line_num);
}
catch(string e)
{
cout << e << '\n';
return 1;
}
string binary = parse.word;
binary = whitespace(binary);
if (binary.size() == 0)
{
continue;
}
fout << parse.word << endl;
for (auto x : parse.extra_words)
{
fout << x << endl;
}
}
fin.close();
fout.close();
return 0;
}
parser::parser(int prog_size)
{
this->address.resize(prog_size + 1);
this->current_address = 0;
ifstream aux("operations.txt");
while(aux.peek() != EOF)
{
string name, code;
aux >> name >> code;
this->op[name] = code;
}
aux.close();
aux.open("registers.txt");
while(aux.peek() != EOF)
{
string name, code;
aux >> name >> code;
this->reg[name] = code;
}
}
void parser::symbols_table(string instruction, int line_num)
{
instruction = whitespace(instruction);
if (instruction.size() == 0)
{
return;
}
int start = 0;
string delim = "#";
int end = instruction.find(delim, start);
if (end == 0) // this is a variable definition
{
start = end + delim.size();
end = instruction.size();
string val = instruction.substr(start, end - start);
val = whitespace(val);
// check for characters and use ascii
if (!is_number(val))
{
this->error("Variable value must be numeric.", line_num);
}
else
{
int x = s_int(val);
this->sym[line_num] = x;
this->word = bin(x);
}
}
}
void parser::line(string instruction, int line_num, bool var_err)
{
this->address[line_num] = this->current_address;
instruction = whitespace(instruction);
if (instruction.size() == 0) // empty line
{
return;
}
++(this->current_address);
this->word.clear();
this->extra_words.clear();
int start = 0;
string delim = " ";
int end = instruction.find(delim, start);
string key = instruction.substr(start, end - start);
start = end + delim.size();
key = whitespace(capitalize(key));
if (this->op.count(key) > 0) // operation
{
this->word += this->op[key];
if (this->word.size() == 4) // 2 operand
{
delim = ",";
end = instruction.find(delim, start);
if (end == string::npos)
{
this->error("Two operand intruction but only one found.", line_num);
}
else
{
key = instruction.substr(start, end - start);
start = end + delim.size();
this->word += this->operand(key, line_num);
key = instruction.substr(start, instruction.size() - start);
this->word += this->operand(key, line_num);
}
}
else if (this->word.size() == 10) // 1 operand
{
key = instruction.substr(start, instruction.size() - start);
this->word += this->operand(key, line_num);
}
else if (this->word.size() == 8) // branching operation
{
string val = instruction.substr(start, instruction.size() - start);
val = capitalize(val);
val = whitespace(val);
if (!is_number(val))
{
this->error("Branching instruction must take relative offset.", line_num);
}
int x = s_int(val);
x += line_num + 1;
x = this->address[x];
x -= this->current_address;
this->word += bin(x, 8); // offset doesn't match (asm - bin)
// this->word += this->operand(key, line_num);
}
else if (this->word == "1110000000000000") // JSR
{
string val = instruction.substr(start, instruction.size() - start);
val = whitespace(val);
if (!is_number(val))
{
this->error("JSR must take a numeric value.", line_num);
}
this->extra_words.push_back(bin(this->address[s_int(val)]));
}
else // no operand operation
{
/* nothings needs to be done */;
}
}
else if (key[0] == '#') // variable definition
{
this->symbols_table(instruction, line_num);
}
else if (is_number(key))
{
this->error("Unknown instruction '" + key + "' .", line_num);
}
else
{
this->error("Unknown identifier '" + key + "' .", line_num);
}
this->current_address += this->extra_words.size();
}
string parser::operand(string op, int line_num)
{
op = whitespace(op);
if (op[0] == '#') // variable
{
string w = "010111"; // (R7)+
string val = op.substr(1, op.size() - 1);
val = whitespace(val);
if (!is_number(val))
{
this->error("Operand must take a numeric value after '#'.", line_num);
}
this->extra_words.push_back(bin(s_int(val)));
return w;
}
else if (op[0] == '@') // register
{
string tmp = this->operand(op.substr(1, op.size() - 1), line_num); // get as direct
tmp[2] = '1'; //change to indirect
// this->word += tmp;
return tmp;
}
else if (op[0] == '-') // auto-decrement
{
int start = op.find("-(", 0);
int end = op.find(")", 0);
if (start == string::npos || end == string::npos)
{
this->error("Incorrect addressing mode '" + op + "' .", line_num);
}
start += 2;
string val = op.substr(start, end - start);
val = whitespace(val);
return "100" + this->reg[capitalize(val)];
}
else if (op[0] == '(') // auto-increment
{
int start = op.find("(", 0);
int end = op.find(")+", 0);
if (start == string::npos || end == string::npos)
{
this->error("Incorrect addressing mode '" + op + "' .", line_num);
}
start += 1;
string val = op.substr(start, end - start);
val = whitespace(val);
return "010" + this->reg[capitalize(val)];
}
else if (this->reg.count(op) > 0) // register
{
return "000" + this->reg[op];
}
else if (is_between(op[0], '0', '9')) // numebr or indexed
{
int start = 0;
string delim = "(";
int end = op.find(delim, start);
if (end != string::npos) // indexed
{
string val = op.substr(start, end - start);
val = whitespace(val);
if (!is_number(val))
{
this->error("Indexed mode must have a number.", line_num);
}
int x = s_int(val);
this->extra_words.push_back(bin(x));
start = end + delim.size();
end = op.find(")", start);
if (end == string::npos)
{
this->error("Expected ')'.", line_num);
}
string rop = op.substr(start, end - start);
val = whitespace(rop);
return "110" + this->reg[capitalize(rop)];
}
else // number (address)
{
if (!is_number(op))
{
this->error("Unknown operand '" + op + "' .", line_num);
}
int x = s_int(op);
this->extra_words.push_back(bin(this->address[x] - this->current_address - 1));
return "110111"; // X(R7)
}
}
else
{
this->error("Unknown operand '" + op + "' .", line_num);
}
}
void parser::error(string e, int line_num)
{
string err_msg = "Error in line " + to_string(line_num) + ":\n\t" + e;
throw err_msg;
}
pair<string, string> files_names(int argc, char* argv [])
{
pair<string, string> files;
if (argc > 2)
{
files.first = argv[1];
files.second = argv[2];
}
else
{
cin >> files.first >> files.second;
}
return files;
}
string whitespace(string s)
{
int i;
for (i = 0; i < s.size(); ++i)
{
if (s[i] != ' ')
{
break;
}
}
int j;
for (j = s.size() - 1; j >= 0; --j)
{
if (s[j] != ' ')
{
break;
}
}
s = s.substr(i, j + 1 - i);
return s;
}
bool is_between(char c, char from, char to)
{
if (from > to)
{
swap(from, to);
}
return (c >= from && c <= to);
}
bool is_number(string s, bool allow_negative)
{
if (s[0] == '-' && !allow_negative) // don't allow negative values
{
return false;
}
else if (s[0] == '-' && allow_negative) // allow negative values
{
s = s.substr(1, s.size());
}
for (auto c : s)
{
if (!is_between(c, '0', '9'))
{
return false;
}
}
return true;
}
bool is_valid_name(string s)
{
if (!is_between(s[0], 'A', 'Z') && !is_between(s[0], 'a', 'z'))
{
return false;
}
for (auto c : s)
{
if (!(is_between(c, '0', '9') || is_between(c, 'a', 'z') || is_between(c, 'A', 'Z') || c == '_'))
{
return false;
}
}
return true;
}
string capitalize(string s)
{
for (auto & c : s)
{
if (is_between(c, 'a', 'z'))
{
c += 'A' - 'a';
}
}
return s;
}
string bin(unsigned int x, int b)
{
string s;
s.resize(b, '0');
for(int i = 0; i < b; ++i)
{
s[b-1 - i] += (x & 1);
x >>= 1;
}
return s;
}
int pwr(int b, int e)
{
int ans = 1;
while(e--)
{
ans *= b;
}
return ans;
}
int s_int(string num)
{
int x = 0;
int i = 0;
reverse(num.begin(), num.end());
for (auto d : num)
{
if (d == '-')
{
x *= -1;
}
else
{
x += (d-'0') * pwr(10, i);
++i;
}
}
return x;
}