11 - Procedure Call 1 Outline AnExample Recursive...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Procedure Call
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline An Example Recursive Suggested reading Chap 3.7
Background image of page 2
3 Example 1 int swap_add(int *xp, int *yp) 2 { 3 int x = *xp; 4 int y = *yp; 5 6 *xp = y; 7 *yp = x; 8 return x + y; 9 } 10
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Example 11 int caller() 12 { 13 int arg1 = 534; 14 int arg2 = 1057; 15 int sum = swap_add(&arg1, &arg2); 16 int diff = arg1 - arg2; 17 18 return sum * diff; 19 }
Background image of page 4
5 Example 0 Saved %ebp -4 arg2(1057) -8 arg1(534) -12 Stack frame for caller Before int sum = swap_add(&arg1, &arg2); %ebp %esp
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Parameter Passing 0 Saved %ebp -4 arg2(1057) -8 arg1(534) -12 Stack frame for caller 1 leal -4(%ebp),%eax 2 pushl %eax %ebp %esp
Background image of page 6
7 Parameter Passing 0 Saved %ebp -4 arg2(1057) -8 arg1(534) -12 &arg2 -16 Stack frame for caller 1 leal -4(%ebp),%eax 2 pushl %eax 3 leal -8(%ebp),%eax 4 pushl %eax %ebp %esp
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Call Instruction 0 Saved %ebp -4 arg2(1057) -8 arg1(534) -12 &arg2 -16 &arg1 -20 Return Addr Stack frame for caller 1 leal -4(%ebp),%eax 2 pushl %eax 3 leal -8(%ebp),%eax 4 pushl %eax 5 call swap_add Call the swap_add function %ebp %esp
Background image of page 8
9 Setup code in swap_add 0 Saved %ebp -4 arg2(1057) -8 arg1(534) -12 yp(&arg2) -16 xp(&arg1) -20 Return Addr -24 Saved %ebp Stack frame for caller swap_add: 1 pushl %ebp Save old %ebp %ebp %esp
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Setup code in swap_add 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 8 4 Return Addr 0 Saved %ebp Stack frame for caller swap_add: 1 pushl %ebp Save old %ebp 2 movl %esp,%ebp Set %ebp as frame pointer %ebp %esp Stack frame for swap_add
Background image of page 10
11 Body code in swap_add 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 8 4 Return Addr 0 Saved %ebp -4 Saved %ebx Stack frame for caller %esp %ebp Stack frame for swap_add swap_add: 1 pushl %ebp Save old %ebp 2 movl %esp,%ebp Set %ebp as frame pointer 3 pushl %ebx Save %ebx
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Body code in swap_add 24 Saved %ebp 20 arg2(1057) 16 arg1(534) 12 8 4 Return Addr 0 Saved %ebp -4 Saved %ebx Stack frame for caller 5 movl 8(%ebp),%edx Get xp %esp %ebp Stack frame for swap_add edx
Background image of page 12
13 Body code in swap_add 24 Saved %ebp
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 43

11 - Procedure Call 1 Outline AnExample Recursive...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online