Repository navigation
Expand file tree
/
Copy pathdisasm.c
More file actions
106 lines (96 loc) · 3.93 KB
/
Copy pathdisasm.c
File metadata and controls
106 lines (96 loc) · 3.93 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
#include <stdint.h>
#include <stdio.h>
#include "disasm_tables.h"
// Sign-extend a value with 'bits' significant bits to 32 bits
static inline int32_t sign_extend(int32_t val, int bits) {
int shift = 32 - bits;
return (val << shift) >> shift;
}
// Disassemble a single 32-bit RISC-V instruction
void disassemble(uint32_t instr) {
uint8_t opcode = instr & 0x7F;
uint8_t rd = (instr >> 7) & 0x1F;
uint8_t funct3 = (instr >> 12) & 0x07;
uint8_t rs1 = (instr >> 15) & 0x1F;
uint8_t rs2 = (instr >> 20) & 0x1F;
uint8_t funct7 = (instr >> 25) & 0x7F;
uint32_t imm_i = (instr >> 20) & 0xFFF;
switch (opcode) {
case 0x33: // R-type
for (int i = 0; i < rtype_table_size; i++) {
if (funct3 == rtype_table[i].funct3 && funct7 == rtype_table[i].funct7) {
printf("%s %s, %s, %s\n", rtype_table[i].mnemonic, reg_names[rd], reg_names[rs1], reg_names[rs2]);
return;
}
}
printf("Unknown R-type instruction (funct3: 0x%x, funct7: 0x%x)\n", funct3, funct7);
break;
case 0x13: // I-type
for (int i = 0; i < itype_table_size; i++) {
if (funct3 == itype_table[i].funct3) {
printf("%s %s, %s, %d\n", itype_table[i].mnemonic, reg_names[rd], reg_names[rs1], sign_extend(imm_i, 12));
return;
}
}
printf("Unknown I-type instruction (funct3: 0x%x)\n", funct3);
break;
case 0x23: { // S-type
for (int i = 0; i < stype_table_size; i++) {
if (funct3 == stype_table[i].funct3) {
uint32_t imm_s = ((instr >> 7) & 0x1F) | ((instr >> 25) & 0x7F) << 5;
printf("%s %s, %s, %d\n", stype_table[i].mnemonic, reg_names[rs2], reg_names[rs1], sign_extend(imm_s, 12));
return;
}
}
printf("Unknown S-type instruction (funct3: 0x%x)\n", funct3);
break;
}
case 0x63: { // B-type
for (int i = 0; i < btype_table_size; i++) {
if (funct3 == btype_table[i].funct3) {
int32_t imm_b =
((instr >> 7) & 0x1E) |
((instr >> 25) & 0x3F) << 5 |
((instr >> 7) & 0x01) << 11 |
((instr >> 31) & 0x01) << 12;
printf("%s %s, %s, %d\n", btype_table[i].mnemonic, reg_names[rs1], reg_names[rs2], sign_extend(imm_b, 13));
return;
}
}
printf("Unknown B-type instruction (funct3: 0x%x)\n", funct3);
break;
}
case 0x37: // LUI
case 0x17: { // AUIPC
for (int i = 0; i < utype_table_size; i++) {
if (opcode == utype_table[i].opcode) {
uint32_t imm_u = instr & 0xFFFFF000;
printf("%s %s, 0x%x\n", utype_table[i].mnemonic, reg_names[rd], imm_u);
return;
}
}
printf("Unknown U-type instruction (opcode: 0x%x)\n", opcode);
break;
}
case 0x6F: { // J-type (JAL)
int32_t imm_j =
((instr >> 21) & 0x3FF) << 1 |
((instr >> 20) & 0x1) << 11 |
((instr >> 12) & 0xFF) << 12 |
((instr >> 31) & 0x1) << 20;
printf("jal %s, %d\n", reg_names[rd], sign_extend(imm_j, 21));
break;
}
case 0x67: { // JALR (I-type)
if (funct3 == 0x0) {
printf("jalr %s, %s, %d\n", reg_names[rd], reg_names[rs1], sign_extend(imm_i, 12));
return;
}
printf("Unknown JALR instruction (funct3: 0x%x)\n", funct3);
break;
}
default:
printf("Unknown opcode: 0x%x\n", opcode);
break;
}
}