-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.cpp
71 lines (54 loc) · 1.58 KB
/
decode.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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define OPCODE_MOV 0x88
string reg_table[] = {"al", "ax", "cl", "cx", "dl", "dx", "bl", "bx", "ah", "sp", "ch", "bp", "dh", "si", "bh", "di"};
string decodeOpMov(uint16_t op) {
uint8_t D, W, MOD, REG, RM;
D = (op >> 8) & 0x02;
W = (op >> 8) & 0x01;
MOD = (op >> 6) & 0x03 ;
REG = (op >> 3) & 0x07;
RM = op & 0x07;
return "mov " + (D == 0 ?
reg_table[2*RM + W] + " " + reg_table[2*REG +W] :
reg_table[2*REG +W] + " " + reg_table[2*RM + W]);
}
void decode(vector<char> instructions) {
uint8_t opcode;
uint16_t op;
for(int offset = 0; offset < instructions.size();) {
op = ((instructions[offset] & 0xff) << 8) | instructions[offset+1] & 0xff;
opcode = instructions[offset] & 0xfc;
switch (opcode) {
case (uint8_t)OPCODE_MOV:
cout << decodeOpMov(op) << endl;
offset += 2;
break;
default:
cout << "Operation with opcode " << (uint8_t)opcode << "not supported";
return;
}
}
}
int main(int argc, char **argv){
if (argc != 2) {
cout << "Wrong number of arguments, expected 2, got " << argc << endl;
}
// open the binary
ifstream BinaryFile(argv[1], ios::in | ios::binary);
if (!BinaryFile) {
cerr << "Failed to open binary file" << endl;
return -1;
}
// get file size
BinaryFile.seekg(0, ios::end);
streampos fileSize = BinaryFile.tellg();
BinaryFile.seekg(0, ios::beg);
// buffer
vector<char> buffer(fileSize);
BinaryFile.read(buffer.data(), fileSize);
decode(buffer);
BinaryFile.close();
}