Assembly language function template - A lazy convention

Programming, for all ages and all languages.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Assembly language function template - A lazy convention

Post 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.
If a trainstation is where trains stop, what is a workstation ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Assembly language function template - A lazy convention

Post 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.
If a trainstation is where trains stop, what is a workstation ?
Post Reply