Operating System - HP-UX
1753447 Members
4898 Online
108794 Solutions
New Discussion

how to call a function by aCC inline assembly

 
SOLVED
Go to solution
jufengyao
Advisor

how to call a function by aCC inline assembly

Hi,

 

I want to call a function by aCC inline assembly, but can't find a pseudo-function which can let me to make a call (some thing like _Asm_call).

any suggestions will be great help!

 

thanks.

25 REPLIES 25
jufengyao
Advisor

Re: how to call a function by aCC inline assembly

if any one can give out a sample code of call a function by aCC inline assembly of IA64, it will be more better: include push parameters, call, and return.

Dennis Handly
Acclaimed Contributor

Re: how to call a function by aCC inline assembly

>I want to call a function by aCC inline assembly,

 

You can't and there is no need to do so.  You do a normal call and intersperse your inline assembly _Asm_ calls.

You might want to explain what you are trying to do?

jufengyao
Advisor

Re: how to call a function by aCC inline assembly

Hi Dennis,

 

thanks for your reply. I need to implement a call function interface. the method define is: 

void* callfunction(int *data, int* type, int count, void* function_address). data: this array contains the parameters which will be passed to called function with the address: function_address, count:  is the len of data, type: indicate the return type, can be int, float, double, long.

 

so I need to implement by assembley, can't use some thing like: function_address(.....) to call directly. 

seems no inline asssembly can reach arm, but assembly can. can you give me an example of how to implement callfunction?

 

so thanks

Dennis Handly
Acclaimed Contributor

Re: how to call a function by aCC inline assembly

>I need to implement a call function interface.

 

Are you writing some kind of interpreter?

 

>void* callfunction(int *data, int *type, int count, void* function_address).

 

What is this function returning in the void*?

The proper declaration for a generic function is: void (*function_address)(void)

 

>data: this array contains the parameters which will be passed to

 

Are there count int parms by value?

 

>so I need to implement by assembly,

 

You can't do this in assembly either.

 

>can't use some thing like: function_address(.....) to call directly. 

 

You can but you need a doubly nested switch, one for the return type and one for the data parms.

For each return type and parms, you'll need to cast function_address to that function.

switch (*type) {

case T_int:

   int ii = ((int (*)(int))function_address)(data(0));

 

I'm not sure how you are encoding your data array and count?

jufengyao
Advisor

Re: how to call a function by aCC inline assembly

> switch (*type) {

> case T_int:

>   int ii = ((int (*)(int))function_address)(data(0));

 

this is exactly the usage of this function, not the implementation. it needs return void* because it can return int, long, double, float, or a pointer. void* can be cast to any type.

 

"type" array indicates the type of data element, which can be int, float, long, double, pointer.

"count" indicates the length of type array.

"data" is an int array, but it is only a memory block, which will hold data format: int, float, long, double, pointer, according to the relative type.

 

the aim of data, type, and count is that: we can get input parameters for function whose address is function_address.  the design of callfunction is to call any functions with differenet number & type of paramters.

 

because IA-64 use registers from r32 as in/local/out registers, and pass parameters by out registers, it is hard to let callfunction to call any number parameters functions. so I limit the called funtions' params' number to 4. that means called function at most has 4 parameters.

 

thanks for your reply.

I have implemented callfunction by asm.

jufengyao
Advisor

Re: how to call a function by aCC inline assembly

oh, 

> switch (*type) {

> case T_int:

>   int ii = ((int (*)(int))function_address)(data(0));

it is calling the function whose address is function_address. but the calling function's params' number is not fixed, it can have no param, or many. so I get no idea that I can directly call in this way.

Dennis Handly
Acclaimed Contributor

Re: how to call a function by aCC inline assembly

>It needs return void* because it can return int, long, double, float, or a pointer. void* can be cast to any type.

 

A void* can only be cast to a pointer to objects, not double/float, etc.

And if cast to a pointer, where is it going to point, if the function really returns an int?

 

>the aim of data, type and count is that: we can get input parameters for function whose address is function_address.  the design of callfunction is to call any functions with different number & type of parameters.

 

Unless you have a more complex description of this, you can't do it with just those three.

 

>because IA-64 use registers from r32 as in/local/out registers, and pass parameters by out registers, it is hard to let callfunction to call any number parameters functions. So I limit the called functions' parms' number to 4. That means called function at most has 4 parameters.

 

The max is 8.  The rest are on the stack.

 

>but the calling function's parm number is not fixed, it can have no parm or many.

 

You must have a switch for each possible case.  Or always pass 8 parms.

jufengyao
Advisor

Re: how to call a function by aCC inline assembly

> A void* can only be cast to a pointer to objects, not double/float, etc.

 

it is not true. void* is a pointer, and a pointer can be cast by forcely to any other types. only need they have the same bytes. for example:

 

void* p = ...;

int ip = (int)(long)p; // for 64bit, pointer is 8 bytes, but int only 4 bytes, so first cast to long, then to int. but will lose 0x60000000000000000000

 

> if cast to a pointer, where is it going to point, if the function really returns an int?

 

it will truely cause an error under 64bit, but is okie under 32bit. when call a function, accroding to the return type, it will use the different return value funtion. if return type is float, float returning function will be called, e.g.

 

> The max is 8.  The rest are on the stack.

 

this is useful. thanks. :-)

 

> You must have a switch for each possible case.  Or always pass 8 parms.

 

correctly. that is the reason why I need assembly. because directly call as you suggested  can't reach this aim. because of different number of params.

 

thanks for your great help.

 

 

 

jufengyao
Advisor

Re: how to call a function by aCC inline assembly

what I have done is this: according to the number of params of called function, I use a switch to every case. but limit the number of params to 4 at most