1 / 16

ITEC 352

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.

talon
Download Presentation

ITEC 352

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. ITEC 352 Lecture 18 Functions in Assembly

  2. Review • Questions? • Project due on Friday • Exam • Average 76 • Methods for functions in assembly • Register based • Memory based • Sethi / srl

  3. Outline • Functions

  4. Registers • • Subroutine linkage with registers passes parameters in registers. High level language equivalent?

  5. 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. Back to functions • Limitations of each approach • Registers • Data link area • Recursion • Possible solutions

  7. 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. 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. 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. 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. 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. 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

  13. 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

  14. 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

  15. 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

  16. Review • More on how it works • Goal: understand how java  assembly works

More Related