views:

167

answers:

3

Hi guys!

For my small hobby project I need to emit machine code from C++ program in runtime. I have base address 0xDEADBEEF and want to write something like this:

Assembler a((void*)0xDEADBEEF);
a.Emit() << 
  Push(Reg::Eax) <<
  Push(Reg::Ebx) <<
  Jmp(0xFEFEFEFE);

Inline assembler isn't my choice because generated machine code is dependent of the program state.

Does anybody know any existing library for doing this? If no, would it be a good idea to develop one from scratch and make it open source? (I mean, will anybody ever use this library if it existed?)

+2  A: 

This might be a overkill, because it supports many platforms, has its own intermediate language, does optimizations, etc, but the http://llvm.org/ seems to be a god try.

Christian
That's a cool project, but it's too heavyweight and (as far as I understand) doesn't give me fine control over generated instructions. Thanks anyway.
Anton
+2  A: 

You could use Nicolas Capen's softwire. Its really not supported any more as he now works on a similar product at Transgaming called SoftAsm. Still it kinda does what you want.

Goz
+2  A: 

Check out Asmjit. It seems to be the thing you're looking for.

Taavi