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
|
#include "codes.h"
#include "byteorder.h"
#include "minicc.h"
#include "../operators.h"
#include <exception>
#include <unordered_map>
using namespace std::string_literals;
// REX prefix: 0b0100WRXB
std::vector<uint8_t> REX(const std::string& s) {
uint8_t result{0b01000000};
if (s == "W")
result |= 0b00001000;
if (s == "R")
result |= 0b00000100;
if (s == "X")
result |= 0b00000010;
if (s == "B")
result |= 0b00000001;
return { result };
}
namespace {
std::unordered_map<std::string, size_t> IndexOfRegister{
{"al", 0}, {"ah", 4},
{"bl", 3}, {"bh", 7},
{"cl", 1}, {"ch", 5},
{"dl", 2}, {"dh", 6},
{"ax", 0}, {"sp", 4},
{"bx", 3}, {"bp", 5},
{"cx", 1}, {"si", 6},
{"dx", 2}, {"di", 7},
{"eax", 0}, {"esp", 4},
{"ebx", 3}, {"ebp", 5},
{"ecx", 1}, {"esi", 6},
{"edx", 2}, {"edi", 7},
{"rax", 0}, {"rsp", 4},
{"rbx", 3}, {"rbp", 5},
{"rcx", 1}, {"rsi", 6},
{"rdx", 2}, {"rdi", 7},
};
} // namespace
// Manual, page 530
// Reg + Reg/Memory
std::vector<uint8_t> ModRM(const std::string& reg, const std::string& rm, int32_t disp) {
uint8_t result{}; // MOD is highest 2 bits, then 3 bits Reg, the 3 bits R/M
std::vector<uint8_t> displacement_bytes;
size_t val_reg{};
// reg
if (reg.size() > 0 && reg[0] == '/') { // "/digit"
try {
val_reg = stoull(reg.substr(1));
} catch (const std::exception& ex) {
throw std::runtime_error("ModRM: Bad digit in arg1: "s + reg);
}
} else { // reg
val_reg = RegNo(reg);
}
result |= (val_reg << 3);
// rm
if (rm.size() > 2 && rm.front() == '[' && rm.back() == ']') { // indexed / MemPtr
uint8_t rm_bits {RegNo(rm.substr(1, rm.size() - 2))};
if (rm_bits == 4)
throw std::runtime_error("ICE: SIB byte not yet supported");
if (disp == 0 && rm_bits != 5) { // no displacement
// ignore: keep MOD == 00, no displacement bytes
if (rm_bits == 5)
throw std::runtime_error("ICE: [rbp] with no displacement is not supported"); // TODO: Support this, and SIB byte
} else if (disp >= -128 && disp < 128) {
result |= 0b01000000; // 8 bit displacement
displacement_bytes.push_back(uint8_t(disp));
} else {
result |= 0b10000000; // 32 bit displacement
displacement_bytes += endian::to_little(uint32_t(disp));
}
result |= rm_bits;
} else { // normal register access
result |= 0b11000000;
result |= RegNo(rm);
}
return std::vector<uint8_t>{result} + displacement_bytes;
}
uint8_t RegNo(const std::string& reg)
{
auto index{ IndexOfRegister.find(reg) };
if (index == IndexOfRegister.end())
throw std::runtime_error("Reg: Unknown register for arg: "s + reg);
return index->second;
}
// Switch from operand size 64bit to 32bit
std::vector<uint8_t> OpSizePrefix()
{
return {0x66};
}
#if 0
prefixes{
"lock", 0xf0,
// branch hint
0x2e, "branch not taken"
0x3e, "branch taken"
0x66, "operand size override" // switch between 16 and 32 bit operands
0x67, "address size override" // switch between 16 and 32 bit addresses
};
};
#endif
|