Assembler vs function pointers #65

T-Maxxx opened this Issue Sep 7, 2016 · 4 comments


None yet

2 participants

T-Maxxx commented Sep 7, 2016

As we know there are some .asm files used to create bodies for some functions using "jmp addr" instructions and to export functions for plugin system. This is working exactly like functors in C/C++. Why not use them instead of nasm? As a profit - a bit faster compilation and less requirements for target system.

D4edalus commented Sep 7, 2016

maybe you can show how that would look like for one function?

T-Maxxx commented Sep 7, 2016 edited

void (*Com_Printf)(const char * fmt, ...) = (void(*)(const char *, ...)0x12345678;
Where 0x12345678 is an address in lnxded binary.
Variable Com_Printf (also called function pointer or functor) at a compilation will became a function with next body: jmp dword ptr [0x12345678]. Exactly the same we can see in .asm files.
Here's pseudo assembler code will be generated for Com_Printf("test format %d\n", 0x123);:

push 0x123; push "test format %d\n"; call Com_Printf => jmp 0x12345678 => (original function) => (return back to Com_Printf call).
Obviously, this will work only for __cdecl functions because stack is cleared after original function code. Variadic arguments in declaration always means this is __cdecl function, for other, you must include prologue and epilogue where you will reserve and clear function stack, or save\restore registers (used for IDA's __usercall, __stdcall, __thiscall) etc.

T-Maxxx commented Sep 13, 2016

I can convert assembly code into functors and update compiler script if you want.

T-Maxxx commented Sep 14, 2016

Whop, one more reason to get rid of nasm #76

@T-Maxxx T-Maxxx self-assigned this Sep 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment