Inline assembly help!

Started by
6 comments, last by MENTAL 18 years, 8 months ago
Alright. So the reason I am trying to use inline assembly is because I want to use it to push and pop stuff off the stack to deal with calling dlls. My question is, what needs to go before a function definition, i.e. __stdcall, or __cdecl? I have read a little about both but don't fully understand either. Next I have been having trouble with saving the stack and then restoring it before I return...

void __stdcall MyDllFunction(void);

void main(void)
{
     MyDllFunction();
}

void __stdcall MyDllFunction(void)
{
	//prolog
	__asm {
		push ebp
		mov ebp, esp
	}

	//epilog
	__asm {
		pop ebp
	}
}

So, I think that is corrent. Compiles on mine. But the problem is, how do I push and pop args onto the stack before I call the function? Because if I push or pop anything it destroys the stack. Is there any easier way to do interfunction communication to and from dlls? Thanks. If anything is unclear, let me know.
Advertisement
With __stdcall and __cdecl, the compiler will set up the stack frame for you. I think there's a __naked declaration to prevent that so you can set up the stack frame yourself.

That might not be what you're looking for because I have noo idea what you are trying to do
Hmmm, __naked didn't work.

Anywayz, sorry for the unclearness.

Basically I want to be able to push a couple of parameters before I call the function, then when i enter the function, use the stuff pushed onto the stack, and then exit the function.

The reason I am using assembly is because each of the functions that I am going to be calling is going to use different parameters. And when I am doing LoadLibrary() type stuff I was under the understanding that you couldn't really pass parameters. I.E.

/*When the object is created, the library and associated function are loaded into this variable.  Each object will use different variables in different ways. */void (*func_ptr) (void);


Before I call the objects function, I would like to be able to put some addresses and things like that onto the stack before calling the function. Then the function will manipulate the data and then return.

I really sorry about being vague. It is mostly because I am very new to DLLs and inline assembly.

Thanks for all the help. Any information is appreciated.
Of course you can pass parameters to functions inside DLLs when using LoadLibrary/GetProcAddress, e.g.

typedef IDirect3D9* (WINAPI *ddCreateD3D9t)(UINT);ddCreateD3D9t ddCreateD3D9 = NULL;// get procedure address for Direct3DCreate9:ddCreateD3D9 = (ddCreateD3D9t) GetProcAddress( hD3DInstance, "Direct3DCreate9" );if( !ddCreateD3D9 ) return 0://// initialize Direct3D9//Direct3D9 = ddCreateD3D9( D3D_SDK_VERSION );if( !Direct3D9 ) return 0;


or something similar should work.
Quote:Original post by Ace826
Hmmm, __naked didn't work.


I think this is what you want:

typedef int(*Func)(int,char*,const std::string &);Func myfunc = (Func)GetProcAddress(dllInstance,"MyFunc");int result = myfunc(1,"foo","bar");


Of course you don't have to use the typedef but it helps.

I've used naked before but only to do some really really strange stuff, and it seems that what you're trying to do doesn't qualify as really strange.

Here's an example of what naked can do:

__declspec( naked ) __int64 rdtsc(){  __asm rdtsc;  __asm ret;}
It could be that, or it could be that he wants to load functions that are unknown at compiletime.. For example, PHP can load (almost) any dll, and any functions within it, if using some extensions (w32api and/or FFI)..

Anyway, to do this, you must be familiar with the calling conventions and stuff, but if you are, nothing should stop you from doing it (it's alot of work though I think)
Wow thanks for all the info guys!

Going to work on it a little more. :)

Trying to pass an unknown number of parameters. When function is called it will pull the number of params it needs. After checking for proper number of params. I think I almost have it. Assembly is so kool.

Thanks again for the help.
the ... operator is your friend.

This topic is closed to new replies.

Advertisement