how call a function with parameters in eax and esi. gcc c++

1.1k views Asked by At

I need to call a function pointer using gcc that i can´t typedef in c++. I´m not good at asm so i would appreciate your help.

The function passes arg1 in eax and arg2 in esi. args are pushed right to left and the caller cleans the stack

signed int __usercall o_Fkt<eax>(int a1<eax>, int a2<esi>, int a3, int a4, int a5)

edit: Thanks Willem. I had success calling it this way:

    int callfn(void* a1, void* a2, int a3, int a4, void* a5 )
    {
        int result;
        unsigned long fktAddr = 0x0092FE40;
        __asm volatile(
                "push %[a5]\n\t"
                "push %[a4]\n\t"
                "push %[a3]\n\t"
                "call edx\n\t"
                "add esp, 12"
                : "=a" (result)
                :  "d" (fktAddr), "a" (a1), "S" (a2) , [a5] "r" (a5), [a4] "r" (a4), [a3] "r" (a3)
                : "memory"
        );
        return result;
    }

edit2: added "memory" to clobber list

1

There are 1 answers

1
Willem Hengeveld On

something like this - i did not test it, but the compiler output looks ok i think.

int oFkt(int a1);
int callfn(int a1, int a2, int a3, int a4, int a5)
{
    int result;
    __asm (
            "push %[a5]\n\t"
            "push %[a4]\n\t"
            "push %[a3]\n\t"
            "call oFkt\n\t"
            "add  esp, 12"
            : "=a" (result)
            : "a" (a1), "S" (a2), [a5] "r" (a5), [a4] "r" (a4), [a3] "r" (a3)
    );
    return result;
}

The =a output constraint causes the return value to be copied into result, the a input constraint loads a1 in eax, and S loads a2 into esi the rest is just pushing the other arguments.