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 --- Makefile | 1 + asm/intel64/all_ops.h | 1 + asm/intel64/movsx.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ asm/intel64/movsx.h | 31 ++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 asm/intel64/movsx.cpp create mode 100644 asm/intel64/movsx.h diff --git a/Makefile b/Makefile index 5394354..a58b18c 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,7 @@ PROGSRC=\ asm/intel64/int.cpp \ asm/intel64/jmp.cpp \ asm/intel64/mov.cpp \ + asm/intel64/movsx.cpp \ asm/intel64/mul.cpp \ asm/intel64/neg.cpp \ asm/intel64/not.cpp \ diff --git a/asm/intel64/all_ops.h b/asm/intel64/all_ops.h index 75b28f9..fc38b3f 100644 --- a/asm/intel64/all_ops.h +++ b/asm/intel64/all_ops.h @@ -16,6 +16,7 @@ #include "neg.h" #include "not.h" #include "mov.h" +#include "movsx.h" #include "mul.h" #include "or.h" #include "pop.h" 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); + }) +}; + +} diff --git a/asm/intel64/movsx.h b/asm/intel64/movsx.h new file mode 100644 index 0000000..a31f538 --- /dev/null +++ b/asm/intel64/movsx.h @@ -0,0 +1,31 @@ +// Move With Sign-Extension + +#pragma once + +#include + +class Op_movsx: public Op +{ +public: + Op_movsx(const Asm::Args& args); + +public: + std::vector getCode() override + { + return machine_code; + } + + size_t size() override + { + return machine_code.size(); + } + + bool optimize() override ///< returns true if changed + { + return false; + } + +protected: + std::vector machine_code; +}; + -- cgit v1.2.3