X86 Assembly Calling Convention
Published
Calling Convention
总结一下关于汇编函数调用过程的知识。
References:
http://www.cs.virginia.edu/~evans/cs216/guides/x86.html https://en.wikibooks.org/wiki/X86_Assembly
Make a subrouting call
-
Before calling a subrouting, save the caller-saved registers (
EAX, ECX, EDX). -
Push the parameters onto the stack.
-
Use
callinstruction, places the retrun address onto stack, and branches to the subroutine code.
Callee prologue
- Push old base pointer value, for restore it after subroutine.
push ebp
mov ebp, esp
- Allocate local variables by make space on the stack. e.g.
sub esp, 12
- Save the values of the callee-saved register push them onto stack (
EBX, EDI, ESI).
Callee body proceed
Callee epilogue
-
Leave the return value in
EAX. -
Restore the old values of any callee-saved registers (
EDI, ESI, EBX). -
Deallocate local variables.
mov esp, ebp
-
Restore the caller's base pointer value by poping
EBPfrom the stack. -
Return to the caller by executing a return
retinstruction, find and reomve the return address from the stack.
After subroutine return
-
Remove the parameters from stack.
-
Restore the contents of caller-saved registers (
EAX, ECX, EDX)