-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.cpp
More file actions
187 lines (166 loc) · 5.27 KB
/
assembler.cpp
File metadata and controls
187 lines (166 loc) · 5.27 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
#include <iostream>
#include <fstream>
#include <map>
#include <bitset>
#include <string>
std::string get_destination(const std::string& instruction){
int position = instruction.find('=');
if (position != std::string::npos) {
return instruction.substr(0, position);
}
return "";
}
std::string get_computation(const std::string& instruction) {
int start_position = instruction.find("=");
int end_position = instruction.find(";");
if (start_position != std::string::npos && end_position != std::string::npos) {
return instruction.substr(start_position + 1, end_position - start_position - 1);
}
if (start_position == std::string::npos) {
return instruction.substr(0, end_position);
}
if (end_position == std::string::npos) {
return instruction.substr(start_position + 1, instruction.size() - start_position - 1);
}
return "ERROR";
}
std::string get_jump(const std::string& instruction){
int start_position = instruction.find(";") + 1;
if (start_position != std::string::npos) {
return instruction.substr(start_position, 3);
}
return "";
}
int main() {
//Edit to be path to your files from working directory
const std::string path = "./Desktop/proj_euler/";
std::cout << "Assuming path is " << path << std::endl << "Enter file name to assemble: ";
std::string inFileName;
std::cin >> inFileName;
std::ifstream file(path + inFileName);
if(!file.is_open()) {
std::cerr << "Error, could not find file; ensure you have read the readme.md" << std::endl;
return 1;
}
//First sweep to check for tokens
std::map<std::string, int> tokens;
std::pair<std::string, int> data_tokens[23] = {
std::make_pair("SP", 0),
std::make_pair("LCL", 1),
std::make_pair("ARG", 2),
std::make_pair("THIS", 3),
std::make_pair("THAT", 4),
std::make_pair("SCREEN", 16384),
std::make_pair("KBD", 24576)
};
for (int i = 0; i<16; ++i){
data_tokens[i + 7] = std::make_pair("R" + std::to_string(i), i);
}
for (int i = 0; i<23; ++i){
tokens.insert(data_tokens[i]);
}
std::string line;
int line_number = 0;
while(file >> line){
//if A-instruction
if(line.at(0) == '(' && !isdigit(line.at(1))){
//if token
line.erase(0, 1);
line.erase(line.size() - 1);
tokens.insert(make_pair(line, line_number));
line_number--;
}
if(line.at(0) == '/'){
line_number--;
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
line_number++;
}
//Second sweep to translate instructions
std::map<std::string, std::string> jumps;
std::pair<std::string, std::string> data_jumps[] = {
std::make_pair("JGT", "001"),
std::make_pair("JEQ", "010"),
std::make_pair("JGE", "011"),
std::make_pair("JLT", "100"),
std::make_pair("JNE", "101"),
std::make_pair("JLE", "110"),
std::make_pair("JMP", "111")
};
for(int i=0; i<7; ++i){
jumps.insert(data_jumps[i]);
}
std::map<std::string, std::string> computations;
std::pair<std::string, std::string> data[] = {
std::make_pair("0", "0101010"),
std::make_pair("1", "0111111"),
std::make_pair("-1", "0111010"),
std::make_pair("D", "0001100"),
std::make_pair("A", "0110000"),
std::make_pair("M", "1110000"),
std::make_pair("!D", "0001101"),
std::make_pair("!A", "0110001"),
std::make_pair("!M", "1110001"),
std::make_pair("-D", "0001111"),
std::make_pair("-A", "0110011"),
std::make_pair("-M", "1110011"),
std::make_pair("D+1", "0011111"),
std::make_pair("A+1", "0110111"),
std::make_pair("M+1", "1110111"),
std::make_pair("D-1", "0001110"),
std::make_pair("A-1", "0110010"),
std::make_pair("M-1", "1110010"),
std::make_pair("D+A", "0000010"),
std::make_pair("D+M", "1000010"),
std::make_pair("D-A", "0010011"),
std::make_pair("D-M", "1010011"),
std::make_pair("A-D", "0000111"),
std::make_pair("M-D", "1000111"),
std::make_pair("D&A", "0000000"),
std::make_pair("D&M", "1000000"),
std::make_pair("D|A", "0010101"),
std::make_pair("D|M", "1010101")
};
for (int i = 0; i < 28; ++i) {
computations.insert(data[i]);
}
file.clear();
file.seekg(0);
int minimum_variable_memory_address = 16;
while(file >> line){
if(line.at(0) == '/') {
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} else {
if(line.at(0) == '@'){
//if A-instruction
line.erase(0,1);
if(isdigit(line.at(0))){
std::cout << '0' << std::bitset<15>(std::stoi(line)) << std::endl;
} else {
if(tokens.count(line) > 0){
std::cout << '0' << std::bitset<15>(tokens[line]) << std::endl;
} else {
std::cout << '0' << std::bitset<15>(minimum_variable_memory_address) << std::endl;
tokens.insert(make_pair(line, minimum_variable_memory_address));
minimum_variable_memory_address++;
}
}
} else if (line.at(0) != '(') {
std::string instruction = "111";
const std::string computation = get_computation(line);
instruction += computations[computation];
const std::string destination = get_destination(line);
instruction += destination.find('A') != std::string::npos ? '1' : '0';
instruction += destination.find('D') != std::string::npos ? '1' : '0';
instruction += destination.find('M') != std::string::npos ? '1' : '0';
if(line.find(";") != std::string::npos){
const std::string jump = get_jump(line);
instruction += jumps[jump];
} else {
instruction += "000";
}
std::cout << instruction << std::endl;
}}
}
return 0;
}