Where is there documentation on how to write inline asm?

pineapple meapineapple at gmail.com
Thu Nov 15 21:32:10 UTC 2018


On Thursday, 15 November 2018 at 21:12:39 UTC, Adam D. Ruppe 
wrote:
> On Thursday, 15 November 2018 at 21:07:51 UTC, pineapple wrote:
>> Is there a way to access this pointer?
>
> It is passed as.. I think the final argument to the function. 
> (unless it is the first, do a quick test to find out).
>
>> Also, the calling convention documentation there doesn't 
>> mention anything about 64-bit targets, are 64-bit registers 
>> just not used?
>
> It uses the C rules there, so look up the 64 bit C abi. I think 
> this is accurate 
> https://msdn.microsoft.com/en-us/library/ms235286.aspx

On the positive side: I see now how to return a 64-bit value from 
a function in Windows x64! And I understand how the arguments are 
coming in. This is very helpful.

On the less positive side: I still have no clue how to return my 
16-byte struct. The Microsoft x64 ABI documentation I've seen so 
far explains how return values of 8 bytes and fewer work, but 
haven't explained how larger return values work. The obvious 
answer of "RAX or EAX contains a pointer" is either not working 
or my asm is wrong. (The latter is certainly a possibility.)


     // Returns 128 (0x80)
     ulong retTest() {
         version(D_InlineAsm_X86_64) asm {
             naked;
             mov RAX, 0x80;
             ret;
         }
     }

     // Crashes
     struct Result {
         ulong low;
         ulong high;
     }
     Result retTest() {
         version(D_InlineAsm_X86_64) asm {
             naked;
             mov [RAX + 0], 0x80;
             mov [RAX + 8], 0x80;
             ret;
         }
     }

     // Also crashes
     struct Result {
         ulong low;
         ulong high;
     }
     Result retTest() {
         version(D_InlineAsm_X86_64) asm {
             naked;
             mov [R9 + 0], 0x80;
             mov [R9 + 8], 0x80;
             ret;
         }
     }



More information about the Digitalmars-d-learn mailing list