[RESOLVED] Question about volatile registers - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: [RESOLVED] Question about volatile registers

  1. #16
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,976

    Re: [RESOLVED] Question about volatile registers

    When your code deals with the Windows API, it needs to fully support the calling convention used by the Windows API. For Win64, there is only 1 calling convention and that's the fastcall method. It's somewhat of an oddball principle since some registers get preserved (r15 is one) and some don't (r11 among others).
    I guess they decided on this to find a balance between what gets preserved and performance.

    Regardless, when your code calls into the Win API, it needs to follow the FULL set of rules of the Win API.


    Inside your own code, you can use whatever parameter passing rules as you deem best. Even different calling conventions in different bits of code if you really want to, you just need to rigorously follow whatever rules are in place for each function that gets called. And if one fucntion calls another function it's part of that 1st function to properly save/restore whatever is needed to assure that the calling rules of that 1st function are guaranteed.

    You really don't need to worry at all about the SYSCALL/SYSRET instructions unless you are using those in your own code (which would mean it's kernel code, which... is somewhat unlikely). Those 2 instructions are somewhat strange in that they don't preserve all registers where you might think they do when compared to how a regular call/ret work.

  2. #17
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,556

    Re: [RESOLVED] Question about volatile registers

    Regardless, when your code calls into the Win API, it needs to follow the FULL set of rules of the Win API.
    Are these rules officially documented by Microsoft somewhere? I've picked up most of my knowledge in this area from reading various articles.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #18
    Join Date
    Aug 2009
    Location
    Finally back in Alaska
    Posts
    141

    Re: [RESOLVED] Question about volatile registers

    http://msdn.microsoft.com/en-us/library/ms235286.aspx

    Youll find most everything in the above article, Ive reread those pages a hundred times it seems. If you dont completely get a part of it you can usually find another page that will explain that aspect a little deeper with a quick search.

    When your code deals with the Windows API, it needs to fully support the calling convention used by the Windows API. For Win64, there is only 1 calling convention and that's the fastcall method.
    I have my code set up where it expects any register not used directly for passing parameters to or from a function to be unchanged. rax, rcx, rdx are rarely saved for that reason. Ive also arranged it so that any of the windows api calls are made through a function internal to my code so that if I change things around I dont have to go through the entire code base to change things. Ive now got it where any functions that call into windows saves all volatile registers (plus whatever non-volatiles that function uses) upon entering, and restoring them before it exits (after the function makes the call into windows) and everything appears to be running smoothly now. It just through me for a loop because I switch between using my function for memory management and using malloc (either case GetMem is called, calling malloc inside GetMem is configured that way) and hadnt run into any problems till I changed that function (I increased the amount of memory it reserved for itself). From everything I can tell, the few windows functions I call into pretty much returns the registers unchanged.

    Anyhow, I usually dont come in here bugging yall till I start getting frustrated, I just took a while to track down where the problem was. If you go through most of my posts, the problem usually boils down to my having a difficult time debugging an area of code. For the most part Ive found debugging assembly code almost as easy as any of the other languages, but every once in a while I find something extremely difficult with no apparant way to make it easier (still learning though). In the end, its usually one of yall that points it out or leads me to the solution. Thanks

  4. #19
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,976

    Re: [RESOLVED] Question about volatile registers

    Quote Originally Posted by 2kaud View Post
    Are these rules officially documented by Microsoft somewhere? I've picked up most of my knowledge in this area from reading various articles.
    Yes
    Without this, it would be impossible for non-MS companies/organisations to write any form of compiler that works with the Windows API.

    What is remarkable (somewhat) is that MS opted to support only a single calling convention for the API (most of the Win32 API is __stdcall, some are __cdecl) as well as for the user code via the MS compiler. So your own code uses the same set of rules as the Windows API does.
    This may or may not be the same for other compilers, but in a way, I guess it would make little sense to try and support 2 methods.

    The link AKRichard privides in #18 is only the tip of the iceberg though, there's additional pages to explain in more details how it all works (such as how varargs work). Even the exception mechanism is fully documentend. (not that you need to know that amount of detail except (pun intended) if you were writing your own compiler).

    But again, that's how Windows API and MS VC does it.

    In Assembly you can do whatever you want as long as you rigourously follow your own rules. there may be good reasons to make every register (non)volatile. There's advantages and disadvantages to either point of view. The hybrid solution MS chose has advantages (and disadvantages) too.


    Also, just because a register is marked volatile... doesn't mean that a windows API call WILL change that register (or even if it does, change it all the time). This may give you a false impression that not saving works... until some condition is met that causes the register to change where it didn't before.

  5. #20
    Join Date
    Aug 2009
    Location
    Finally back in Alaska
    Posts
    141

    Re: [RESOLVED] Question about volatile registers

    This may give you a false impression that not saving works... until some condition is met that causes the register to change where it didn't before.
    Just learned that the hard way lol.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center