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
|
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mnemonics.h"
const char *progname;
#define ERROR(...) \
do { \
fprintf(stderr, "%s: ", progname); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
#define PERROR(str) \
do { \
int err = errno; \
fprintf(stderr, "%s: ", progname); \
errno = err; \
perror(str); \
} while (0)
static struct line {
int label;
uint16_t value;
uint8_t mnem;
} lines[0x7fff];
static size_t maxpc;
#define LINE(addr) &lines[(uint16_t)(addr) >> 1]
#define MNEM(line) ((line)->mnem < MNEM_LAST ? &mnemonics[(line)->mnem] : NULL)
static const uint8_t match_instruction(unsigned bits)
{
for (int i = 0; i < MNEM_LAST; i++) {
const struct mnemonic *m = mnemonics + i;
if ((bits & m->mask) == m->bits) return i;
}
return MNEM_LAST;
}
static inline int get_argvalue(const struct line *line, int argidx)
{
const struct operand *op = MNEM(line)->operands + argidx;
switch (op->type) {
case REG:
case REGPTR: return 7 & (line->value >> op->shift);
case IMM: return (uint8_t) (line->value >> op->shift);
case PCOFF: return (int8_t) (line->value >> op->shift);
}
assert(0);
}
static struct line *get_pcoff_target(uint16_t pc, int argidx)
{
struct line *l = LINE(pc + get_argvalue(LINE(pc), argidx));
return l - lines >= 2*maxpc ? NULL : l;
}
static void assign_labels(void)
{
/* mark those addresses that need a label */
for (size_t pc = 0; pc < maxpc; pc += 2) {
const struct line *l = LINE(pc);
const struct mnemonic *m = MNEM(l);
if (!m) continue;
const struct operand *ops = m->operands;
for (int j = 0; j < 3 && ops[j].type; j++) {
if (ops[j].type == PCOFF) {
struct line *target = get_pcoff_target(pc, j);
if (target) target->label = 1;
}
}
}
/* assign labels in ascending order */
int label = 0;
for (size_t pc = 0; pc < maxpc; pc += 2) {
struct line *l = LINE(pc);
l->label = l->label ? label++ : -1;
}
}
static void print_operand(FILE *out, size_t pc, size_t argidx)
{
const struct line *inst = LINE(pc);
const struct operand *op = MNEM(inst)->operands + argidx;
switch (op->type) {
case REG: fprintf(out, "r%d", get_argvalue(inst, argidx)); break;
case REGPTR: fprintf(out, "@r%d", get_argvalue(inst, argidx)); break;
case IMM: fprintf(out, "%d", get_argvalue(inst, argidx)); break;
case PCOFF: {
const struct line *target = get_pcoff_target(pc, argidx);
if (target && target->label >= 0)
fprintf(out, "L%d", target->label);
else
fprintf(out, "%d", get_argvalue(inst, argidx));
break;
}
default: assert(0);
}
}
static int read_lines(FILE *in)
{
unsigned bits;
for (maxpc = 0; fscanf(in, " %x\n", &bits) > 0; maxpc += 2) {
if (maxpc > 0xffff) {
ERROR("Input exceeds 16-bit address space");
return 1;
}
*LINE(maxpc) = (struct line) {
.mnem = match_instruction(bits),
.value = bits,
};
}
return 0;
}
static void disassemble(FILE *in, FILE *out)
{
if (read_lines(in)) return;
assign_labels();
for (size_t pc = 0; pc < maxpc; pc += 2) {
const struct line *line = LINE(pc);
unsigned bits = line->value;
const struct mnemonic *m = MNEM(line);
if (line->label >= 0) fprintf(out, "L%d:", line->label);
if (!m) {
fprintf(out, "\t.word 0x%04x\n", bits);
continue;
}
fprintf(out, "\t%s", m->mnem);
for (int j = 0; j < 3 && m->operands[j].type; j++) {
if (j) fprintf(out, ", ");
else fprintf(out, "%*s", 8 - (int)strlen(m->mnem), "");
print_operand(out, pc, j);
}
fprintf(out, "\n");
}
}
static void usage(const char *progname)
{
printf("usage: %s [-o output] [input]\n", progname);
printf(" %s -h\n", progname);
printf("\n");
printf("\t%-16s %s\n", "-h", "display this help text and exit");
printf("\t%-16s %s\n", "-o output", "write output to 'output' (default=stdout)");
printf("\t%-16s %s\n", "input", "read input from 'input' (default=stdin)");
}
int main(int argc, char **argv)
{
const char *outname = NULL;
int error = 0;
extern int optind;
extern char *optarg;
progname = argv[0];
for (int opt; (opt = getopt(argc, argv, "+ho:")) != -1;) {
switch (opt) {
case 'h':
usage(argv[0]);
return 0;
case 'o':
outname = optarg;
break;
default:
error = 1;
}
}
if (optind < argc - 1) {
ERROR("Too many input files");
return 1;
}
if (error) return 1;
FILE *in = stdin;
FILE *out = stdout;
if (optind < argc && !(in = fopen(argv[optind], "r"))) {
PERROR("Error opening input file");
return 1;
}
if (outname && !(out = fopen(outname, "w"))) {
PERROR("Error opening output file");
fclose(in);
return 1;
}
disassemble(in, out);
fclose(in);
fclose(out);
return 0;
}
|