From 1ac8ab06e9aad3b6d22685255459d71cb49e1f28 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 9 Nov 2020 09:50:58 +0100 Subject: First program: Can add 2 integers and return result via exit code --- asm/intel64/add.cpp | 6 +++++- asm/intel64/codes.cpp | 46 +++++++++++++++++++++++++++++++++------------- asm/intel64/codes.h | 3 +++ asm/intel64/mov.cpp | 5 +++++ 4 files changed, 46 insertions(+), 14 deletions(-) (limited to 'asm/intel64') diff --git a/asm/intel64/add.cpp b/asm/intel64/add.cpp index 106ffec..4438895 100644 --- a/asm/intel64/add.cpp +++ b/asm/intel64/add.cpp @@ -12,8 +12,12 @@ Op_add::Op_add(Asm::Args& args) if (args[0].type() == typeid(Asm::Args::Register32) && std::any_cast(args[0]).name() == "eax" && args[1].type() == typeid(Asm::Args::Immediate32)) - { // add eax, imm32 + { // add eax, imm32 (before "add reg32, imm32"! It's shorter.) machine_code = std::vector{ 0x05 } + std::any_cast(args[1]).getCode(); + } else if (args[0].type() == typeid(Asm::Args::Register32) && + args[1].type() == typeid(Asm::Args::Immediate32)) + { // add reg32, imm32 + machine_code = std::vector{ 0x81 } + ModRM("/0", std::any_cast(args[0]).name()) + std::any_cast(args[1]).getCode(); } else if (args[0].type() == typeid(Asm::Args::Register64) && std::any_cast(args[0]).name() == "rax" && args[1].type() == typeid(Asm::Args::Immediate32)) diff --git a/asm/intel64/codes.cpp b/asm/intel64/codes.cpp index 66a08dd..5d93a57 100644 --- a/asm/intel64/codes.cpp +++ b/asm/intel64/codes.cpp @@ -29,14 +29,14 @@ namespace { {"dl", 2}, {"dh", 6}, {"ax", 0}, {"sp", 4}, - {"bx", 3}, {"bp", 7}, - {"cx", 1}, {"si", 5}, - {"dx", 2}, {"di", 6}, + {"bx", 3}, {"bp", 5}, + {"cx", 1}, {"si", 6}, + {"dx", 2}, {"di", 7}, {"eax", 0}, {"esp", 4}, - {"ebx", 3}, {"ebp", 7}, - {"ecx", 1}, {"esi", 5}, - {"edx", 2}, {"edi", 6}, + {"ebx", 3}, {"ebp", 5}, + {"ecx", 1}, {"esi", 6}, + {"edx", 2}, {"edi", 7}, }; } @@ -44,15 +44,26 @@ namespace { // 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); - + 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); @@ -62,6 +73,15 @@ uint8_t ModRM(const std::string& reg, const std::string& rm) { 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, diff --git a/asm/intel64/codes.h b/asm/intel64/codes.h index 0ff17f1..112eef4 100644 --- a/asm/intel64/codes.h +++ b/asm/intel64/codes.h @@ -10,3 +10,6 @@ std::vector REX(const std::string& s); // Manual, page 530 // Reg + Reg/Memory uint8_t ModRM(const std::string& reg, const std::string& rm); + +// Just the number of reg, e.g. for encoding inside primary opcode +uint8_t RegNo(const std::string& reg); diff --git a/asm/intel64/mov.cpp b/asm/intel64/mov.cpp index 40a48f8..8603fc9 100644 --- a/asm/intel64/mov.cpp +++ b/asm/intel64/mov.cpp @@ -15,6 +15,8 @@ Op_mov::Op_mov(Asm::Args& args) // r/m8, r8: ModRM:r/m (w), ModRM:reg (r) machine_code = std::vector{ 0x88 } + ModRM(std::any_cast(args[1]).name(), std::any_cast(args[0]).name()); + } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Immediate32)) { // mov reg32, imm32 + machine_code = std::vector{ static_cast(0xB8 + RegNo(std::any_cast(args[0]).name())) } + std::any_cast(args[1]).getCode(); } else { throw std::runtime_error("Unimplemented: mov "s + args[0].type().name() + " "s + args[1].type().name()); } @@ -25,6 +27,9 @@ namespace { bool registered { registerOp(mangleName("mov"), [](Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); + }) && + registerOp(mangleName("mov"), [](Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); }) }; -- cgit v1.2.3