-
Notifications
You must be signed in to change notification settings - Fork 0
/
disassemble.c
executable file
·160 lines (123 loc) · 3.77 KB
/
disassemble.c
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
#include <stdio.h>
#include <stdlib.h>
#include "disassemble.h"
void disassemble(inst_t inst)
{
/* you could use itype or type instead of rtype */
/* remember that opcode field is at the same place for all types */
switch (inst.rtype.opcode)
{
case 0x0: // opcode == 0x0 (SPECIAL)
switch (inst.rtype.funct)
{
case 0x0:
printf("sll\t$%d,$%d,%u\n", inst.rtype.rd, inst.rtype.rt, inst.rtype.shamt);
break;
case 0x2:
printf("srl\t$%d,$%d,%u\n", inst.rtype.rd, inst.rtype.rt, inst.rtype.shamt);
break;
case 0x3:
printf("sra\t$%d,$%d,%u\n", inst.rtype.rd, inst.rtype.rt, inst.rtype.shamt);
break;
case 0x8:
printf("jr\t$%d\n", inst.rtype.rs);
break;
case 0x9:
printf("jalr\t$%d,$%d\n",inst.rtype.rd, inst.rtype.rs);
break;
case 0x10:
printf("mfhi\t$%d\n", inst.rtype.rd);
break;
case 0x12:
printf("mflo\t$%d\n", inst.rtype.rd);
break;
case 0x18:
printf("mult\t$%d,$%d\n", inst.rtype.rs, inst.rtype.rt);
break;
case 0x21:
printf("addu\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0x23:
printf("subu\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0x24:
printf("and\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0x26:
printf("xor\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0x27:
printf("nor\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0x2a:
printf("slt\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0x2b:
printf("sltu\t$%d,$%d,$%d\n",inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
case 0xc: // funct == 0xc (SYSCALL)
printf("syscall\n");
break;
case 0x25: // funct == 0x25 (OR)
printf("or\t$%d,$%d,$%d\n", inst.rtype.rd, inst.rtype.rs, inst.rtype.rt);
break;
default: // undefined funct
fprintf(stderr, "%s: illegal instruction: %08x\n", __FUNCTION__, inst.bits);
exit(-1);
break;
}
break;
case 0x4:
printf("beq\t$%d,$%d,%d\n", inst.itype.rs, inst.itype.rt, (int16_t) inst.itype.imm*4);
break;
case 0x5:
printf("bne\t$%d,$%d,%d\n", inst.itype.rs, inst.itype.rt, (int16_t) inst.itype.imm*4);
break;
case 0x9:
printf("addiu\t$%d,$%d,%d\n", inst.itype.rt, inst.itype.rs, (int16_t) inst.itype.imm);
break;
case 0xa:
printf("slti\t$%d.$%d,%d\n", inst.itype.rs, inst.itype.rt, (int16_t) inst.itype.imm);
break;
case 0xb:
printf("sltiu\t$%d,$%d,%d\n", inst.itype.rs, inst.itype.rt, inst.itype.imm);
break;
case 0xc:
printf("andi\t$%d,$%d,0x%x\n", inst.itype.rs, inst.itype.rt, inst.itype.imm);
break;
case 0xe:
printf("xori\t$%d,$%d,0x%x\n", inst.itype.rt, inst.itype.rs, inst.itype.imm);
break;
case 0xf:
printf("lui\t$%d,0x%x\n", inst.itype.rt, inst.itype.imm);
break;
case 0x20:
printf("lb\t$%d,%d($%d)\n", inst.itype.rt, (int16_t) inst.itype.imm, inst.itype.rs);
break;
case 0x23:
printf("lw\t$%d,%d($%d)\n", inst.itype.rt, inst.itype.imm, inst.itype.rs);
break;
case 0x24:
printf("lbu\t$%d,%u($%d)\n", inst.itype.rt, (int16_t) inst.itype.imm, inst.itype.rs);
break;
case 0x28:
printf("sb\t$%d,%d($%d)\n", inst.itype.rt, (int16_t) inst.itype.imm, inst.itype.rs);
break;
case 0x2b:
printf("sw\t$%d,%d($%d)\n", inst.itype.rt, (int16_t) inst.itype.imm, inst.itype.rs);;
break;
case 0x3:
printf("jal\t0x%x\n", inst.jtype.addr*4);
break;
case 0xd: // opcode == 0xd (ORI)
printf("ori\t$%d,$%d,0x%x\n", inst.itype.rt, inst.itype.rs, inst.itype.imm);
break;
case 0x2: // opcode == 0x2 (J)
printf("j\t0x%x\n", inst.jtype.addr*4);
break;
default: // undefined opcode
fprintf(stderr, "%s: illegal instruction: %08x\n", __FUNCTION__, inst.bits);
exit(-1);
break;
}
}