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) 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 { struct AAA_t : Naked<0x37> {}; AAA_t AAA; struct AAD_t : Naked<0xD50A>, Imm8<0xD5> {}; AAD_t AAD; }; namespace X86_3264 { }; }; using namespace X86::X86_3264; using namespace X86::X86_32; int main() { AAA(); AAD(); AAD(16); AAD(-16); return 0; }