Professional Documents
Culture Documents
Topic V0B: Procedure Calls, Stack, and Memory Layout
Topic V0B: Procedure Calls, Stack, and Memory Layout
Subroutine
Subprogram
Where?
Into a stack
What Goes Into the
Stack Stack
5: …
6: r = bar(a, b); 1: int bar(int x, int y)
safe to
7: t = 2 * r; 2: { overwrite return
8: … 3: int i, p, t; address into ra
9: r = bar(b, a); 4: t = 1;
10: c = c + 4 * r; 5: for(i = 0 ; i <x ; i++){
11: … 6: t = baz(x);
12: } 7: p = p * t; 1: int baz(int d)
return to 8: } 2: {
correct address 9: return p; 3: int z;
10: } 4: z = 100 – d;
restore ra 5: return z;
return to
from stack
correct address
6: }
Saving Registers Into the
Stack Memory execution it is time to return to the caller
Now that the procedure has completed its
↑ High Address
0
Stack: automatic storage
Memory
Layout
Text: program code (instructions) sp 7FFF FFFChex
0
Stack: automatic storage
Recap