Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Macro Review Stack Frames

Similar presentations


Presentation on theme: "Lecture 7 Macro Review Stack Frames"— Presentation transcript:

1 Lecture 7 Macro Review Stack Frames
CS 2130 Lecture 7 Macro Review Stack Frames

2 Questions?

3 Macros Revisited Write a macro to automatically check for return values from printf

4 Recall printf("some stuff %d %f", i, x);
Problem: We're ignoring the return value which could indicate a problem

5 We should write... if(printf("some stuff %d %f", i, x)<0) {
(void)fprintf(stderr,"Yikes!!!"); exit(EXIT_FAILURE); } Which, of course, is a pain in the neck!

6 First Approach OK? #define PRINTF( stuff ) if(printf stuff <0) \
{ \ (void)fprintf(stderr,"Yikes!!!"); \ exit(EXIT_FAILURE); \ } and we would use it like this: PRINTF( "some stuff %d %f", i, x ) OK?

7 Not exactly... #define PRINTF( stuff ) if(printf stuff <0) \ { \
(void)fprintf(stderr,"Yikes!!!"); \ exit(EXIT_FAILURE); \ } This is the fix: PRINTF( ( "some stuff %d %f", i, x ) )

8 Questions?

9 Stack Frames

10 Stack 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

11 Buzzwords 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.

12 Key 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.

13 Basic Idea Caller Pushing arguments on stack Push fp onto stack
MOV fp, sp JALR Rd, Rs (RdPC+4; PC  Rs) Callee Put return address on stack if necessary Code may move sp if necessary fp is fixed.

14 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp main sp 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("%d%d", &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

15 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); sp u: 15 Calling sequence for first call to gcd starts by evaluating and pushing argument values onto stack

16 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 sp v: 10

17 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 sp control link (fp) Frame pointer value is pushed onto stack providing a control link from new frame back to old

18 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp sp control link (fp)

19 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) sp return address Pushing return address onto stack completes stack frame and function execution may start by jumping to code

20 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) sp return address

21 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) sp return address

22 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) return address sp u: 10 Starting second call to gcd

23 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) return address u: 10 sp v: 5

24 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp sp control link (fp)

25 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) sp return address

26 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) sp return address

27 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) sp return address

28 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) return address sp u: 5 Starting third call to gcd

29 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) return address u: 5 sp v: 0

30 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 fp sp control link (fp)

31 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 fp control link (fp) sp return address

32 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 fp control link (fp) sp return address

33 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 fp control link (fp) sp return address

34 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 fp control link (fp) sp return address

35 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 fp control link (fp) sp return address

36 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &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 sp fp control link (fp)

37 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) return address u: 5 v: 0 sp control link (fp) We can move the SP and jump to the return address

38 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 fp control link (fp) sp return address Put the return address into a register

39 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 control link (fp) return address u: 10 v: 5 sp fp control link (fp)

40 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) return address u: 10 v: 5 sp control link (fp)

41 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) sp return address

42 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 fp control link (fp) sp return address Put the return address into a register

43 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 sp fp control link (fp)

44 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); u: 15 v: 10 sp control link (fp)

45 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp main sp 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y));

46 5 Euclid's Algorithm int x,y; int gcd(int u, int v) { if(v == 0)
Global/static area x: 15 y: 10 Euclid's Algorithm fp main sp 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("%d%d", &x, &y); printf ("%d\n",gcd(x,y)); 5

47 Questions?

48


Download ppt "Lecture 7 Macro Review Stack Frames"

Similar presentations


Ads by Google