CS 2130 - PowerPoint PPT Presentation

About This Presentation
Title:

CS 2130

Description:

printf('some stuff %d %f', i, x) ... x: 15. y: 10. main. fp. sp. return address. Starting out assuming that. values of 15 and 10 have. been read into x and y. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 49
Provided by: ble87
Category:
Tags:

less

Transcript and Presenter's Notes

Title: CS 2130


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)
  • (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
  • \
  • (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
  • \
  • (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?PC4 PC ? Rs)
  • Callee
  • Put return address on stack if necessary
  • Code may move sp if necessary
  • fp is fixed.

14
Euclid'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
15
Euclid'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
16
Euclid'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
17
Euclid'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
18
Euclid'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)
19
Euclid'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
20
Euclid'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
21
Euclid'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
22
Euclid'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
23
Euclid'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
24
Euclid'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)
25
Euclid'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
26
Euclid'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
27
Euclid'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
28
Euclid'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
29
Euclid'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
30
Euclid'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)
31
Euclid'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
32
Euclid'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
33
Euclid'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
34
Euclid'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
35
Euclid'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
36
Euclid'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)
37
Euclid'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
38
Euclid'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
39
Euclid'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)
40
Euclid'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)
41
Euclid'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
42
Euclid'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
43
Euclid'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)
44
Euclid'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)
45
Euclid'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))

46
Euclid'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
47
Questions?
48
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com