Page 2 of 2

Re: Assembly language function template - A lazy convention

Posted: Fri Sep 10, 2010 12:08 am
by gerryg400
smwikipedia wrote:Just a sum up.

I finally come to the following code template as a simple & lazy convention:

Code: Select all

  pushl %ebp
  movl %esp, %ebp
  pushal          <--- save all the registers, this is kind of a lazy solution
  subl xxx, %esp  <--- allocate space for local variables
  ....    
  addl xxx, %esp  <--- reclaim the space for local variables
  popal           <--- restore all the registers
  (we can set return value in general purpose register here)
  movl %ebp, %esp
  popl %ebp
  ret yyy     <--- for __stdcall convention, the callee will clear the parameters pushed on stack by caller
pusha actually pushes ALL registers inlcuding ebp and esp. So, if you are going to push everything anyway, why not do this ?

Code: Select all

  pushal   <--- save all the registers including ebp and original esp, this is kind of a lazy solution
  movl %esp, %ebp
  subl xxx, %esp  <--- allocate space for local variables
  ....    
  movl %ebp, %esp
  popal           <--- restore all the registers
  ret yyy     <--- for __stdcall convention, the callee will clear the parameters pushed on stack by caller
This is then the standard c prologue/epilogue with pushal replacing push ebp.

The problem with both the methods is that the popal trashes all your registers so you need to put the return value somewhere in RAM before you do it.

Re: Assembly language function template - A lazy convention

Posted: Fri Sep 10, 2010 2:15 am
by gerryg400
berkus wrote:And of course pushal/popal pair will be hellishly slow, especially compared to no push/pop when register convention is used (for a reason).
Yep, I agree. But the OP is looking for the 'lazy' way to do it.