[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 804
  • Last Modified:

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?
  function(...);
  //what must i do here?
  ...
}
0
hengck23
Asked:
hengck23
  • 2
1 Solution
 
BeyondWuCommented:
>>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
0
 
BeyondWuCommented:
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
}

0
 
grg99Commented:
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.

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now