-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisassembler.java
More file actions
276 lines (256 loc) · 7.19 KB
/
Disassembler.java
File metadata and controls
276 lines (256 loc) · 7.19 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
import java.util.*;
//Class Disassembler
public class Disassembler
{
//note to self. Substring numbers are wrong because the actual binary input
//numbers will be in reverse.
public static void main(String[] args)
{
//String binBits = setBits("00000000001000110001000000100000");
//String binBits = setBits("00001010000001000000000000000001");
String binBits = setBits("00100000111010000000000110001111");
String registers[] = {"$zero","$at","$v0","$v1","$a0","$a1","$a2","$a3","$t0","$t1","$t2","$t3","$t4",
"$t5","$t6","$t7","$s0","$s1","$s2","$s3","$s4","$s5","$s6","$s7","$t8","$t9","$k0","$k1","$gp","$sp",
"$fp","$ra"};
int fType = opcode(binBits.substring(0,6));
int rs,rt,rd,shamt;
String instrName = new String();
String jAddress = new String();
int immediate;
if(fType == 0) //do instructions for r-types
{
rs = decodeReg(binBits.substring(6,11));
rt = decodeReg(binBits.substring(11,16));
rd = decodeReg(binBits.substring(16,21));
shamt = decodeShamt(binBits.substring(21,26));
instrName = decodeRFunc(binBits.substring(26,32));
if(instrName.equals("syscall"))
{
System.out.println(instrName);
}
else if(shamt > 0)
{
System.out.println(instrName + " " + registers[rd] + ", " + registers[rt] + ", " + shamt + "(" + registers[rs] + ")");
}
else
System.out.println(instrName + " " + registers[rd] + ", " + registers[rt] + ", " + registers[rs]);
}
else if(fType == 1) //do instructions for j-types
{
instrName = decodeIJFunc(binBits.substring(0,6));
jAddress = decodeJAddress(binBits.substring(6));
System.out.println(instrName + " " + jAddress);
}
else //do instructions for i-type
{
instrName = decodeIJFunc(binBits.substring(0,6));
rs = decodeReg(binBits.substring(6,11));
rt = decodeReg(binBits.substring(11,16));
immediate = decodeImmField(binBits.substring(16));
System.out.println(instrName + " " + registers[rt] + " " + registers[rs] + " " + immediate);
}
}
static String setBits(String b)
{
return b;
}
//decodes the opcode
static int opcode(String bits)
{
if(bits.equals("000000"))
return 0; //r-format
else if(bits.equals("000010") || bits.equals("000011"))
return 1; //j-format
else
return 2; //i-format
}
//decodes the register bits
static int decodeReg(String bits)
{
//starts at the fifth bit and works towards to first adding the appropriate power of 2
int reg=0;
if(bits.charAt(4) == '1')
reg += 1;
if(bits.charAt(3) == '1')
reg += 2;
if(bits.charAt(2) == '1')
reg += 4;
if(bits.charAt(1) == '1')
reg += 8;
if(bits.charAt(0) == '1')
reg += 16;
return reg;
}
//decodes shamt
static int decodeShamt(String bits)
{
//starts at the 5th bit and goes to the first while adding the correct power of 2
int offset = 0;
if(bits.charAt(4) == '1')
offset += 1;
if(bits.charAt(3) == '1')
offset += 2;
if(bits.charAt(2) == '1')
offset += 4;
if(bits.charAt(1) == '1')
offset += 8;
if(bits.charAt(0) == '1')
offset += 16;
return offset;
}
//decodes the function name for r-type instruction and syscall
static String decodeRFunc(String bits)
{
//finds the value of the bits
String funcName = new String();
int func = 0;
if(bits.charAt(5) == '1')
func += 1;
if(bits.charAt(4) == '1')
func += 2;
if(bits.charAt(3) == '1')
func += 4;
if(bits.charAt(2) == '1')
func += 8;
if(bits.charAt(1) == '1')
func += 16;
if(bits.charAt(0) == '1')
func += 32;
//searches for a matching hex code to for the value of the bits passed to it
//and returns the name of the instruction
if(func == 0x20)
funcName = "Add";
else if(func == 0x21)
funcName = "addu";
else if(func == 0x24)
funcName = "and";
else if(func == 0x08)
funcName = "jr";
else if(func == 0x27)
funcName = "nor";
else if(func == 0x25)
funcName = "or";
else if(func == 0x2a)
funcName = "slt";
else if(func == 0x2b)
funcName = "sltu";
else if(func == 0x00)
funcName = "sll";
else if(func == 0x02)
funcName = "srl";
else if(func == 0x22)
funcName = "sub";
else if(func == 0x23)
funcName = "subu";
else if(func == 0xc)
funcName = "syscall";
return funcName;
}
//decodes the function name for I and J types
//conflict in the opcode/function code with addi and syscall
static String decodeIJFunc(String bits)
{
//finds the value of the bits passed to it
String funcName = new String();
int func = 0;
if(bits.charAt(5) == '1')
func += 1;
if(bits.charAt(4) == '1')
func += 2;
if(bits.charAt(3) == '1')
func += 4;
if(bits.charAt(2) == '1')
func += 8;
if(bits.charAt(1) == '1')
func += 16;
if(bits.charAt(0) == '1')
func += 32;
//compares the value of bits passed to it and compares that to the
//hex values for the instructions. Returns instruction name
if(func == 0x8)
funcName = "addi";//i-format
else if(func == 0x9)
funcName = "addiu";
else if(func == 0xc)
funcName = "andi";
else if(func == 0x4)
funcName = "beq";
else if(func == 0x5)
funcName = "bne";
else if(func == 0x24)
funcName = "lbu";
else if(func == 0x25)
funcName = "lhu";
else if(func == 0x30)
funcName = "ll";
else if(func == 0xf)
funcName = "lui";
else if(func == 0x23)
funcName = "lw";
else if(func == 0xd)
funcName = "ori";
else if(func == 0xa)
funcName = "slti";
else if(func == 0xb)
funcName = "sltiu";
else if(func == 0x28)
funcName = "sb";
else if(func == 0x38)
funcName = "sc";
else if(func == 0x29)
funcName = "sh";
else if(func == 0x2b)
funcName = "sw";
else if(func == 0x2)//j-types
funcName = "j";
else if(func == 0x3)
funcName = "jal";
return funcName;
}
//decodes the 26 bit address for J type instructions
static String decodeJAddress(String bits)
{
String address = new String();
String hexAddress = new String();
int binToHex;
//sign extend for 26 bit address. Can replace 4 0's if PC counter is known.
address = "0000" + bits + "00";
for(int i = 0;i<32;i=i+4)
{
//finds the numerical value of the four bits that it is looking at
binToHex = 0;
if(address.charAt(i+3) == '1')
binToHex += 1;
if(address.charAt(i+2) == '1')
binToHex += 2;
if(address.charAt(i+1) == '1')
binToHex += 4;
if(address.charAt(i) == '1')
binToHex += 8;
//takes the numerical value of the four bits, converts it to hex and adds that to the hexadecimal address.
if(binToHex < 10)
hexAddress += Integer.toString(binToHex);
else if(binToHex == 10)
hexAddress += 'A';
else if(binToHex == 11)
hexAddress += 'B';
else if(binToHex == 12)
hexAddress += 'C';
else if(binToHex == 13)
hexAddress += 'D';
else if(binToHex == 14)
hexAddress += 'E';
else if(binToHex == 15)
hexAddress += 'F';
}
return hexAddress;
}
static int decodeImmField(String bits)
{
int value = 0;
for(int i = 0;i<16;i++)
if(bits.charAt(i) == '1')
value += 2^i;
return value;
}
}