Convert assembly to machine code in C++

前端 未结 3 1598
野趣味
野趣味 2021-01-07 01:57

I seek for any lib or function to convert a string of assembly code to machine code, like the following:

char asmString[] = {\"mov eax,13H\"};
byte[] output;         


        
3条回答
  •  迷失自我
    2021-01-07 02:40

    You should define what you really want:

    Do you want to generate machine code at runtime? Then use some JIT compilation library like libgccjit, libjit, LLVM, GNU lightning, or asmjit. asmjit is a library emitting x86 machine code, probably what you need. There is no absolute need to use a string containing assembler code.

    Or do you want to translate some assembler syntax (and there are several assembler syntaxes even for x86) to object code or machine code? Then you'll better run a real assembler as an external program. The produced object code will contain relocation directives, and you'll need something to handle these (e.g. a linker).

    Alternative, you should consider generating some (e.g.) C code at runtime, then forking a compilation, and dynamically loading and using the resulting function at runtime (e.g. with dlopen(3) and dlsym). See this

    Details are obviously operating system, ABI, and processor specific.

提交回复
热议问题