#include "div.h" #include "codes.h" #include #include #include using namespace std::string_literals; Op_div::Op_div(const Asm::Args& args) { if (args[0].type() == typeid(Asm::Args::Register8)) { // div reg8 (accu is al (remainder=ah) <- ah / reg8) machine_code = std::vector{ 0xF6 } + ModRM("/6", std::any_cast(args[0]).name()); } else if (args[0].type() == typeid(Asm::Args::Register32)) { // div reg32 (accu is eax (remainder=edx) <- edx:eax / reg32) machine_code = std::vector{ 0xF7 } + ModRM("/6", std::any_cast(args[0]).name()); } else if (args[0].type() == typeid(Asm::Args::Register64)) { // div reg64 (accu is rax (remainder=rdx) <- rdx:rax / reg64) machine_code = REX("W") + std::vector{ 0xF7 } + ModRM("/6", std::any_cast(args[0]).name()); } else if (args[0].type() == typeid(Asm::Args::Mem8Ptr64)) { // div byte ptr [reg64] (accu is al (remainder=ah) <- ah / x) Asm::Args::Mem8Ptr64 ptr{std::any_cast(args[0])}; machine_code = std::vector{ 0xF6 } + ModRM("/6", ptr.reg(), ptr.offs()); } else if (args[0].type() == typeid(Asm::Args::Mem32Ptr64)) { // div dword ptr [reg64] (accu is eax (remainder=edx) <- edx:eax / x) Asm::Args::Mem32Ptr64 ptr{std::any_cast(args[0])}; machine_code = std::vector{ 0xF7 } + ModRM("/6", ptr.reg(), ptr.offs()); } else if (args[0].type() == typeid(Asm::Args::Mem64Ptr64)) { // div qword ptr [reg64] (accu is rax (remainder=rdx) <- rdx:rax / x) Asm::Args::Mem64Ptr64 ptr{std::any_cast(args[0])}; machine_code = REX("W") + std::vector{ 0xF7 } + ModRM("/6", ptr.reg(), ptr.offs()); } else { throw std::runtime_error("Unimplemented: div "s + args[0].type().name()); } } namespace { bool registered { registerOp(mangleName("div"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && registerOp(mangleName("div"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && registerOp(mangleName("div"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && registerOp(mangleName("div"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && registerOp(mangleName("div"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) && registerOp(mangleName("div"), [](const Asm::Args& args) -> std::shared_ptr{ return std::make_shared(args); }) }; }