Skip to content

Commit

Permalink
add RegMem32 for nicer pointer syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Aug 5, 2023
1 parent d285500 commit ffcfa5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/assembler/X86Assembler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions test/assembler/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit ffcfa5e

Please sign in to comment.