-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.cpp
More file actions
383 lines (305 loc) · 14.7 KB
/
asm.cpp
File metadata and controls
383 lines (305 loc) · 14.7 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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#define FIRST_OMITTED_LABEL "LOAD_SPRITES:"
#define LAST_OMITTED_LABEL "DRAW_PIXEL:"
void load_map_INSTR_SET(std::map<std::string, int>& in);
void load_map_REG(std::map<std::string, int>& in);
std::string trim(const std::string& str);
int main(int argc, char* argv[]){
//2 string inputs, input.txt and output.txt
if(argc < 4){
std::cerr << "Too few input arguments\n";
exit(-1);
}
std::string in_file_name = argv[1];
std::string out_file_name = argv[2];
std::string debug_file_name = argv[3];
std::ifstream in_file(in_file_name);
std::ofstream out_file(out_file_name);
std::ofstream debug_file(debug_file_name);
if(!in_file.is_open()){
std::cerr << "Error: '" << in_file_name << "' not opened\n";
exit(-1);
}if(!out_file.is_open()){
std::cerr << "Error: '" << out_file_name << "' not opened\n";
exit(-1);
}if(!debug_file.is_open()){
std::cerr << "Error: '" << debug_file_name << "' not opened\n";
exit(-1);
}
//maps for tracking instr, reg and labes
std::map<std::string, int> INSTR_SET;
std::map<std::string, int> REG;
std::map<std::string, int> LABELS;
load_map_INSTR_SET(INSTR_SET);
load_map_REG(REG);
//string and string_stream vectors 2b loaded with lines from input.txt
std::vector<std::string> lines;
std::vector<std::stringstream> lines_stream;
std::stringstream temp_ss;
std::string line, str, str1, str2, str3;
//read from file line by line, ignoring emtpy lines and trimming whitespace @ start and end of line
int line_count = 0;
while(std::getline(in_file, line)){
line = trim(line);
if(line != ""){
//intercepts MACROS macro instruction (op addr data)
if(line.substr(0,8) == "LOAD_RAM"){
temp_ss.str(line);
str.clear(); str1.clear(); str2.clear(); str3.clear();
temp_ss >> str >> str1 >> str2 >> str3;
temp_ss.str(""); temp_ss.clear();
temp_ss << "MOV G0 " << str2;
lines.push_back(temp_ss.str());
temp_ss.str(""); temp_ss.clear();
temp_ss << "SW ZERO G0 " << str1;
lines.push_back(temp_ss.str());
line_count = line_count + 2;
//intercepts BGT
}else if(line.substr(0,3) == "BGT"){
temp_ss.str(line);
str.clear(); str1.clear(); str2.clear(); str3.clear();
temp_ss >> str >> str1 >> str2 >> str3;
temp_ss.str(""); temp_ss.clear();
temp_ss << "BLT " << str2 << " " << str1 << " " << str3;
lines.push_back(temp_ss.str());
}else{
lines.push_back(line);
line_count++;
}
}
}std::cout << "line_count is " << line_count << "\n\n";
//copying lines[] to a stringstream lines_stream
//std::cout << "lines[]\n=======\n";
for(const auto& line : lines){
//std::cout << line << '\n';
lines_stream.push_back(std::stringstream(line));
}//std::cout << '\n';
//storing temporary strings emptying the lines_streams vector into op_code, term1, term2 and term3 vectors
int jj = 0;
std::vector<std::string> op_code, term1, term2, term3;
std::vector<int> lines_with_labels;
//std::cout << "First word of lines_stream[]\n============================\n";
for(uint ii = 0; ii < lines_stream.size(); ii++){
str.clear(); str1.clear(); str2.clear(); str3.clear();
lines_stream[ii] >> str >> str1 >> str2 >> str3;
//std::cout << ii << ": " << str << '\n';
if(INSTR_SET.find(str) == INSTR_SET.end() && str.at(0) != '#'){
LABELS[str] = ii - jj;
lines_with_labels.push_back(ii - jj);
jj++; //progressive offset so that labels will correctly allign with program
}else if(str.at(0) == '#'){
jj++; //progressive offset
}else{
//filtering out comments, and pushing to string vectors
op_code.push_back(str);
if(str1[0] != '#'){
term1.push_back(str1);
}else{
term1.push_back("");
}
if(str2[0] != '#'){
term2.push_back(str2);
}else{
term2.push_back("");
}
if(str3[0] != '#'){
term3.push_back(str3);
}else{
term3.push_back("");
}
}
}LABELS.erase(""); //errasing dummy pair("", 0) that is created when empty strings are read from in.txt
//omiting print to stdout for this range
uint skip_start = LABELS[FIRST_OMITTED_LABEL];
uint skip_end = LABELS[LAST_OMITTED_LABEL];
//cout of final instructions
std::cout << "Final Instructions\n(PC# INSTR TERM1 TERM2 TERM3)\n";
std::cout << "======================================\n";
int count = 0;
for(uint ii = 0; ii < op_code.size(); ii++){
//newline if label points to this line, and prints label
if(std::find(lines_with_labels.begin(), lines_with_labels.end(), ii) != lines_with_labels.end()){
std::cout << "\n\n";
for (const auto& label : LABELS) {
if ((uint)label.second == ii) {
std::cout << label.first;
}
}//intructions w/ formatting
}if(ii < skip_start || ii > skip_end){
std::cout << '\n' << std::setw(4) << std::right << count << ": " << std::setw(6) <<
op_code[ii] << '\t' << std::setw(6) << term1[ii] << '\t' << std::setw(6) << term2[ii] <<
'\t' << std::setw(6) << term3[ii] << '\t';
}else if(ii == skip_start){
std::cout << "\t#Begining of omitted instructions#";
}else if(ii == skip_end){
std::cout << "\t#End of omitted instructions#";
}count++;
}std::cout << "\n\n";
//cout of labels in order of instruction they point to
std::cout << "LABELS[]\n" << "========\n";
for(int ii = 0; ii < line_count; ii++){
for (const auto& label : LABELS) {
if (label.second == ii) {
std::cout << std::left << std::setw(24) << label.first << '\t' << std::right << std::setw(4) << ii << '\n';
}
}
}std::cout << '\n';
//additoin debug, prints out addition maps
/*
std::cout << "INSTR_SET\n" << "=========\n";
for(const auto& pair : INSTR_SET){
std::cout << "Key: " << pair.first << ", Value: " << pair.second << '\n';
}std::cout << '\n';
std::cout << "REG\n" << "===\n";
for(const auto& pair : REG){
std::cout << "Key: " << pair.first << ", Value: " << pair.second << '\n';
}std::cout << '\n';
*/
//for formatting, as seen in if statement excluding ii = 0, there is a better way to do this, im lazy
out_file << "v2.0 raw\n";
//iterates through 4 string vectors, converting to machine code & printing out
//assembly instr, switch case, and output machine code hex in 3 consecutive lines
//each switch case corresponsed to the instruction#
debug_file << "INSTR Conversion\n================\n";
for(uint ii = 0; ii < op_code.size(); ii++){
debug_file << std::dec << "PC#: " << ii << '\n' << op_code[ii] << " " << term1[ii]<< " " << term2[ii]<< " " << term3[ii]<< " " << '\n';
switch (INSTR_SET[op_code[ii]]){
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << REG[term2[ii]] << REG[term3[ii]] << "000\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << REG[term2[ii]] << REG[term3[ii]] << "000\n";
break;
case 9:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << REG[term2[ii]] << "0000\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << REG[term2[ii]] << "0000\n";
break;
case 11: case 12: case 13:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << REG[term2[ii]] << term3[ii] << "\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << REG[term2[ii]] << term3[ii] << '\n';
break;
case 14: case 15:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << REG[term2[ii]] << REG[term3[ii]] << "000\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << REG[term2[ii]] << REG[term3[ii]] << "000\n";
break;
case 16: case 17:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << REG[term2[ii]] << term3[ii] << "\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << REG[term2[ii]] << term3[ii] << '\n';
break;
case 18: case 19: case 20:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << REG[term2[ii]];
debug_file << std::setw(4) << std::setfill('0') << std::hex << std::uppercase << LABELS[term3[ii]] << "\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << REG[term2[ii]];
out_file << std::setw(4) << std::setfill('0') << std::hex << std::uppercase << LABELS[term3[ii]] << '\n';
break;
case 21:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << std::setw(4) << std::setfill('0') << LABELS[term1[ii]] << "00\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << std::setw(4) << std::setfill('0') << LABELS[term1[ii]] << "00\n";
break;
case 22:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << std::setw(4) << std::setfill('0') << LABELS[term1[ii]];
debug_file << REG[term2[ii]] << "0\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << std::setw(4) << std::setfill('0') << LABELS[term1[ii]];
out_file << REG[term2[ii]] << "0\n";
break;
case 23:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << "00000\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << "00000\n";
break;
case 26:
debug_file << std::dec << "CASE: " << INSTR_SET[op_code[ii]] << std::hex << '\n';
debug_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
debug_file << REG[term1[ii]] << term2[ii] << "0\n\n";
out_file << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << INSTR_SET[op_code[ii]];
out_file << REG[term1[ii]] << term2[ii] << "0\n";
break;
default:
break;
}
}return 0;
}
void load_map_INSTR_SET(std::map<std::string, int>& in){
in["ADD"] = 0;
in["SUB"] = 1;
in["MUL"] = 2;
in["DIV"] = 3;
in["BSR"] = 4;
in["BSL"] = 5;
in["AND"] = 6;
in["OR"] = 7;
in["XOR"] = 8;
in["NOT"] = 9;
in["A_PASS"] = 10;
in["ADDI"] = 11;
in["SUBI"] = 12;
in["MULI"] = 13;
in["SLT"] = 14;
in["SEQ"] = 15;
in["LW"] = 16;
in["SW"] = 17;
in["BLT"] = 18;
in["BEQ"] = 19;
in["BNE"] = 20;
in["JMP"] = 21;
in["JAL"] = 22;
in["JR"] = 23;
in["MOV"] = 26;
}
void load_map_REG(std::map<std::string, int>& in){
in["ZERO"] = 0;
in["HI"] = 1;
in["KB"] = 2;
in["PC"] = 3;
in["SP"] = 4;
in["LR"] = 5;
in["BUFF"] = 6;
in["CLK"] = 7;
in["G0"] = 8;
in["G1"] = 9;
in["G2"] = 10;
in["G3"] = 11;
in["G4"] = 12;
in["G5"] = 13;
in["G6"] = 14;
in["G7"] = 15;
}
//filters out whitespace from beging and end of string no idea how this works, i stole it from gpt :)
std::string trim(const std::string& str) {
// Find the first non-whitespace character
auto start = std::find_if_not(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); });
// Find the last non-whitespace character
auto end = std::find_if_not(str.rbegin(), str.rend(), [](unsigned char c) { return std::isspace(c); }).base();
// Create a trimmed string if valid range exists
return (start < end) ? std::string(start, end) : std::string();
}