-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
435 lines (377 loc) · 9.96 KB
/
main.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
enum template { A, B, C, D, A2, B2, C2, E2, A3, B3, E3, VACANT, JUMP_TODO };
/* access IL bits */
uint8_t read_len(uint8_t *instruction)
{
uint8_t instr_mask = 0b11000000; // xc0
return (instr_mask & instruction[3]) >> 6;
}
/* access mode bits */
uint8_t read_mode1(uint8_t *instruction)
{
uint8_t mode1_mask = 0b00111000; // x38
return (mode1_mask & instruction[3]) >> 3;
}
/* access OP1 (?) bits */
uint8_t read_opcode_6(uint8_t *instruction)
{
uint8_t mask_1 = 0b00000111;
uint8_t mask_2 = 0b11100000;
return ((instruction[3] & mask_1) << 3 | (instruction[2] & mask_2) >> 5);
}
/* access M bit */
uint8_t read_m_bit(uint8_t *instruction)
{
uint8_t mask = 0b10000000;
return (instruction[1] & mask) >> 7;
}
/* parse and output A template instruction */
void print_a(uint8_t *instruction)
{
uint8_t mask;
int output;
printf("instruction length: %d\n", read_len(instruction));
printf("mode: %d\n", read_mode1(instruction));
printf("opcode: %d\n", read_opcode_6(instruction));
mask = 0b00011111;
output = (instruction[2] & mask);
printf("dest register: %d\n", output);
mask = 0b10000000;
output = instruction[1] & mask >> 7;
printf("M: %d\n", output);
mask = 0b01100000;
output = instruction[1] & mask >> 5;
printf("operand type: %d\n", output);
mask = 0b00011111;
output = (instruction[1] & mask);
printf("src register S: %d\n", output);
mask = 0b11100000;
output = instruction[0] & mask >> 5;
printf("mask: %d\n", output);
mask = 0b00011111;
output = instruction[0] & mask;
printf("source register T: %d\n", output);
}
/* parse and output B template instruction */
void print_b(uint8_t *instruction)
{
uint8_t mask;
int output;
printf("instruction length: %d\n", read_len(instruction));
printf("mode: %d\n", read_mode1(instruction));
printf("opcode: %d\n", read_opcode_6(instruction));
mask = 0b00011111;
output = (instruction[2] & mask);
printf("dest register: %d\n", output);
mask = 0b10000000;
output = instruction[1] & mask >> 7;
printf("M: %d\n", output);
mask = 0b01100000;
output = instruction[1] & mask >> 5;
printf("operand type: %d\n", output);
mask = 0b00011111;
output = (instruction[1] & mask);
printf("src register S: %d\n", output);
output = instruction[0];
printf("immediate constant: %d\n", output);
}
/* parse and output C template instruction */
void print_c(uint8_t *instruction)
{
uint8_t mask;
int output;
printf("instruction length: %d\n", read_len(instruction));
printf("mode: %d\n", read_mode1(instruction));
printf("opcode: %d\n", read_opcode_6(instruction));
mask = 0b00011111;
output = (instruction[2] & mask);
printf("dest register: %d\n", output);
output = instruction[1];
printf("immediate constant 2: %d\n", output);
output = instruction[0];
printf("immediate constant 1: %d\n", output);
}
/* parse and output D template instruction */
void print_d(uint8_t *instruction)
{
uint8_t mask;
int output;
printf("instruction length: %d\n", read_len(instruction));
printf("mode: %d\n", read_mode1(instruction));
mask = 0b00000111;
output = instruction[3] & mask;
printf("opcode: %d\n", output);
output = instruction[2];
output <<= 8;
output |= instruction[1];
output <<= 8;
output |= instruction[0];
printf("immediate constant 3: %d\n", output);
}
void print_a2(uint8_t *instruction) {
uint32_t im6;
print_a(instruction);
im6 = instruction[4];
im6 |= instruction[5] << 8;
im6 |= instruction[6] << 16;
im6 |= instruction[7] << 24;
printf("IM6: %d\n", im6);
}
void print_b2(uint8_t *instruction) {
uint32_t im6;
print_b(instruction);
im6 = instruction[4];
im6 |= instruction[5] << 8;
im6 |= instruction[6] << 16;
im6 |= instruction[7] << 24;
printf("IM6: %d\n", im6);
}
enum template get_template_0(uint8_t *instruction)
{
switch (read_mode1(instruction)) {
case 0:
return A;
case 1:
return B;
case 2:
return A;
case 3:
return B;
case 4:
return A;
case 5:
return A;
case 6:
return A;
case 7:
return B;
default:
printf("error 0\n");
exit(1);
}
}
enum template get_template_1(uint8_t *instruction)
{
switch (read_mode1(instruction)) {
case 0:
return A;
case 1:
return C;
case 2:
return A;
case 3:
return B;
case 4:
return C;
case 5:
return VACANT;
case 6:
return JUMP_TODO;
case 7:
if ((0 <= read_opcode_6(instruction)) && (read_opcode_6(instruction) < 16)) {
return D; // 1.7D
}
return C; // 1.7C
default:
printf("error 1\n");
exit(1);
}
}
enum template get_template_2(uint8_t *instruction)
{
switch (read_mode1(instruction)) {
case 0:
switch (read_m_bit(instruction)) {
case 0:
return E2;
case 1:
return A2;
}
case 1:
return A2;
case 2:
return E2;
case 3:
return A2;
case 4:
return A2;
case 5:
switch (read_opcode_6(instruction)) {
case 3:
printf("2.5.0 A2\n");
return A2; // 2.5.0
case 1: // 2.5.1
case 2: // 2.5.2
return B2;
case 4: //
case 5:
case 7:
return C2;
}
case 6:
return A2;
case 7:
return VACANT;
default:
printf("error 2\n");
exit(1);
}
}
enum template get_template_3(uint8_t *instruction)
{
switch (read_mode1(instruction)) {
case 0:
return E3;
case 1:
switch (read_opcode_6(instruction)) {
case 0:
return A3;
case 1:
return B3;
}
case 2:
return E3;
case 3:
return A3;
case 4:
return VACANT;
case 5:
return VACANT;
case 6:
return VACANT;
case 7:
return VACANT;
default:
printf("error 3\n");
exit(1);
}
}
enum template get_template(uint8_t *instruction)
{
switch (read_len(instruction)) {
case 0:
return get_template_0(instruction);
case 1:
return get_template_1(instruction);
case 2:
return get_template_2(instruction);
case 3:
return get_template_3(instruction);
default: // shouldn't ever happen
return VACANT;
}
}
void print_hex(uint8_t *instruction, size_t n)
{
for (int i=0; i<n/4; ++i) {
for (int j=3; j>=0; --j) {
printf("%x ", instruction[i*4 + j]);
}
}
printf("\n");
}
int main(int argc, char **argv) {
// validate command line input
if (argc != 2) {
printf("usage: disassembler <assembly_file>\n");
return 1;
}
//int fd = open("memset.ob", O_RDONLY);
int fd = open(argv[1], O_RDONLY);
// open file
if (fd < 0) {
printf("open failed\n");
exit(1);
}
// TODO: replace hard coded start and end points.
// consider searching for 4 bytes set to 0 and then for 4 bytes not set to 0 and
// start reading there until we see 0 x 4 bytes again?
lseek(fd, 0x68, SEEK_SET); // why 0x68?
size_t n = 4 * 17;
uint8_t buf[n];
if (read(fd, buf, n) != n) {
printf("read failed\n");
exit(1);
}
uint8_t *p = buf;
while (p < buf + n) {
printf("\n");
switch (get_template(p)) {
case A:
printf("template A\n");
print_hex(p, 4);
print_a(p);
p += 4;
break;
case B:
printf("template B\n");
print_hex(p, 4);
print_b(p);
p += 4;
break;
case C:
printf("template C\n");
print_hex(p, 4);
print_c(p);
p += 4;
break;
case D:
printf("template D\n");
print_hex(p, 4);
print_d(p);
p += 4;
break;
case A2:
printf("template A2\n");
print_hex(p, 8);
print_a2(p);
p += 8;
break;
case B2:
printf("template B2\n");
print_hex(p, 8);
print_b2(p);
p += 8;
break;
case C2:
printf("template C2\n");
print_hex(p, 8);
p += 8;
break;
case E2:
printf("template E2\n");
print_hex(p, 8);
p += 8;
break;
case A3:
printf("template A3\n");
print_hex(p, 12);
p += 12;
break;
case B3:
printf("template B3\n");
print_hex(p, 12);
p += 12;
break;
case E3:
printf("template E3\n");
print_hex(p, 12);
p += 12;
break;
case JUMP_TODO:
printf("jump template 1.6A or 1.7B\n");
print_hex(p, 4);
p += 4;
break;
default:
printf("error\n");
exit(1);
}
printf("\n");
}
return 0;
}