C function ptr to asm code with nonstandard arg passing

Hi all

I have a piece of code written in x86 asm (compiled with nasm) and I
would like to call this function from C code through a function pointer.

The problem is that the asm function expects arguments into some x86
registers that are not the GCC-calling-convention standard ones, even
if using attribute((regparm(X))) on the func pointer.

In Watcom/QNX4 I have used the following code:

typedef void (myfunc_t) ( char *, char , int );
#pragma aux myfunc_t modify [eax] parm [esi] [edi] [ecx];
myfunc_t
Funcp;


But using the following GCC code:

typedef void (myfunc_t) ( char *, char , int )
attribute((regparm(3)));
myfunc_t
Funcp;

the code generated when calling (*Funcp) put the three arguments into
EAX, EDX and ECX, respectively, that is not what I need.

Any idea?

Thanks in advance,
Davide

Davide Ancri <nospam@please.me> wrote:
DA > Hi all

DA > I have a piece of code written in x86 asm (compiled with nasm) and I
DA > would like to call this function from C code through a function pointer.

DA > The problem is that the asm function expects arguments into some x86
DA > registers that are not the GCC-calling-convention standard ones, even
DA > if using attribute((regparm(X))) on the func pointer.

DA > In Watcom/QNX4 I have used the following code:

DA > typedef void (myfunc_t) ( char *, char , int );
DA > #pragma aux myfunc_t modify [eax] parm [esi] [edi] [ecx];
DA > myfunc_t
Funcp;


DA > But using the following GCC code:

DA > typedef void (myfunc_t) ( char *, char , int )
DA > attribute((regparm(3)));
DA > myfunc_t
Funcp;

DA > the code generated when calling (*Funcp) put the three arguments into
DA > EAX, EDX and ECX, respectively, that is not what I need.

DA > Any idea?

DA > Thanks in advance,
DA > Davide

I am not a GCC expert. I don’t know if you can do what you want.

But you can write a cover function in asm that receives the arguments
the way the C compiler puts them on the stack and then re-call your
other asm function.

Bill Caroselli wrote:

I am not a GCC expert. I don’t know if you can do what you want.

But you can write a cover function in asm that receives the arguments
the way the C compiler puts them on the stack and then re-call your
other asm function.

Of course I can… but the goal is to achieve very high performances.

I have access to the asm source also, so I can rewrite the asm func to
receive its parameters on the GCC-standard ones, but it seems strange
to me that GCC guys have never coped whit a problem like this.

Thanks for your tips!

Davide

Something like

asm volatile( “jmp *%0;” : “r”(funcp), “S”(arg1), “D”(arg2), “c”(arg3) )

should do the trick… You may need to worry about mentioning registers
etc that the function clobbers…

Davide Ancri wrote:

Hi all

I have a piece of code written in x86 asm (compiled with nasm) and I
would like to call this function from C code through a function pointer.

The problem is that the asm function expects arguments into some x86
registers that are not the GCC-calling-convention standard ones, even if
using attribute((regparm(X))) on the func pointer.

In Watcom/QNX4 I have used the following code:

typedef void (myfunc_t) ( char *, char , int );
#pragma aux myfunc_t modify [eax] parm [esi] [edi] [ecx];
myfunc_t
Funcp;


But using the following GCC code:

typedef void (myfunc_t) ( char *, char , int )
attribute((regparm(3)));
myfunc_t
Funcp;

the code generated when calling (*Funcp) put the three arguments into
EAX, EDX and ECX, respectively, that is not what I need.

Any idea?

Thanks in advance,
Davide


cburgess@qnx.com

Colin Burgess wrote:

Something like

asm volatile( “jmp *%0;” : “r”(funcp), “S”(arg1), “D”(arg2), “c”(arg3) )

should do the trick… You may need to worry about mentioning registers
etc that the function clobbers…

This way I can declare an inline func to be used in C code instead of
the call through func pointer (with “call” in place of “jmp” in the
inline asm, I suppose…), but this means changing the C code in every
place the func ptr is dereferenced, and not only the “function type”
declaration.

I think the cleanest way is rewriting the asm function to accept
params into the “standard” registers.

Thanks to all!
Davide