-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisassembler.cpp
More file actions
312 lines (272 loc) · 9.63 KB
/
disassembler.cpp
File metadata and controls
312 lines (272 loc) · 9.63 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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
using namespace std;
// CS2323 Lab-3: RISC-V Disassembler
// Submission by Edward Nathan Varghese - CS22BTECH11020
int PC = 0;
map <int, bool> labelled;
map <int, int> PC_to_label;
// Function Prototypes
string HexToBinary (string, map <char, string>&);
void initializeMap (map <char, string>&);
string BinToAssembly (string, string);
string BinToReg (string);
string BinToImm (string, bool);
int BinToImmB (string, bool);
int BinToImmJ (string, bool);
int main (void) {
freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);
ifstream read ("input.txt");
map <char, string> binmap;
initializeMap (binmap);
string s, bin;
vector <string> lines;
// Reading lines and passing it to BinToAssembly
while (read >> s){
if (!s.length()) break;
bin = HexToBinary (s, binmap);
if (!labelled[PC]) labelled[PC] = false;
lines.push_back(BinToAssembly (bin, s));
PC += 4;
}
// Assigning labels
map <int, string> text_label;
int label_counter = 1;
for (int pc = 0; pc < PC; pc += 4){
if (labelled[pc]){
text_label[pc] = "L" + to_string(label_counter);
label_counter++;
lines[pc/4] = text_label[pc] + ": " + lines[pc/4];
}
}
// Printing out the lines
for (int pc = 0; pc < PC; pc += 4){
if (PC_to_label[pc]){
if (PC_to_label[pc] - 4 <= 0 || PC_to_label[pc] - 4 >= PC);
else{
for (int i = lines[pc/4].length() - 1; i > -1; i--){ // deleting the offset
if (lines[pc/4][i] == ' '){
lines[pc/4] = lines[pc/4].substr(0, i + 1);
break;
}
}
lines[pc/4] += text_label[PC_to_label[pc] - 4];
}
}
cout << lines[pc/4] << '\n';
}
return 0;
}
// Converts the given binary encoding to an assembly instruction
string BinToAssembly (string bin, string hex){
string opcode = bin.substr(25, 7);
string ins = "", invalid = "invalid";
// For R-format Instructions, the opcode is 0110011
if(opcode == "0110011"){
string rs1 = bin.substr(12, 5), rs2 = bin.substr(7, 5), funct7 = bin.substr(0, 7);
string funct3 = bin.substr(17, 3), rd = bin.substr(20, 5);
if (funct7 == "0000000"){
if (funct3 == "000") ins += "add ";
else if (funct3 == "100") ins += "xor ";
else if (funct3 == "110") ins += "or ";
else if (funct3 == "111") ins += "and ";
else if (funct3 == "001") ins += "sll ";
else if (funct3 == "101") ins += "srl ";
else if (funct3 == "010") ins += "slt ";
else if (funct3 == "011") ins += "sltu ";
else return invalid;
}
else if (funct7 == "0100000"){
if (funct3 == "000") ins += "sub ";
else if (funct3 == "101") ins += "sra ";
else return invalid;
}
else return invalid;
ins += BinToReg (rd) + ", ";
ins += BinToReg (rs1) + ", ";
ins += BinToReg (rs2);
return ins;
}
// For I-format Instructions, the opcodes are 0010011, 0000011 and 1100111
else if(opcode == "0010011" || opcode == "0000011" || opcode == "1100111"){
string rs1 = bin.substr(12, 5), imm = bin.substr(0, 12);
string funct3 = bin.substr(17, 3), rd = bin.substr(20, 5);
bool sign = true;
if(opcode == "0010011"){
if (funct3 == "000") ins += "addi ";
else if (funct3 == "100") ins += "xori ";
else if (funct3 == "110") ins += "ori ";
else if (funct3 == "111") ins += "andi ";
else if (funct3 == "001") ins += "slli ";
else if (funct3 == "101"){
if (bin.substr(0, 6) == "000000") ins += "srli ";
else ins += "srai ";
}
else if (funct3 == "010") ins += "slti ";
else if (funct3 == "011"){
ins += "sltiu ";
sign = false;
}
else return invalid;
}
else if(opcode == "0000011"){
sign = true;
if (funct3 == "000") ins += "lb ";
else if (funct3 == "001") ins += "lh ";
else if (funct3 == "010") ins += "lw ";
else if (funct3 == "011") ins += "ld ";
else if (funct3 == "100"){
ins += "lbu ";
sign = false;
}
else if (funct3 == "101"){
ins += "lhu ";
sign = false;
}
else if (funct3 == "110"){
ins += "lwu ";
sign = false;
}
else return invalid;
ins += BinToReg (rd) + ", ";
ins += BinToImm (imm, sign);
ins += "(" + BinToReg (rs1) + ")";
return ins;
}
else if(opcode == "1100111"){ // jalr
if (funct3 == "000") ins += "jalr ";
else return invalid;
}
ins += BinToReg (rd) + ", ";
ins += BinToReg (rs1) + ", ";
ins += BinToImm (imm, sign);
return ins;
}
// For S-format Instructions, the opcode is 0100011
else if(opcode == "0100011"){
string rs1 = bin.substr(12, 5), rs2 = bin.substr(7, 5);
string funct3 = bin.substr(17, 3), imm = bin.substr(0, 7) + bin.substr(20, 5);
if (funct3 == "000") ins += "sb ";
else if (funct3 == "001") ins += "sh ";
else if (funct3 == "010") ins += "sw ";
else if (funct3 == "011") ins += "sd ";
else return invalid;
ins += BinToReg (rs2) + ", ";
ins += BinToImm (imm, true);
ins += "(" + BinToReg (rs1) + ")";
return ins;
}
// For B-format Instructions, the opcode is 1100011
else if(opcode == "1100011"){
string rs1 = bin.substr(12, 5), rs2 = bin.substr(7, 5);
string funct3 = bin.substr(17, 3);
string imm = bin.substr(0, 1) + bin.substr(24, 1) + bin.substr(1, 6) + bin.substr(20, 4);
bool sign = true;
if (funct3 == "000") ins += "beq ";
else if (funct3 == "001") ins += "bne ";
else if (funct3 == "100") ins += "blt ";
else if (funct3 == "101"){
ins += "bge ";
sign = false;
}
else if (funct3 == "110"){
ins += "bltu ";
sign = false;
}
else if (funct3 == "111"){
ins += "bgeu ";
sign = false;
}
else return invalid;
if (PC + BinToImmB (imm, sign) > -1) labelled[PC + BinToImmB (imm, sign)] = true;
PC_to_label[PC] = PC + BinToImmB (imm, true) + 4;
ins += BinToReg (rs1) + ", ";
ins += BinToReg (rs2) + ", ";
ins += to_string(BinToImmB (imm, sign));
return ins;
}
// For J-format Instructions (jal), the opcode is 1101111
else if(opcode == "1101111"){
string rd = bin.substr(20, 5);
string imm = bin.substr(0, 1) + bin.substr(12, 8) + bin.substr(11, 1) + bin.substr(1, 10);
if (PC + BinToImmJ (imm, true) > -1) labelled[PC + BinToImmJ (imm, true)] = true;
PC_to_label[PC] = PC + BinToImmJ (imm, true) + 4;
ins += "jal ";
ins += BinToReg (rd) + ", ";
ins += to_string(BinToImmJ (imm, true));
return ins;
}
// For U-format Instructions (lui), the opcode is 0110111
else if(opcode == "0110111"){
string rd = bin.substr(20, 5);
for (int i = 0; i < 5; i++) hex[i] = tolower (hex[i]);
ins += "lui ";
ins += BinToReg (rd) + ", ";
ins += "0x" + hex.substr(0, 5);
return ins;
}
else return invalid;
}
// Converts a 5 digit binary to its respective register
string BinToReg (string s){
int num = 0, x = 16;
for (int i = 0; i < 5; i++){
if (s[i] == '1') num += x;
x /= 2;
}
return "x" + to_string(num);
}
// Converts Binary to Immediate
string BinToImm (string s, bool sign){
int num = 0, x = 2048;
for (int i = 0; i < 12; i++){
if (s[i] == '1') num += x;
x /= 2;
}
if (sign && num > 2047) num = 2047 - num;
return to_string(num);
}
// Converts Binary to Immediate
int BinToImmB (string s, bool sign){
int num = 0, x = 4096;
for (int i = 0; i < 12; i++){
if (s[i] == '1') num += x;
x /= 2;
}
if (sign && num > 4095) num = 4095 - num;
return num;
}
// Converts Binary to Immediate
int BinToImmJ (string s, bool sign){
int num = 0, x = 1048576;
for (int i = 0; i < 20; i++){
if (s[i] == '1') num += x;
x /= 2;
}
if (sign && num > 1048575) num = 1048575 - num;
return num;
}
// Converts Hexadecimal to Binary
string HexToBinary (string hex, map <char, string>& binmap){
string bin = "", bad = "00000000000000000000000000000000";
for (int i = 0; i < hex.length(); i++){
hex[i] = tolower(hex[i]);
if (!(hex[i] <= '9' && hex[i] >= '0') && !(hex[i] <= 'f' && hex[i] >= 'a')){
bin = bad;
break;
}
bin += binmap[hex[i]];
}
return bin;
}
// Initializes the Hexadecimal to Binary Map
void initializeMap (map <char, string>& binmap){
binmap['1'] = "0001"; binmap['5'] = "0101"; binmap['9'] = "1001"; binmap['d'] = "1101";
binmap['2'] = "0010"; binmap['6'] = "0110"; binmap['a'] = "1010"; binmap['e'] = "1110";
binmap['3'] = "0011"; binmap['7'] = "0111"; binmap['b'] = "1011"; binmap['f'] = "1111";
binmap['4'] = "0100"; binmap['8'] = "1000"; binmap['c'] = "1100"; binmap['0'] = "0000";
}