-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisAsm.cpp
64 lines (55 loc) · 1.62 KB
/
DisAsm.cpp
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
#include <cstdio>
#include <cassert>
#include "linker.h"
#include "My_Headers/txt_files.h"
char* translate_code(int code);
int main() {
size_t bin_size = 0;
char* bin = read_file_to_buffer_alloc(BIN_FILE_NAME, "rb", &bin_size);
assert(bin);
char* pc = bin;
FILE* fout = fopen(DISASM_OUTPUT_FILENAME, "wb");
assert(fout);
int signature = *(int*)pc;
if(SIGNATURE != signature) {
fprintf(fout, "Signature mismatch. Choose correct bin file");
return 0;
}
assert(SIGNATURE == signature);
pc += sizeof(SIGNATURE);
int version = *pc++;
fprintf(fout, ";Signature: %X\n;Version: %d\n", signature, version);
while (pc - bin != bin_size) {
switch (*pc) {
#define DEF_CMD(name, token, scanf_sample, n_args, instructions, disasm) \
case CMD_##name:\
fprintf(fout, "%#6X: ", pc - bin);\
fprintf(fout, #token disasm);\
fprintf(fout, "\n");\
pc += 1 + sizeof(int) * n_args;\
break;
#include "commands.h"
#undef DEF_CMD
default:
fprintf(fout, "ERROR. Unknown command. Translation stopped.");
fprintf(stderr, "ERROR. Unknown command. Translation stopped.");
abort();
}
}
fclose(fout);
free(bin);
}
char* translate_code(int code) {
switch (code) {
case AX:
return (char*)"ax";
case BX:
return (char*)"bx";
case CX:
return (char*)"cx";
case DX:
return (char*)"dx";
default:
return (char*)"UNKNOWN REGISTER";
}
}