#include "codes.h" #include #include using namespace std::string_literals; // REX prefix: 0b0100WRXB std::vector 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 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}, }; } // Manual, page 530 // Reg + Reg/Memory uint8_t ModRM(const std::string& reg, const std::string& rm) { uint8_t result{0b11000000}; 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 auto index1{ IndexOfRegister.find(reg) }; if (index1 == IndexOfRegister.end()) throw std::runtime_error("ModRM: Unknown register for arg1: "s + reg); val_reg = index1->second; } result |= (val_reg << 3); // rm auto index2{ IndexOfRegister.find(rm) }; if (index2 == IndexOfRegister.end()) throw std::runtime_error("Unknown register for arg2: "s + rm); result |= index2->second; return result; } 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; } #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