3 ms·
Executing Numerical Constants as Code It would be better to use bytes, since x86 instructions can be as small as one byte (e.g. the C3 in 0x55C35D08 and 0x55C3
by userbinator 11y ago
Executing Numerical Constants as Code
It would be better to use bytes, since x86 instructions can be as small as one byte (e.g. the C3 in 0x55C35D08 and 0x55C3C9D2 is the return instruction, the last byte of the function, and the 55 is the start of the next one), but this is basically how a JIT works. If the access permissions are set right (or nonexistent, as the case may be), you can generate code into a buffer and execute it directly.
You could explore this further by modifying the 08 in 0x55C35D08 to any signed byte value (80 to 7F --- -128 to 127), resulting in a "JIT'd" function that adds that amount to its argument. The actual instruction is the 3 bytes 83 C0 08, which is "add eax, 8". Definitely not portable, but if you're "pseudo-JITting" pieces of code in your application, you are likely worrying about other more important things than portability.
(If you work with things like this enough, you start memorising the most common opcodes and can mentally disassemble hex dumps...)