From a632cce380a853f5400111e19e1380982ed8a7fd Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 28 Nov 2020 12:38:49 +0100 Subject: Implemented MOVSX - Move With Sign-Extension --- asm/intel64/movsx.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 asm/intel64/movsx.cpp (limited to 'asm/intel64/movsx.cpp') diff --git a/asm/intel64/movsx.cpp b/asm/intel64/movsx.cpp new file mode 100644 index 0000000..9dd1ec8 --- /dev/null +++ b/asm/intel64/movsx.cpp @@ -0,0 +1,116 @@ +#include "movsx.h" + +#include "codes.h" + +#include +#include + +#include + +using namespace std::string_literals; + +Op_movsx::Op_movsx(const Asm::Args& args) +{ + if (args[0].type() == typeid(Asm::Args::Register16) && args[1].type() == typeid(Asm::Args::Register8)) { // movsx reg16, reg8 + machine_code = OpSizePrefix() + std::vector{ 0x0F, 0xBE } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + + } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Register8)) { // movsx reg32, reg8 + machine_code = std::vector{ 0x0F, 0xBE } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Register8)) { // movsx reg64, reg8 + machine_code = REX("W") + std::vector{ 0x0F, 0xBE } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + + } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Register16)) { // movsx reg32, reg16 + machine_code = OpSizePrefix() + std::vector{ 0x0F, 0xBF } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Register16)) { // movsx reg64, reg16 + machine_code = REX("W") + std::vector{ 0x0F, 0xBF } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Register32)) { // movsx reg64, reg32 + machine_code = REX("W") + std::vector{ 0x63 } + + ModRM(std::any_cast(args[0]).name(), std::any_cast(args[1]).name()); + + + } else if (args[0].type() == typeid(Asm::Args::Register16) && args[1].type() == typeid(Asm::Args::Mem8Ptr64)) { // movsx reg16, byte ptr [reg64] + Asm::Args::Mem8Ptr64 ptr{std::any_cast(args[1])}; + machine_code = OpSizePrefix() + std::vector{ 0x0F, 0xBE } + + ModRM(std::any_cast(args[0]).name(), ptr.reg(), ptr.offs()); + + } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Mem8Ptr64)) { // movsx reg32, byte ptr [reg64] + Asm::Args::Mem8Ptr64 ptr{std::any_cast(args[1])}; + machine_code = std::vector{ 0x0F, 0xBE } + + ModRM(std::any_cast(args[0]).name(), ptr.reg(), ptr.offs()); + + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Mem8Ptr64)) { // movsx reg64, byte ptr [reg64] + Asm::Args::Mem8Ptr64 ptr{std::any_cast(args[1])}; + machine_code = REX("W") + std::vector{ 0x0F, 0xBE } + + ModRM(std::any_cast(args[0]).name(), ptr.reg(), ptr.offs()); + + } else if (args[0].type() == typeid(Asm::Args::Register32) && args[1].type() == typeid(Asm::Args::Mem16Ptr64)) { // movsx reg32, word ptr [reg64] + Asm::Args::Mem16Ptr64 ptr{std::any_cast(args[1])}; + machine_code = OpSizePrefix() + std::vector{ 0x0F, 0xBF } + + ModRM(std::any_cast(args[0]).name(), ptr.reg(), ptr.offs()); + + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Mem16Ptr64)) { // movsx reg64, word ptr [reg64] + Asm::Args::Mem16Ptr64 ptr{std::any_cast(args[1])}; + machine_code = REX("W") + std::vector{ 0x0F, 0xBF } + + ModRM(std::any_cast(args[0]).name(), ptr.reg(), ptr.offs()); + + } else if (args[0].type() == typeid(Asm::Args::Register64) && args[1].type() == typeid(Asm::Args::Mem32Ptr64)) { // movsx reg64, dword ptr [reg64] + Asm::Args::Mem32Ptr64 ptr{std::any_cast(args[1])}; + machine_code = REX("W") + std::vector{ 0x63 } + + ModRM(std::any_cast(args[0]).name(), ptr.reg(), ptr.offs()); + + + } else { + throw std::runtime_error("Unimplemented: movsx "s + args[0].type().name() + " "s + args[1].type().name()); + } +} + +namespace { + +bool registered { + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsxd"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsx"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) && + registerOp(mangleName("movsxd"), [](const Asm::Args& args) -> std::shared_ptr{ + return std::make_shared(args); + }) +}; + +} -- cgit v1.2.3