February 12, 2008 articles rss Articles RSS

Intel x86 Function-call Conventions

This is a reference documenting some of the Intel x86 stack and register conventions.

In 32-bit land, Intel provides us with eight 32-bit registers.

  • %ebp (”base pointer”) - This points to the base of the current stack frame, which is usually set up at the beginning of a function to serve as a reference to the arguments. If function-call conventions are followed, the %ebp will usually point to the address containing the previous %ebp.
  • %esp (”stack pointer”) - This points to the top element on the stack, which is at the lowest memory address because the stack grows downwards. Assembly instructions that work with the stack, such as PUSH and POP, use and modify %esp.
  • %esi (”source index”) - This register is used to store the source address for other operations.
  • %edi (”destination index”) - Similarly, this register can be used to store the destination address for other operations.
  • %eax (”accumulator”) - This register is used to hold the return value from a function call. It can also be used as a temporary register in a function since it is a caller-save register.
  • %ebx (”base register”) - This register is a callee-save register that doesn’t really have a meaningful usage convention.
  • %ecx (”count register”) - This is a caller-save register that theoretically should be used for counters, such as i in a for loop.
  • %edx (”data register”) - This is another caller-save register that can be used as an extension for %eax.

For reference, the three caller-save registers are: %eax, %ecx, %edx. The other registers are callee-save.

Setup of stack for a function call:

pushl %ebp
movl %esp, %ebp

To clean up the above:

popl %ebp

A ret instruction effectively pops the return address into %eip.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*