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,978 of 4,675   
   Bartc to Bernhard Schornak   
   Re: 64 bit stack alignment   
   30 Aug 17 11:29:59   
   
   From: bc@nospicedham.freeuk.com   
      
   On 30/08/2017 10:10, Bernhard Schornak wrote:   
   > Alex wrote:   
      
   >> 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.   
      
   This assumes the code doesn't use the stack for any other purposes   
   between function entry, and a call to a function that expects the stack   
   to be aligned (at the call).   
      
   For example, things may be put on the stack while evaluating a complex   
   expression and one of the terms requires a call. Or you are pushing   
   arguments 5, 6 or 7 of a complex call, and one of those expressions   
   itself involves a function call.   
      
   It is also possible, if not calling external functions, that calls to   
   internal functions in your code, which do not require alignment, do not   
   bother with keeping the stack aligned, or use a simple argument-passing   
   convention (and have pushed an even or odd number of parameters), or   
   don't need a stack frame.   
      
   For whatever reason, when it is necessary to call an external function,   
   it won't know the stack alignment.   
      
      
   (Solutions I've used:   
      
   * Call a special stub function for calling external functions. There is   
   a separate one for 4, 5, 6 etc parameters. It uses a check, and branch,   
   and will rearrange things as needed. It expects such calls to be rare.   
   Local calls use a private call convention.   
      
   * Keep track of how many things have been pushed onto the stack at any   
   point in an instruction sequence. Then it will know if the stack is   
   aligned or not and generate the correct code.   
      
   * Avoid using the stack for any purpose than for calling functions. And   
   in the latter case, it avoids nested calls (by pre-evaluating any such   
   terms). Both the last two I've used in generated code.)   
      
   --   
   bartc   
      
   --- 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