160 likes | 270 Views
ITEC 352. Lecture 18 Functions in Assembly. Review. Questions? Project due on Friday Exam Average 76 Methods for functions in assembly Register based Memory based Sethi / srl. Outline. Functions. Registers. • Subroutine linkage with registers passes parameters in registers.
E N D
ITEC 352 Lecture 18 Functions in Assembly
Review • Questions? • Project due on Friday • Exam • Average 76 • Methods for functions in assembly • Register based • Memory based • Sethi / srl
Outline • Functions
Registers • • Subroutine linkage with registers passes parameters in registers. High level language equivalent?
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).
Back to functions • Limitations of each approach • Registers • Data link area • Recursion • Possible solutions
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?
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
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?
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.
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.
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. Step 1 • Consider a program: • int f( int x, int y) { • int a = 10; • int b = 5; • } • int main() { • int z = 5; • f(z, z) ; • } Stack pointer (%sp) Activation record for main. Memory for z Return address PROGRAM STACK
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) Step 2 • Consider a program: • int f( int x, int y) { • int a = 10; • int b = 5; • } • int main() { • int z = 5; • f(z, z) ; • } Stack pointer (%sp) return address In ARC this is stored in %r15(address of line 8) 5 Parameters to be passed to f. 5 Memory for z Return address PROGRAM STACK
Step 2B • Consider a program: • int f( int x, int y) { • int a = 10; • int b = 5; • } • int main() { • int z = 5; • f(z, z) ; • } Stack pointer (%sp) b (value 5) a (value 10) return address(%r15) 5 Activation record of function f. 5 Memory for z Next: This is a continuation from previous slide. You can see that we now have the complete activation record of function f. Return address PROGRAM STACK
Final step • Consider a program: • int f( int x, int y) { • int a = 10; • int b = 5; • } • int main() { • int z = 5; • f(z, z) ; • } Stack pointer (%sp) Program stack after f has finished execution. You can see that the local variables a and b are no longer accessible, Memory for z Next: After f finishes execution its activation record is no longer “live” -- it is popped out. Return address PROGRAM STACK
Review • More on how it works • Goal: understand how java assembly works