how to implement __fastcall for __declspec(naked) function?

Posted on 2004-11-07
Last Modified: 2012-06-21
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?
Question by:hengck23
    LVL 9

    Expert Comment

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

    Accepted Solution

    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

    LVL 22

    Expert Comment

    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.


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    IT Help Desk Summit 4 238
    Array in beta 10 262
    port mapped I/o query 10 111
    gcc error: -mpreferred-stack-boundary=2 is not between 4 and 12 7 1,692
    It Is not possible to enable LLDP in vSwitch(at least is not supported by VMware), so in this article we will enable this, and also go trough how to enabled CDP and how to get this information in vSwitches and also in vDS.
    It can often be challenging to stay relevant in the rapidly evolving world of technology. This can make recruiting talent difficult for companies of all sizes.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now