To sum function as an example
int sum(int a,int b)
{
return a+b;
}
Its assembly code
.text
.global sum
sum:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
addl 12(%ebp),%eax
leave
ret
Then the main function
int main(void)
{
int a;
int b=12;
int c=24;
a=sum(b,c);
return 0;
}
Assembler code for
.text
.global main
main:
pushl %ebp
movl %esp,%ebp
subl $12,%esp
movl $12,-8(%ebp)
movl $24,-12(%ebp)
pushl -12(%ebp)
pushl -8(%ebp)
call sum
addl $8,%esp
movl %eax,-4(%ebp)
movl $0,%eax
leave
ret
Local variables are used to apply subl $ 12,% esp on the application stack space to hold the 12 bytes a, b, c. a is -4 (% ebp), b is -8 (% ebp), and so on.
Register usage conventions
accordance with established practice, the register% eax,% edx,% ecx register is divided saved caller. When the procedure calls p q, q can override these registers, without disrupting any data desired p. On the other hand, the register% ebx,% esi,% edi caller is divided into a save register. This means q must be covered before these registers, save them first into the stack and restore them before returning, should these values may be required in the future for computing p.