-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssembler.cpp
More file actions
86 lines (71 loc) · 2.2 KB
/
Assembler.cpp
File metadata and controls
86 lines (71 loc) · 2.2 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
#include "Assembler.h"
#include "Parser.h"
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <iostream>
Assembler::Assembler (std::string in_file, std::string out_file)
: in_file (in_file), out_file (out_file) {
}
void Assembler::assemble () {
first_pass ();
std::cout << "-- Done with first pass" << std::endl;
second_pass ();
std::cout << "-- Done with second pass" << std::endl;
}
void Assembler::first_pass () {
int line_number = 0;
Parser parser (in_file);
while (true) {
parser.advance ();
if (!parser.hasMoreCommands ())
break;
if (parser.isPseudoCommand ()) {
std:;
std::string symbol;
parser.get_pseudo_fields (symbol);
if (table.contains (symbol))
continue;
table.add_symbol (symbol, line_number);
} else {
line_number++;
}
}
}
void Assembler::second_pass () {
Parser parser (in_file);
// generate the output file
std::ofstream fout;
fout.open (out_file);
if (fout.fail ()) {
std::cout << "Failed to open the output file";
std::terminate ();
}
while (true) {
parser.advance ();
if (!parser.hasMoreCommands ())
break;
if (parser.isInstructionA ()) {
// Write the op code
fout << '0';
// Extract the value
std::string value;
parser.get_a_fileds (value);
// If numberic or not
if (std::all_of (value.begin (), value.end (), ::isdigit)) {
fout << std::bitset<15> (stoi (value)) << std::endl;
} else {
if (!table.contains (value)) {
table.add_variable (value);
}
fout << std::bitset<15> (table.get_address (value)) << std::endl;
}
} else if (parser.isInstructionC ()) {
std::string dest, comp, jmp;
parser.get_c_fields (dest, comp, jmp);
// std::cout << comp << std::endl;
fout << "111" << encoder.encode_comp (comp) << encoder.encode_dest (dest)
<< encoder.encode_jmp (jmp) << std::endl;
}
}
}