-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdisassembler.c
84 lines (72 loc) · 1.73 KB
/
disassembler.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
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "rabbit_bif.h"
#include "rabbit_codewords.h"
#include "rabbit_io.h"
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Need to pass file to execute.\n");
return 1;
}
char *fn = argv[1];
FILE *fp = fopen(fn, "r");
if (fp == NULL) {
fprintf(stderr, "Can't open `%s'.\n", fn);
return 1;
}
while (1) {
uint32_t word = 0;
int nread = read_word(fp, &word);
if (nread == 0) {
fclose(fp);
return 0;
}
printf("%.8x:\t", word);
struct unpacked_s i = decode(word);
assert(i.opcode < kNumInstrs);
const char *name = instr_nargs[i.opcode].name;
unsigned nargs = instr_nargs[i.opcode].nargs;
uint32_t immediate = 0;
printf("%s", name);
if (i.opcode == Bif) {
assert(i.rega < kNumBifs);
printf(" %s\n", bif_names[i.rega]);
continue;
}
if (nargs == 1 || nargs == 2 || nargs == 3) {
putchar(' ');
if (nargs == 2 || nargs == 3) {
if (nargs == 3) {
if (i.modes.rega_deref == 1) {
printf("(r%d), ", i.rega);
} else {
printf("r%d, ", i.rega);
}
}
if (i.modes.regb_deref == 1) {
printf("(r%d), ", i.regb);
} else {
printf("r%d, ", i.regb);
}
}
if (i.modes.regc_deref == 1) {
putchar('(');
}
if (i.modes.immediate == 1) {
nread = read_word(fp, &immediate);
if (nread == 0) {
return 1;
}
printf("$%d", immediate);
} else {
printf("r%d", i.regc);
}
if (i.modes.regc_deref) {
putchar(')');
}
}
putchar('\n');
}
}