-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdas.c
247 lines (229 loc) · 5.09 KB
/
das.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* WARNING: assumes machine is little endian */
/*
* das: disassemble pdp-11 a.out file
* it could do more, but it's all I needed
*/
//#include <u.h>
//#include <libc.h>
#include <stdio.h>
#include <string.h>
#define nil 0
#define print printf
#define fprint fprintf
#define sprint sprintf
typedef unsigned short u16int;
typedef unsigned char u8int;
typedef u16int word;
typedef u8int byte;
#define W(x) ((x) & 0177777)
struct aout
{
word magic;
word textsz;
word datasz;
word bsssz;
word symsz;
word entry; /* 0 */
word stacksz; /* 0 */
word flag;
};
struct aout aout;
struct sym
{
char name[8];
word type;
word val;
};
struct sym *symtab;
int nsym;
enum /* Instruction type */
{
NoOps,
Binary,
Unary,
RegBinary,
Reg,
Trap,
Br,
Sob,
Byte = 01000,
};
struct instdef
{
char *name;
word w, m;
int type;
} instdefs[] = {
{ "mov", 0010000, 0070000, Binary|Byte },
{ "cmp", 0020000, 0070000, Binary|Byte },
{ "bit", 0030000, 0070000, Binary|Byte },
{ "bic", 0040000, 0070000, Binary|Byte },
{ "bis", 0050000, 0070000, Binary|Byte },
{ "add", 0060000, 0170000, Binary },
{ "sub", 0160000, 0170000, Binary },
{ "clr", 0005000, 0077700, Unary|Byte },
{ "com", 0005100, 0077700, Unary|Byte },
{ "inc", 0005200, 0077700, Unary|Byte },
{ "dec", 0005300, 0077700, Unary|Byte },
{ "neg", 0005400, 0077700, Unary|Byte },
{ "adc", 0005500, 0077700, Unary|Byte },
{ "sbc", 0005600, 0077700, Unary|Byte },
{ "tst", 0005700, 0077700, Unary|Byte },
{ "ror", 0006000, 0077700, Unary|Byte },
{ "rol", 0006100, 0077700, Unary|Byte },
{ "asr", 0006200, 0077700, Unary|Byte },
{ "asl", 0006300, 0077700, Unary|Byte },
{ "mark", 0006400, 0177700, 0 }, // TODO
{ "mfpi", 0006500, 0177700, Unary },
{ "mfpd", 0106500, 0177700, Unary },
{ "mtpi", 0006600, 0177700, Unary },
{ "mtpd", 0106600, 0177700, Unary },
{ "sxt", 0006700, 0177700, Unary },
{ "mul", 0070000, 0177000, RegBinary },
{ "div", 0071000, 0177000, RegBinary },
{ "ash", 0072000, 0177000, RegBinary },
{ "ashc", 0073000, 0177000, RegBinary },
{ "xor", 0074000, 0177000, RegBinary },
{ "sob", 0077000, 0177000, Sob },
{ "br", 0000400, 0177400, Br },
{ "bne", 0001000, 0177400, Br },
{ "beq", 0001400, 0177400, Br },
{ "bge", 0002000, 0177400, Br },
{ "blt", 0002400, 0177400, Br },
{ "bgt", 0003000, 0177400, Br },
{ "ble", 0003400, 0177400, Br },
{ "bpl", 0100000, 0177400, Br },
{ "bmi", 0100400, 0177400, Br },
{ "bhi", 0101000, 0177400, Br },
{ "blos", 0101400, 0177400, Br },
{ "bvc", 0102000, 0177400, Br },
{ "bvs", 0102400, 0177400, Br },
{ "bcc", 0103000, 0177400, Br },
{ "bcs", 0103400, 0177400, Br },
{ "jsr", 0004000, 0177000, RegBinary },
{ "emt", 0104000, 0177400, Trap },
{ "trap", 0104400, 0177400, Trap },
{ "jmp", 0000100, 0177700, Unary },
{ "rts", 0000200, 0177770, Reg },
{ "spl", 0000230, 0177770, 0 }, // TODO
{ "nop", 0000240, 0177777, 0 }, // TODO
{ "ccc", 0000240, 0177760, 0 }, // TODO
{ "scc", 0000260, 0177760, 0 }, // TODO
{ "swab", 0000300, 0177700, Unary },
{ "halt", 0000000, 0177777, 0 },
{ "wait", 0000001, 0177777, 0 },
{ "rti", 0000002, 0177777, 0 },
{ "bpt", 0000003, 0177777, 0 },
{ "iot", 0000004, 0177777, 0 },
{ "reset", 0000005, 0177777, 0 },
{ "rtt", 0000006, 0177777, 0 },
{ nil, 0, 0, 0 }
};
struct instdef*
getinst(word inst)
{
struct instdef *i;
for(i = instdefs; i->name; i++)
if((inst & i->m) == i->w)
return i;
return nil;
}
char*
disop(char *s, int m, word addr)
{
extern word fetch(void);
static char *rstr[8] = {
"r0", "r1", "r2", "r3",
"r4", "r5", "sp", "pc"
};
int r;
word w;
r = m & 7;
m >>= 3;
if(m != 1 && m & 1)
*s++ = '*';
switch(m & 7){
case 0:
s += sprint(s, "%s", rstr[r]);
break;
case 1:
s += sprint(s, "(%s)", rstr[r]);
break;
case 2:
case 3:
if(r == 7){
w = fetch();
s += sprint(s, "$%o", w);
}else
s += sprint(s, "(%s)+", rstr[r]);
break;
case 4:
case 5:
// if(r == 7)
// addr -= 2;
s += sprint(s, "-(%s)", rstr[r]);
break;
case 6:
case 7:
w = fetch();
if(r == 7)
s += sprint(s, "%o", W(w + addr));
else if(w & 0100000)
s += sprint(s, "-%o(%s)", W(~w+1), rstr[r]);
else
s += sprint(s, "%o(%s)", w, rstr[r]);
break;
}
return s;
}
char*
dis11(word addr, word inst)
{
static char line[128];
char *s;
struct instdef *def;
memset(line, 0, sizeof(line));
def = getinst(inst);
if(def == nil)
return line;
s = line;
strcpy(s, def->name);
s += strlen(def->name);
if(inst & 0100000 && def->type & Byte)
*s++ = 'b';
switch(def->type & ~Byte){
case Binary:
*s++ = '\t';
s = disop(s, inst>>6 & 077, addr);
*s++ = ',';
s = disop(s, inst & 077, addr);
break;
case Unary:
*s++ = '\t';
s = disop(s, inst & 077, addr);
break;
case RegBinary:
*s++ = '\t';
s = disop(s, inst>>6 & 07, addr);
*s++ = ',';
s = disop(s, inst & 077, addr);
break;
case Reg:
*s++ = '\t';
s = disop(s, inst & 07, addr);
break;
case Br:
if(inst & 0200)
s += sprint(s, "\t%o", W(addr+2 + ((inst|0177400)<<1)));
else
s += sprint(s, "\t%o", W(addr+2 + ((inst&0377)<<1)));
break;
case Trap:
s += sprint(s, "\t%o", inst&0377);
break;
case Sob:
;// TODO
}
*s = '\0';
return line;
}