CS485G Spring 2015 25
2. Example
1 int add3(int x) {
2 int localx = x;
3 incrk(&localx, 3);
4 return localx;
5 }
6 void incrk(int
*
ip, int k) {
7
*
ip += k;
8 }
add3:
pushl %ebp # save old ebp
movl %esp, %ebp # new ebp
subl $24, %esp # allocate 24 bytes (6 "chunks")
movl 8(%ebp), %eax # a = x
movl %eax, -4(%ebp) # localx = x
movl $3, 4(%esp) # actual2 = 3
leal -4(%ebp), %eax # a = &localx
movl %eax, (%esp) # actual1 = &localx
call incrk # a = incrk(actual1, actual2)
movl -4(%ebp), %eax # a = localx
addl $24, %esp # deallocate 24 bytes
popl %ebp # restore ebp
ret # return
3. right before the call to incrk(), the stack has these “chunks”:
x at 8(%ebp)
return address to add3’s caller
old ebp at (%ebp)
localx at -4(%ebp)
unused
unused
unused
actual 2 at 4(esp)
actual 1 at (esp)
Komentarze do niniejszej Instrukcji