how to implement __fastcall for __declspec(naked) function?

Dear all,

I have just finished coding my __declspec(naked) function.
I want to pass variables through the registers. What are the important things i must take note?

__declspec(naked) void function( ...){

void someCallingFunction(...){
  //what must i do here?
  //what must i do here?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>>I want to pass variables through the registers. What are the important things i must take note?
Just don't use the registers which have been reserved for other purposes.
1. ebp, esp, ---> reserved for stack manipulation, don't pass your variables through these register
2. eax --->usually used as return value, but here I think you still can use it to pass your variables
You can search "Argument Passing and Naming Conventions" in MSDN
Keyword                         Stack cleanup                         Parameter passing
__cdecl                           Caller                                    Pushes parameters on the stack, in reverse order (right to left)
__stdcall                         Callee                                    Pushes parameters on the stack, in reverse order (right to left)
__fastcall                        Callee                                    Stored in registers, then pushed on stack
thiscall(not a keyword)     Callee                                    Pushed on stack; this pointer stored in ECX
So if you want to use __fastcall Conventions, you should clear the stack in your Callee function(...),
you can do it like this, e.g
__declspec(naked) void __fastcall function(int a, int ...)
  ret  x  ; Here, clean the stack! x = number of your parameters * 4


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You may find not a lot of speed increase.. If the top-level language is C or C++, then on some level it is likely the compiler has to generate code to save and restore the registers.   If you've pushed that overhead out of a loop, that's good.  But it's also possible to confuse the C compiler's optimizer this way-- this can lead to overall SLOWER code.   Try it both ways and see.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.