-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disassembler.cpp
131 lines (109 loc) · 3.93 KB
/
Disassembler.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
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
#include "Disassembler.hpp"
#include "File.hpp"
#include <cassert>
#include <cstdlib>
#include <cstring>
Disassembler::Disassembler(const char *inputFileName)
{
assert(inputFileName != nullptr);
FILE *inputFile = std::fopen(inputFileName, "rb");
assembly = readBinaryFileToBuf(inputFile);
assemblySize = *(size_t *)(assembly + sizeof(short) + sizeof(char));
assembly += sizeof(short) + sizeof(char) + sizeof(size_t);
rip = assembly;
auto outputFileName = new char[strlen(inputFileName) + std::strlen("_disassembly") +
std::strlen("asm") - std::strlen("meow") + 1];
std::strcpy(outputFileName, inputFileName);
std::strcpy(strchr(outputFileName, '.'), "_disassembly.asm");
disassemblyFile = std::fopen(outputFileName, "w");
delete[] outputFileName;
}
void Disassembler::operator()()
{
while (rip - assembly < assemblySize) {
switch (*rip) {
#define DEFINE_COMMAND(name, code, noArg, processorSrc) \
case code: { \
rip += sizeof(char); \
\
disassembleInstruction(#name, noArg); \
\
break; \
}
#include "Commands.hpp"
#undef DEFINE_COMMAND
}
}
}
Disassembler::~Disassembler()
{
std::fclose(disassemblyFile);
delete[] (assembly - sizeof(short) - sizeof(char) - sizeof(size_t));
}
void Disassembler::disassembleInstruction(const char *name, bool noArg)
{
assert(name != nullptr);
std::fprintf(disassemblyFile, "%s ", name);
if (noArg) {
disassembleNoArgInstruction();
return;
}
if (isJumpInstruction(name)) {
disassembleJumpInstructionArg();
return;
}
disassembleReadWriteInstructionArgs();
}
void Disassembler::disassembleJumpInstructionArg()
{
auto address = *(ptrdiff_t *) rip;
rip += sizeof(ptrdiff_t);
std::fprintf(disassemblyFile, "%lld\n", address);
}
bool Disassembler::isJumpInstruction(const char *cmdName)
{
assert(cmdName != nullptr);
return (std::strcmp(cmdName, "jmp") == 0) || (std::strcmp(cmdName, "ja") == 0) || (std::strcmp(cmdName, "jae") == 0) ||
(std::strcmp(cmdName, "jb") == 0) || (std::strcmp(cmdName, "jbe") == 0) || (std::strcmp(cmdName, "je") == 0) ||
(std::strcmp(cmdName, "jne") == 0) || (std::strcmp(cmdName, "call") == 0);
}
char Disassembler::regCodeToChar(char regCode)
{
return (regCode >= 0 && regCode <= 3) ? (regCode + 'a') : (regCode - 4 + '0');
}
void Disassembler::disassembleNoArgInstruction()
{
std::fprintf(disassemblyFile, "\n");
}
void Disassembler::disassembleReadWriteInstructionArgs()
{
ReadWriteMode readWriteMode = {};
readWriteMode.mode = *rip;
rip += sizeof(char);
if (readWriteMode.ram) {
std::fprintf(disassemblyFile, "[");
disassembleReadWriteInstructionValueArg(readWriteMode);
std::fprintf(disassemblyFile, "]\n");
return;
}
disassembleReadWriteInstructionValueArg(readWriteMode);
std::fprintf(disassemblyFile, "\n");
}
void Disassembler::disassembleReadWriteInstructionValueArg(ReadWriteMode readWriteMode)
{
if (readWriteMode.reg & readWriteMode.constant) {
auto regCode = *rip;
rip += sizeof(char);
auto offset = *(constant_t *) rip;
rip += sizeof(constant_t);
std::fprintf(disassemblyFile, "r%cx+%lg", regCodeToChar(regCode), offset);
} else if (readWriteMode.reg & !readWriteMode.constant) {
auto regCode = *rip;
rip += sizeof(char);
std::fprintf(disassemblyFile, "r%cx", regCodeToChar(regCode));
} else if (readWriteMode.constant & !readWriteMode.reg) {
auto offset = *(constant_t *) rip;
rip += sizeof(constant_t);
std::fprintf(disassemblyFile, "%lg", offset);
}
}