Title: CS 2130
1CS 2130
- Lecture 7
- Macro Review
- Stack Frames
2Questions?
3Macros Revisited
- Write a macro to automatically check for return
values from printf
4Recall
- printf("some stuff d f", i, x)
- Problem We're ignoring the return value which
could indicate a problem
5We should write...
- if(printf("some stuff d f", i, x)
-
- (void)fprintf(stderr,"Yikes!!!")
- exit(EXIT_FAILURE)
-
- Which, of course, is a pain in the neck!
6First Approach
- define PRINTF( stuff ) if(printf stuff
- \
- (void)fprintf(stderr,"Yikes!!!") \
- exit(EXIT_FAILURE) \
-
- and we would use it like this
- PRINTF( "some stuff d f", i, x )
- OK?
7Not exactly...
- define PRINTF( stuff ) if(printf stuff
- \
- (void)fprintf(stderr,"Yikes!!!") \
- exit(EXIT_FAILURE) \
-
- This is the fix
- PRINTF( ( "some stuff d f", i, x ) )
-
8Questions?
9Stack Frames
10Stack Frames
- A language supporting subroutines must provide a
place to store the data values representing the
variables of the subroutine as well as an address
to return to once execution of the subroutine is
complete. - Languages that don't support recursion can simply
have a static "activation record" for each
subroutine. - Languages that support recursion must have
allocate storage for each call to the subroutine - This is normally implemented using a stack which
may be known as the activation stack
11Buzzwords
- Stack pointer a variable usually maintained in a
dedicated register which points to the top of the
stack. Note In reality stacks typically grow
down from high memory. - Activation record or stack frame The area of the
stack which is storing the activation information
(parameters, local variables and return address,
etc.) for a given module or function. - Frame pointer a variable also maintained in a
dedicated register which indicates a known point
in the stack frame from whence the location of
parameters and variables may be located.
12Key concept
- Converting a C program into assembly language
requires that a certain known sequence of
operations or at least a certain ordering of data
will occur in order to call a function. - Thus when the function code is compiled it will
know for certain exactly where to find it's
parameters, etc.
13Basic Idea
- Caller
- Pushing arguments on stack
- Push fp onto stack
- MOV fp, sp
- JALR Rd, Rs
- (Rd?PC4 PC ? Rs)
- Callee
- Put return address on stack if necessary
- Code may move sp if necessary
- fp is fixed.
14Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
Starting out assuming that values of 15 and 10
have been read into x and y. The stack frame for
main is on the stack. The frame pointer (fp) is
pointing at a known place in the frame while the
stack pointer (sp) indicates the top of the stack
15Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
Calling sequence for first call to gcd starts by
evaluating and pushing argument values onto stack
16Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
17Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
Frame pointer value is pushed onto stack
providing a control link from new frame back to
old
18Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
19Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
Pushing return address onto stack completes stack
frame and function execution may start by
jumping to code
20Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
21Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
22Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
Starting second call to gcd
23Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
24Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
25Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
26Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
27Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
28Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
Starting third call to gcd
29Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
30Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
31Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
return address
32Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
return address
33Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
return address
34Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
return address
35Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
Put the return address into a register
control link (fp)
return address
u 5
v 0
control link (fp)
return address
36Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
37Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
u 5
v 0
control link (fp)
We can move the SP and jump to the return address
38Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
return address
Put the return address into a register
39Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
40Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
u 10
v 5
control link (fp)
41Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
42Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
return address
Put the return address into a register
43Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
44Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
u 15
v 10
control link (fp)
45Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
46Euclid's Algorithm
Global/static area x 15 y 10
main
return address
- int x,y
- int gcd(int u, int v)
- if(v 0)
- return u
- else
- return gcd(v,u v)
-
- int main()
- scanf("dd", x, y)
- printf
- ("d\n",gcd(x,y))
5
47Questions?
48(No Transcript)