summaryrefslogtreecommitdiffhomepage
path: root/asm/intel64/codes.cpp
blob: 66a08ddb8ab9453e8617430bc252ed7bd5cd9176 (plain)
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
#include "codes.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", 7},
  {"cx", 1}, {"si", 5},
  {"dx", 2}, {"di", 6},

  {"eax", 0}, {"esp", 4},
  {"ebx", 3}, {"ebp", 7},
  {"ecx", 1}, {"esi", 5},
  {"edx", 2}, {"edi", 6},
 };

}

// Manual, page 530
// Reg + Reg/Memory
uint8_t ModRM(const std::string& reg, const std::string& rm) {
 // TODO: extend
 uint8_t result{0b11000000};

 auto index1{ IndexOfRegister.find(reg) };
 if (index1 == IndexOfRegister.end())
  throw std::runtime_error("Unknown register for arg1: "s + reg);

 result |= (index1->second << 3);

 auto index2{ IndexOfRegister.find(rm) };
 if (index2 == IndexOfRegister.end())
  throw std::runtime_error("Unknown register for arg2: "s + rm);

 result |= index2->second;

 return result;
}

#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