By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

How to dynamically call C functions using inline asm?

P: n/a
The arguments of function is variable. Given function address,
argument type and data,
how to dynamically call the function? The following is pseudo code.

int count = 0;
int offset = 0;
char buffer[SIZE];

count = getcount(buffer, offset);
void* pfun = getmethod(buffer, offset);

for (int i = 0; i < count; i++)
{
int type = gettype(buffer, offset);
void * data = getdata(buffer, offset, type);
__asm
{
push data
}
}

__asm
{
call pfun;
}
Is this feasible? Who can give me implementation codes? Or some
material about the topic?
Thank you very much.

Jun 15 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Allen wrote:
....
Is this feasible? Who can give me implementation codes? Or some
material about the topic?
Thank you very much.
While asm sections are defined in the C++ standard, the stuff inside
them is implementation specific. You'll need to ask a news group that
is specific to your platform to get the best answers.

Just curious, why would you ever want to do this ? There are standard
ways to call C functions from C++.
Jun 15 '07 #2

P: n/a
On 6 15 , 10 00 , Gianni Mariani <gi3nos...@mariani.wswrote:
Allen wrote:

...
Is this feasible? Who can give me implementation codes? Or some
material about the topic?
Thank you very much.

While asm sections are defined in the C++ standard, the stuff inside
them is implementation specific. You'll need to ask a news group that
is specific to your platform to get the best answers.

Just curious, why would you ever want to do this ? There are standard
ways to call C functions from C++.
I will implement our own rpc functions. I receive remote method call
with variable
arguments from TCP connection. And parse from the buffer to get all
the function
calling information. But how to call the method using asm.

Without inline asm, I will encapsulate local method individually.

Regards,
Allen Chen

Jun 15 '07 #3

P: n/a
Allen wrote:
On 6 15 , 10 00 , Gianni Mariani <gi3nos...@mariani.wswrote:
>Allen wrote:

...
>>Is this feasible? Who can give me implementation codes? Or some
material about the topic?
Thank you very much.
While asm sections are defined in the C++ standard, the stuff inside
them is implementation specific. You'll need to ask a news group that
is specific to your platform to get the best answers.

Just curious, why would you ever want to do this ? There are standard
ways to call C functions from C++.

I will implement our own rpc functions. I receive remote method call
with variable
arguments from TCP connection. And parse from the buffer to get all
the function
calling information. But how to call the method using asm.

Without inline asm, I will encapsulate local method individually.
What benefit do you gain from assembler?

--
Ian Collins.
Jun 15 '07 #4

P: n/a
Allen wrote:
On 6 15 , 10 00 , Gianni Mariani <gi3nos...@mariani.wswrote:
....
Without inline asm, I will encapsulate local method individually.
Look at libffi. It's a generic system for calling any function.
Jun 15 '07 #5

P: n/a
On 6 15 , 10 20 , Gianni Mariani <gi3nos...@mariani.wswrote:
Allen wrote:
On 6 15 , 10 00 , Gianni Mariani <gi3nos...@mariani.wswrote:
...
Without inline asm, I will encapsulate local method individually.

Look at libffi. It's a generic system for calling any function.
For variable argument function calling, ANSI c has stdarg.h and other
relivant files defined. But they are all used to parse arguments from
the stack. I wonder whether there are methods in ANSI c library to
build a va_list variable, not from directly function calling, i.e.
func(1,2,3) or func(1,2,3,4) to determine the argument stack?

Regards,
Allen Chen

Jun 15 '07 #6

P: n/a
Allen wrote:
On 6 15 , 10 20 , Gianni Mariani <gi3nos...@mariani.wswrote:
>Allen wrote:
>>On 6 15 , 10 00 , Gianni Mariani <gi3nos...@mariani.wswrote:
...
>>Without inline asm, I will encapsulate local method individually.
Look at libffi. It's a generic system for calling any function.

For variable argument function calling, ANSI c has stdarg.h and other
relivant files defined. But they are all used to parse arguments from
the stack. I wonder whether there are methods in ANSI c library to
build a va_list variable, not from directly function calling, i.e.
func(1,2,3) or func(1,2,3,4) to determine the argument stack?
There is no standard feature for doing what you describe. The only
things you can do are:

a) compile code to generate the function call.
b) call a library to create any stack frame for any function (like libffi).

Libffi is the only library I know of that does this kind of thing. It's
not exactly portable so you may need to extend it to work on your platform.
Jun 15 '07 #7

