1 / 48

CS 2130

CS 2130. Lecture 7 Macro Review Stack Frames. Questions?. Macros Revisited. Write a macro to automatically check for return values from printf. Recall. printf("some stuff %d %f", i, x); Problem: We're ignoring the return value which could indicate a problem. We should write.

rbush
Download Presentation

CS 2130

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 #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. 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. Basic Idea

  14. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 Calling sequence for first call to gcd starts by evaluating and pushing argument values onto stack

  16. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10

  17. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) Frame pointer value is pushed onto stack providing a control link from new frame back to old

  18. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp)

  19. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  20. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address

  21. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address

  22. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 Starting second call to gcd

  23. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5

  24. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp)

  25. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp) return address

  26. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp) return address

  27. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp) return address

  28. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  29. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp) return address u: 5 v: 0

  30. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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)

  31. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  32. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  33. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  34. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  35. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  36. 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)); } fp sp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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)

  37. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  38. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 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

  39. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp)

  40. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address u: 10 v: 5 control link (fp)

  41. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address

  42. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp) return address Put the return address into a register

  43. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp)

  44. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address u: 15 v: 10 control link (fp)

  45. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address

  46. 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 fp Global/static area x: 15 y: 10 Euclid's Algorithm main return address 5

  47. Questions?

More Related