Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.

Similar presentations


Presentation on theme: "ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly."— Presentation transcript:

1 ITEC 352 Lecture 18 Functions in Assembly

2 Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly –Register based –Memory based Sethi / srl

3 Functions + Assembly Outline Functions

4 Functions + Assembly Registers Subroutine linkage with registers passes parameters in registers. High level language equivalent?

5 Functions + Assembly Memory Subroutine linkage with a data link area passes parameters in a separate area in memory. The address of the memory area is passed in a register ( %r5 here).

6 Functions + Assembly Back to functions Limitations of each approach –Registers –Data link area Recursion Possible solutions

7 Functions + Assembly Function calls void f() { printf(“enter f”); g(); printf(“exit f”); } void g() { printf(“enter g”); h(); printf(“exit g”); } void h() { printf(“enter h”); i(); printf(“exit h”); } void i() { printf(“enter i”); i(); printf(“exit i”); } void main() { f(); } Write out what is called? Does this remind you of any particular data structure?

8 Functions + Assembly Terms Function activation –When its code is being activated –How many times is f activated? Function deactivation –When a function’s code goes from being active to non-active

9 Functions + Assembly OS / Languages Keeps track of what is active what is not Scheduling algorithms –Multi-tasking Scope of variables –Where do they live, when can they be accessed?

10 Functions + Assembly Memory When a program needs to execute, the OS gives the program some memory. This memory is divided into (usually) atleast 3 parts: –Memory to store the generated assembly code of the program. Memory contains the instructions that make up the program. –Data segment: memory to store global variables. Sometimes, this same memory can be used to store dynamically allocated memory: i.e., we do not know at compilation time what memory is to be allocated here. (This is usually the memory we allocate using the “new” or “malloc” functions.) E.g., int x;//Let x be a variable whose value is given by user. get(x); // ask the user to enter the value for x. int y[] = new int[x] ; // Here we don’t know how much memory to allocate for y until the user supplies the value of x. –Control stack (also known as program stack) to store function activations as the program is executing. Here is where the local variables go. Hence, the lifetime of a local variable is limited to the lifetime of the function activation.

11 Functions + Assembly Activation record An activation record is the memory allocated for each function activation. It contains the following data: return address memory for each local variable. memory for parameter values passed from caller function memory addresses of dynamically allocated memory.

12 Functions + Assembly Step 1 Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Initially the stack is empty. Execution of this program starts with the function main. Hence, an activation record for main is created as shown on the stack. PROGRAM STACK Return address Activation record for main. Memory for z Stack pointer (%sp)

13 Functions + Assembly Step 2 Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Next: The function “f” is invoked. We have to create and then push the activation record of “f”. However, before we do that, we have to create space for the actual parameters that we want to pass to function f (here it is z, z) PROGRAM STACK Return address Parameters to be passed to f. Memory for z Stack pointer (%sp) 5 5 return address In ARC this is stored in %r15 (address of line 8)

14 Functions + Assembly Step 2B Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Next: This is a continuation from previous slide. You can see that we now have the complete activation record of function f. PROGRAM STACK Return address Activation record of function f. Memory for z Stack pointer (%sp) 5 5 return address(%r15) a (value 10) b (value 5)

15 Functions + Assembly Final step Consider a program: 1. int f( int x, int y) { 2. int a = 10; 3. int b = 5; 4. } 5. int main() { 6. int z = 5; 7. f(z, z) ; 8. } Next: After f finishes execution its activation record is no longer “live” -- it is popped out. PROGRAM STACK Return address Program stack after f has finished execution. You can see that the local variables a and b are no longer accessible, Memory for z Stack pointer (%sp)

16 Functions + Assembly Review More on how it works –Goal: understand how java  assembly works


Download ppt "ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly."

Similar presentations


Ads by Google