I am developing some functions in assembly language, at the beginning, my functions passing the parameters in the following 3 manners:
- registers
- global data in .data section
- stack
Now I found that the mix of the above 3 manners is making things complicated. And I always fall into the situation where I have to scratch my head to make sure whether certain register is polluted. So I decide to pass the parameters only through stack. And use the following function template as a lazy once-for-all solution:
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
....
popal <--- restore all the registers
movl %ebp, %esp
popl %ebp
(addl yyy, %esp)<--- if it is __stdcall convention, the callee will clear the stack
ret
The caller is responsible for push parameters and clear the stack (like the C call convention). Of course, if the number of parameters is fixed, I can make the callee to clear the stack (like the __stdcall convention on Windows).
I am hoping this template could relieve me from the confusions of registers usage. Could it achieve that? If it is low efficiency, is there some better approach? I'd like to hear your comment.
Many thanks.
ADD:
PUSHA instruction details: http://pdos.csail.mit.edu/6.858/2010/re ... /PUSHA.htm
RET instruction details: http://pdos.csail.mit.edu/6.858/2010/re ... 86/RET.htm