home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 2,977 of 4,675   
   Bernhard Schornak to Alex   
   Re: 64 bit stack alignment   
   30 Aug 17 11:10:20   
   
   From: schornak@nospicedham.web.de   
      
   Alex wrote:   
      
      
   > On 64 bit Windows, stack alignment on a 16 byte boundary is required before   
   calling all except a   
   > leaf function. In the called function, the stack is 8 mod 16.   
   >   
   > Now, I'm struggling to come up with a way of doing it beyond this code   
   (which I didn't invent, but I   
   > can't for the life of me remember where I found it.)   
   >   
   >   push   rsp   
   >   push   [rsp]   
   >   and    spl $F0   
   >   call   funkychicken   
   >   pop    rsp   
   >   
   > It seems to be the only way of doing this without branches, flags or other   
   expensive nonsense. But,   
   > as ever, there may be a better way. Any suggestions?   
      
      
   If we assume the stack was properly aligned by the calling function,   
   there is only one way to align the stack while your function code is   
   running:   
      
      
   sub $0x?8, %rsp   
   ...   
   function code   
   ...   
   add $0x?8, %rsp   
   ret   
      
      
   The subtracted space must be at least 32 (0x20) byte for microsoft's   
   'red zone' plus 8 byte for the return address (pushed onto the stack   
   by the calling function!), so a 'leaf function' should subtract 0x28   
   to work properly in a multithreaded environment. This subtraction of   
   40 (0x28) automatically aligns your stack if it was properly aligned   
   before. Add as many paragraphs (0x28 + n*16 byte) as required as the   
   local storage for your function.   
      
   *After* the subtraction, addresses 0x00(%rsp) through 0x1F(%rsp) are   
   reserved for called functions. Some qwords above 0x20(%rsp) might be   
   required as well to pass parameters to called functions which do not   
   fit into the first four registers (rcx, rdx, r8, r9).   
      
      
   Greetings from Augsburg   
      
   Bernhard Schornak   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca