typedef unsigned char u8; typedef unsigned short u16; typedef unsigned long u32; typedef unsigned long long u64; typedef char s8; typedef short s16; typedef long s32; typedef long long s64; #define TLS __declspec(thread) #define OP1(op, b1) struct op##_t : b1 { using b1::operator(); }; op##_t op; #define OP2(op, b1, b2) struct op##_t : b1, b2 { \ using b1::operator(); using b2::operator(); }; op##_t op; namespace X86 { TLS u8 *x86Ptr; void Write8(u8 b) { *x86Ptr++ = b; } void Write16(u16 w) { *(u16*)x86Ptr = w; x86Ptr += 2; } void Write32(u32 dw) { *(u32*)x86Ptr = dw; x86Ptr += 4; } void Write64(u64 qw) { *(u64*)x86Ptr = qw; x86Ptr += 8; } // Left to right opcode helper for readability template void WriteOp() { if (opcode >> 24) Write32(opcode); else { if (opcode >> 16) Write8((opcode >> 16) & 0xffU); if (opcode >> 8) Write8((opcode >> 8) & 0xffU); Write8(opcode & 0xffU); } } template struct Naked { void operator()() { WriteOp(); } }; template struct Imm8 { void operator()(int ib) { WriteOp(); Write8((u8)ib); } }; namespace X86_32 { OP1(AAA, Naked<0x37>); OP2(AAD, Naked<0xD50A>, Imm8<0xD5>); }; namespace X86_3264 { }; }; using namespace X86::X86_3264; using namespace X86::X86_32; int main() { AAA(); AAD(); AAD(16); AAD(-16); return 0; }