473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to dynamically call C functions using inline asm?

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
9 6950
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
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
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
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
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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg....
12
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of...
1
by: Zlatko Matiæ | last post by:
Hello. How can I call some functions on MSDE when working in Access (.mdb) that is connected to MSDE via ODBC linked tables ? Especially in-line functions, that I would like to use as recordset...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
9
by: asdf | last post by:
When and where should I use inline functions? I know any member functions inside classes are automatically inline function, how about the functions outside the classes? The inline functions is...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
2
by: aaragon | last post by:
Hi everyone, I would like to create a very simple function: // header file inline void point_map() { PointMap pointMap = get(vertex_point_t(), g); } // main.cpp
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.