justin = { main feed , music , code , askjf , pubkey };
Ask Justin Frankel
No reasonable question unanswered since 2009!

Suggested topics: programming, music, sleep, coffee, etc.

Note: please do not ask questions about REAPER features, bugs or scheduling, use the forums instead.


Name: Ask: Human (enter yes):
[back to index] | [unreplied] | [replied] | [recent comments] | [all]

Question: Could you explain at a high level how C code can call run-time created machine code? How do you set that up programtically?
Asked by Will (24.234.128.x) on September 28 2010, 11:35am
Reply on September 28 2010, 6:29pm:
    You can see how EEL2 does this, but I'll give a basic explanation here:

    First off, this is highly dependent on architecture, compiler, OS, etc. Generally, though, this is the process:
    1. Generate the code you wish to run. This can be done by writing the opcodes to a buffer, or by combining stubs of code (Using nasm can be helpful, otherwise you can use __declspec(naked) on MSVC and use inline assembly, or you can use gcc and inline assembly and strip out the compiler generated code, etc).
    2. Mark the pages of code as executable, using VirtualProtect() on win32 or (from memory, I think it is) mprotect on posix.
    3. Execute the code -- you can do this using inline assembly or an assembly stub function, and it would essentially do: "mov eax, address ; call eax". or similar.
    4. Watch things explode! Your code will likely need to save registers, align the stack, etc. The devil is often in the details.

Comment:
    Your Name:   -- Site Owner's Name:  (for human-verification)

    Comment:    

    
  
[back to index] | [unreplied] | [replied] | [recent comments] | [all]
Copyright 2025 Justin Frankel. | RSS