-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIPSDisassembler.java
More file actions
76 lines (75 loc) · 3.75 KB
/
MIPSDisassembler.java
File metadata and controls
76 lines (75 loc) · 3.75 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
public class MIPSDisassembler {
public static void disassemble(int instruction, int address) {
int opcode = (instruction & 0xFC000000) >>> 26;
// If opcode == 0, then this indicates an R-format instruction
if (opcode == 0) {
int destreg = (instruction & 0x0000F800) >>> 11;
int src1reg = (instruction & 0x03E00000) >>> 21;
int src2reg = (instruction & 0x001F0000) >>> 16;
int shamt = (instruction & 0x000007C0) >>> 6;
int funct = instruction & 0x3F;
// Switch statement for r-format: add, sub, and, or, slt
switch (funct) {
case 0x20:
System.out.println(String.format("%X add $%d, $%d, $%d", address, destreg, src1reg, src2reg));
break;
case 0x22:
System.out.println(String.format("%X sub $%d, $%d, $%d", address, destreg, src1reg, src2reg));
break;
case 0x24:
System.out.println(String.format("%X and $%d, $%d, $%d", address, destreg, src1reg, src2reg));
break;
case 0x25:
System.out.println(String.format("%X or $%d, $%d, $%d", address, destreg, src1reg, src2reg));
break;
case 0x2A:
System.out.println(String.format("%X slt $%d, $%d, $%d", address, destreg, src1reg, src2reg));
break;
default:
System.out.println(String.format("%X Unknown instruction", address));
break;
}
// If opcode does not equal 0, then this indicates an I-format instruction like lw, sw, beq, bne
// load word
} else if (opcode == 0x23) {
int src1reg = (instruction & 0x03E00000) >>> 21;
int src2reg = (instruction & 0x001F0000) >>> 16;
short offset = (short) (instruction & 0xFFFF);
System.out.println(String.format("%X lw $%d, %d($%d)", address, src1reg, offset, src2reg));
// store word
} else if (opcode == 0x2B) {
int src1reg = (instruction & 0x03E00000) >>> 21;
int src2reg = (instruction & 0x001F0000) >>> 16;
short offset = (short) (instruction & 0xFFFF);
System.out.println(String.format("%X sw $%d, %d($%d)", address, src1reg, offset, src2reg));
// branch on equal
} else if (opcode == 0x4) {
int src1reg = (instruction & 0x03E00000) >>> 21;
int src2reg = (instruction & 0x001F0000) >>> 16;
short offset = (short) (instruction & 0xFFFF);
int destAddress = address + 4 + (offset << 2);
System.out.println(String.format("%X beq $%d, $%d, Address 0x%X", address, src1reg, src2reg, destAddress));
// branch on not equal
} else if (opcode == 0x5) {
int src1reg = (instruction & 0x03E00000) >>> 21;
int src2reg = (instruction & 0x001F0000) >>> 16;
short offset = (short) (instruction & 0xFFFF);
int destAddress = address + 4 + (offset << 2);
System.out.println(String.format("%X bne $%d, $%d, Address 0x%X", address, src1reg, src2reg, destAddress));
}
}
// Actual instructions we will be disassembling
public static void main(String[] args) {
int[] instructions = {
0x032BA020, 0x8CE90014, 0x12A90003, 0x022DA822, 0xADB30020, 0x02697824, 0xAE8FFFF4,
0x018C6020, 0x02A4A825, 0x158FFFF7, 0x8ECDFFF0
};
// Starting address
int address = 0x9A040;
// Calls disassemble method for each instruction and loops
for (int instr : instructions) {
disassemble(instr, address);
address += 4;
}
}
}