From ffcfa5ea3f792caad679b81b6be07aac2186cdb6 Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Sat, 5 Aug 2023 10:45:52 -0300 Subject: [PATCH] add RegMem32 for nicer pointer syntax --- src/assembler/X86Assembler.hpp | 13 +++++++++++++ test/assembler/Main.cpp | 17 +++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/assembler/X86Assembler.hpp b/src/assembler/X86Assembler.hpp index 208d92f..d12c8d4 100644 --- a/src/assembler/X86Assembler.hpp +++ b/src/assembler/X86Assembler.hpp @@ -32,6 +32,19 @@ namespace tulip::hook { offset(offset) {} }; + inline X86Pointer operator+(X86Register reg, int32_t offset) { + return X86Pointer(reg, offset); + } + + // Use this to easily express a X86Pointer, like so: + // RegMem32 m; + // m[ESP], m[ESP + 4] + struct RegMem32 { + X86Pointer operator[](X86Pointer ptr) { + return ptr; + } + }; + class X86Assembler : public BaseAssembler { public: X86Assembler(uint64_t baseAddress); diff --git a/test/assembler/Main.cpp b/test/assembler/Main.cpp index 97e79ea..cbd8d31 100644 --- a/test/assembler/Main.cpp +++ b/test/assembler/Main.cpp @@ -35,6 +35,7 @@ void assertEqImpl(T&& a, U&& b, const char* expr, const char* file, int line) { int main() { using enum X86Register; + RegMem32 m; { X86Assembler a(0x123); @@ -65,24 +66,24 @@ int main() { X86Assembler a(0x123); a.mov(EAX, EAX); a.mov(ECX, EAX); - a.mov(ECX, X86Pointer(EDX, 4)); - a.mov(ECX, X86Pointer(EBP, 4)); - a.mov(X86Pointer(EBP, 4), ESP); - a.mov(X86Pointer(EBP), EAX); + a.mov(ECX, m[EDX + 4]); + a.mov(ECX, m[EBP + 4]); + a.mov(m[EBP + 4], ESP); + a.mov(m[EBP], EAX); assertEq(a.buffer(), "\x89\xC0\x89\xC1\x8B\x4A\x04\x8B\x4D\x04\x89\x65\x04\x89\x45\x00"_bytes); } { X86Assembler a(0x123); - a.movsd(X86Pointer(ESP), XMM0); - a.movsd(XMM1, X86Pointer(ESP, 4)); + a.movsd(m[ESP], XMM0); + a.movsd(XMM1, m[ESP + 4]); assertEq(a.buffer(), "\xF2\x0F\x11\x04\x24\xF2\x0F\x10\x4C\x24\x04"_bytes); } { X86Assembler a(0x123); - a.movss(X86Pointer(ESP), XMM0); - a.movss(XMM1, X86Pointer(ESP, 4)); + a.movss(m[ESP], XMM0); + a.movss(XMM1, m[ESP + 4]); assertEq(a.buffer(), "\xF3\x0F\x11\x04\x24\xF3\x0F\x10\x4C\x24\x04"_bytes); } } \ No newline at end of file