How to write self-modifying code in x86 assembly

后端 未结 7 1615
醉酒成梦
醉酒成梦 2020-11-28 18:38

I\'m looking at writing a JIT compiler for a hobby virtual machine I\'ve been working on recently. I know a bit of assembly, (I\'m mainly a C programmer. I can read most ass

7条回答
  •  无人及你
    2020-11-28 19:05

    I'm working on a self-modifying game to teach x86 assembly, and had to solve this exact problem. I used the following three libraries:

    AsmJit + AsmTk for assembling: https://github.com/asmjit/asmjit + https://github.com/asmjit/asmtk UDIS86 for disassembling: https://github.com/vmt/udis86

    Instructions are read with Udis86, the user can edit them as a string, and then AsmJit/AsmTk is used to assemble the new bytes. These can be written back to memory, and as other users have pointed out, the write-back requires using VirtualProtect on Windows or mprotect on Unix to fix the memory page permissions.

    The code samples are a just a little long for StackOverflow, so I'll refer you to an article I wrote with code samples:

    https://medium.com/squallygame/how-we-wrote-a-self-hacking-game-in-c-d8b9f97bfa99

    A functioning repo is here (very light-weight):

    https://github.com/Squalr/SelfHackingApp

提交回复
热议问题