-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass2.h
More file actions
146 lines (125 loc) · 4.55 KB
/
pass2.h
File metadata and controls
146 lines (125 loc) · 4.55 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
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
class Pass2
{
private:
map<string, string> map_compbits;
map<string, string> map_destbits;
map<string, string> map_jumpbits;
vector<string>words;
vector<string>lines;
SymbolTable symbolTable;
public:
Pass2(pair<vector<string>, SymbolTable> intermReult){
map_compbits["0"] = "101010";
map_compbits["1"] = "111111";
map_compbits["-1"] = "111010";
map_compbits["D"] = "001100";
map_compbits["A"] = "110000";
map_compbits["!D"] = "001101";
map_compbits["!A"] = "110001";
map_compbits["-D"] = "111111";
map_compbits["-A"] = "101010";
map_compbits["D+1"] = "011111";
map_compbits["A+1"] = "110111";
map_compbits["D-1"] = "001110";
map_compbits["A-1"] = "110010";
map_compbits["D+A"] = "000010";
map_compbits["D-A"] = "010011";
map_compbits["A-D"] = "000111";
map_compbits["D&A"] = "000000";
map_compbits["D|A"] = "010101";
map_destbits["null"] = "000";
map_destbits["M"] = "001";
map_destbits["D"] = "010";
map_destbits["MD"] = "011";
map_destbits["A"] = "100";
map_destbits["AM"] = "101";
map_destbits["AD"] = "110";
map_destbits["AMD"] = "111";
map_jumpbits["null"] = "000";
map_jumpbits["JGT"] = "001";
map_jumpbits["JEQ"] = "010";
map_jumpbits["JGE"] = "011";
map_jumpbits["JLT"] = "100";
map_jumpbits["JNE"] = "101";
map_jumpbits["JLE"] = "110";
map_jumpbits["JMP"] = "111";
lines = intermReult.first;
symbolTable = intermReult.second;
}
~Pass2(){
string hack_file_name = "obj";
hack_file_name+=".hack";
FILE* hack_file = fopen(hack_file_name.c_str(), "w");
for(auto w:words)
fprintf(hack_file,"%s\n",(w.c_str()));
fclose(hack_file);
lines.clear();
cout<<"\nHack file saved as "<<hack_file_name<<endl;
}
string decimalToBinary(int n)
{
string s;
for (int i = 14; i >= 0; i--) {
int k = n >> i;
if (k & 1)
s.push_back('1');
else
s.push_back('0');
}
return s;
}
void convertTowords(){
for(auto line:lines){
string str;
if(line[0]=='('){
//IGNORE
}
else if(line[0]=='@'){
str += "0";
string s = line.substr(1), val_15bit;
if(isdigit(s[0]))
val_15bit = decimalToBinary(stoi(s.c_str()));
else
val_15bit = decimalToBinary(symbolTable.getAddress(s));
str += val_15bit;
}
else {
str += "111";
string dest;
string comp;
string jump;
string aval;
if(line[1]=='='){
dest = line.substr(0,1);
comp = line.substr(2);
jump = "null";
aval = (comp.find("M")!=string::npos)?"1":"0";
for(auto &x:comp)
if(x=='M')
x = 'A';
}else{
dest = "null";
comp = line.substr(0,1);
jump = line.substr(2);
aval = (comp.find("M")!=string::npos)?"1":"0";
for(auto &x:comp)
if(x=='M')
x = 'A';
}
str = str + aval + map_compbits[comp] + map_destbits[dest] + map_jumpbits[jump];
}
words.push_back(str);
}
}
void print_16bitWords(){
cout<<"\n\n.... 16 bit Binary Word\n";
for(auto word:words)
cout<<word<<endl;
cout<<"\t\t\t~end";
}
};