-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.cpp
More file actions
131 lines (124 loc) · 3.58 KB
/
asm.cpp
File metadata and controls
131 lines (124 loc) · 3.58 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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <map>
using namespace std;
vector<string> instr = {"lw","sw","add","addi","sub","and","andi","beqz","j","halt","noop"};
vector<int> opcode={35,43,0,8,0,0,12,4,2,1,3};
vector<int> funccode={-1,-1,32,-1,34,36,-1,-1,-1,-1,-1};
// I = 0, R = 1, J = 2
vector<int> type={0,0,1,0,1,1,0,0,2,2,2};
map<string, int> labels; // (label, line number)
string line;
vector<string> split(string s,char c){
vector<string> result;
string cur = string("");
for (int i = 0; i < s.length(); i++){
// // cerr<<"s[i] = " << s[i]<<endl;
if (s[i] == c) {
// // cerr<<"s[i] == c"<<endl;
if (cur.length() == 0){
continue;
} else {
// // cerr<<"this cur is " << cur << endl;
result.push_back(cur);
cur = "";
}
} else {
// // cerr<<"s[i] != c"<<endl;
if (s[i] != '\r' && s[i]!='\n'&&s[i]!=' ')
cur.push_back(s[i]);
}
}
if (cur.length() != 0) result.push_back(cur);
return result;
}
int main(int argc, char *argv[]){
if (argc !=3) {
cout << "arg should be 3!" << endl;
return 0;
}
fstream file;
file.open(argv[1],ios::in);
int count = 0;
count = 0;
string this_line;
while(getline(file, line)) {
this_line = string("");
for (int i = 0; i < line.length(); i++){
if (line[i] == ';') break;
this_line += line[i];
}
vector<string> line_space_split = split(this_line, ' ');
auto it = find(instr.begin(), instr.end(), line_space_split[0]);
if(it == instr.end()){
labels[line_space_split[0]] = count;
// cerr<<line_space_split[0].length()<<line_space_split[0]<<" "<<labels[line_space_split[0]] <<endl;
}
count++;
}
file.close();
file.open(argv[1], ios::in);
freopen(argv[2],"w",stdout);
count = 0;
while(getline(file, line)){
// cerr<<line<<endl;
int result = 0;
this_line = string("");
for (int i = 0; i < line.length(); i++){
if (line[i] == ';') break;
this_line += line[i];
}
vector<string> line_space_split = split(this_line, ' ');
auto it = find(instr.begin(), instr.end(), line_space_split[0]);
if(it == instr.end()){
line_space_split.erase(line_space_split.begin());
it = find(instr.begin(), instr.end(), line_space_split[0]);
}
int index = distance(instr.begin(), it);
int op = opcode[index];
result |= op << 26;
if (index == 9 || index == 10) { // halt and noop
cout << result << endl;
count++;
continue;
}
int instr_type = type[index];
// cerr<<line_space_split[1]<<endl;
vector<string> regs = split(line_space_split[1],',');
// for (int i = 0; i < regs.size(); i++) {
// // cerr<<regs[i]<<" ";
// }// cerr<<endl;
if (instr_type == 0) { // I
if (index == 7) { // beqz
regs[0].erase(regs[0].begin());
result |= atoi(regs[0].c_str())<<21;
result |= ((labels[regs[1]]-count-1)&0xffff);
} else { // lw, sw, andi, addi
regs[0].erase(regs[0].begin());
regs[1].erase(regs[1].begin());
result |= atoi(regs[1].c_str())<<21;
result |= atoi(regs[0].c_str())<<16;
result |= (atoi(regs[2].c_str())&0xffff);
}
} else if (instr_type == 1) { // R: add, sub, and
regs[0].erase(regs[0].begin());
regs[1].erase(regs[1].begin());
regs[2].erase(regs[2].begin());
result |= (atoi(regs[0].c_str())<<11)|(atoi(regs[1].c_str())<<21)|(atoi(regs[2].c_str())<<16);
result |= funccode[index]&0x07ff;
} else if (instr_type == 2) { // J: j
// cerr<<count << endl;
// // cerr<< regs[0].length() << endl;
// // cerr<< labels[regs[0]] << endl;
result |= (labels[regs[0]]-count-1)&0x3ffffff;
}
cout << result << endl;
count += 1;
}
file.close();
return 0;
}