P: n/a
On Jun 15, 5:34 am, Allen <Allen.Che...@gmail.comwrote:
On 6 15 , 10 20 , Gianni Mariani <gi3nos...@mariani.wswrote:
Allen wrote:
On 6 15 , 10 00 , Gianni Mariani <gi3nos...@mariani.wswrote:
...
Without inline asm, I will encapsulate local method individually.
Look at libffi. It's a generic system for calling any function.

For variable argument function calling, ANSI c has stdarg.h and other
relivant files defined. But they are all used to parse arguments from
the stack. I wonder whether there are methods in ANSI c library to
build a va_list variable, not from directly function calling, i.e.
func(1,2,3) or func(1,2,3,4) to determine the argument stack?
I'm not sure what it is you want to do, do you want to be able to
store all functions in a list and call the appropriate one based on
the RPC-call?
A while ago I implemented a way to in a type safe manner store
functions with different signature in a uniform container.

One use I had for it was to handle HTTP requests in a CGI program:

For instance to handle these two URLs:
http://example.com/hello.cgi?cmd=red...place=bye.html
http://example.com/hello.cgi?cmd=add...gnusson&age=29

and call the correct function with the correct arguments,

void redirect(const std::string &page);
void add_user(const std::string &name, const std::string &last_name,
int age);

you could have a simple map:

typedef std::map<std::string, std::stringvalue_map;
typedef uniform_named_dispatcher<value_map, value_map_extractor>
http_dispatcher;
std::map<std::string, http_dispatcherm_;

m_["add_user"] = http_dispatcher(&add_user, "name", "last_name",
"age");
m_["redirect"] = http_dispatcher(&redirect, "place");

Then you'd just parse the HTTP-request into the value_map and call:

value_map vm = parse_http_request();
m_[vm["cmd"]].invoke(vm);

I think this method would be ideal to handle RPC-calls, but I've never
tried to do it so I wouldn't know for sure. I also use it with great
success in my Windows(tm) programs to call the right function for each
window message.

You can read more here on how the uniform_dispatcher works:
http://amag.rotd.org/tutorials.php?tid=1

Regards,
AM
Jun 15 '07 #8

P: n/a


Hi,

Well, in Visual C++/ms-windows win32 x86 the following applies:

The stack would look like this

Class::Function( Par1, Par2, Par3 )

<par3>
<par2>
<par1>
<this pointer>
[previous stack pointer]
local variables....

Note that since the number of arguments can be variable the this pointer has
to be last. In C and static functions there is no this pointer.

So what you need to do is in assembly push your parameters onto the stack

Then you could do several things. You could load the function address in eax
and do.

void *FunctionAddress = function;
__asm
{
move eax, functionaddress

push par3
push par2
push par1
push this ; if any
push bp
mov bp,sp
sub sp, sizeoflocalvars
call [eax]
mov sp,[bp] ;Clean up the stack
}

In C the caller cleans up the stack unlike pascal. Also windows functions
usually actually have pascal calling convention i.e. the function itself
adjusts sp. The reason is that C can have variable number of arguments
however on x86 you can return and clean (ret <number>) in one instruction
making pascal calling convention a bit more efficient.

It think it would something like this. It is of the top of my head so I am
not 100% sure onlye 95% or so :-)

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Allen" <Al**********@gmail.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
The arguments of function is variable. Given function address,
argument type and data,
how to dynamically call the function? The following is pseudo code.

int count = 0;
int offset = 0;
char buffer[SIZE];

count = getcount(buffer, offset);
void* pfun = getmethod(buffer, offset);

for (int i = 0; i < count; i++)
{
int type = gettype(buffer, offset);
void * data = getdata(buffer, offset, type);
__asm
{
push data
}
}

__asm
{
call pfun;
}
Is this feasible? Who can give me implementation codes? Or some
material about the topic?
Thank you very much.

Jun 15 '07 #9

P: n/a
Ron AF Greve wrote:
Hi,

Well, in Visual C++/ms-windows win32 x86 the following applies:

The stack would look like this

Class::Function( Par1, Par2, Par3 )

<par3>
<par2>
<par1>
<this pointer>
[previous stack pointer]
local variables....

Note that since the number of arguments can be variable the this pointer has
to be last. In C and static functions there is no this pointer.

So what you need to do is in assembly push your parameters onto the stack
[...]

Please don't post offtopic advice. And please don't top-post. Read the FAQ.

The problem is, the stack layout heavily depends on the platform and
compiler. For example, on Visual C++ the this-pointer is in ECX and not on
the stack (whenever I looked).

So calling a function by assembler should be asked and answered only on a
platform specific newsgroup.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 15 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.