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
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
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.