490 likes | 524 Views
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.
E N D
CS 2130 Lecture 7 Macro Review Stack Frames
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... 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!
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?
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 ) )
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
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.
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.
Caller Pushing arguments on stack Push fp onto stack MOV fp, sp JALR Rd, Rs (RdPC+4; PC Rs) Callee Put return address on stack if necessary Code may move sp if necessary fp is fixed. Basic Idea
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
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
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
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
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)
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
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
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
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
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
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)
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
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
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
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
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
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)
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
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
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
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
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
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)
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
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
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)
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)
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
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
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)
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)
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
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