1 / 35

Functions

Functions. Functions and Parameters. History. A function call needs to save the registers in use The called function will use the registers The registers need to be restored when the called function is completed. This requires three things: Who saves the registers

fineen
Download Presentation

Functions

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. Functions Functions and Parameters

  2. History • A function call needs to save the registers in use • The called function will use the registers • The registers need to be restored when the called function is completed. • This requires three things: • Who saves the registers • Who restores the registers • Where are they saved

  3. Who Saves/Restores • Who saves • The calling function • The called function • Who restores • The calling function • The called function • Any combination of the above

  4. Where • A location in memory • In registers • Pointed to by a register • In the caller’s frame • In the called frame • On the stack • Register sets • Sparc Architecture uses the frame and register sets

  5. Sparc Register File • In the Sparc architecture there are a minimum of 128 registers and 8 global registers • These are grouped into 8 global registers and 24 mapped programmer registers • The save instruction changes the register mapping to a new set • The restore instruction restores the old set. • The CWP and WIM are two system registers pointing to the current window of registers and the last free register set.

  6. Sparc Register File In Local Out CWP -> Sixteen General Registers WIM ->

  7. Sparc Register File In Local Out In Local Out CWP -> Sixteen General Registers WIM ->

  8. Sparc Register File In Local Out In Local Out In Local Out CWP -> Sixteen General Registers WIM ->

  9. Window Overflow Sixteen General Registers In Local Out In Local Out CWP -> WIM ->

  10. Window Overflow Save to Frame where this %sp points WIM -> Out In Local Out In Local Out In Local Out CWP -> In Local CWP -> WIM ->

  11. Window Overflow In Local Out In Local Out WIM -> CWP -> Save to Frame WIM -> In Local CWP ->

  12. Function Registers • Each function gets its own registers • The global registers (one copy for all) • Its own copy of the other registers • The I registers • The L registers • The O registers

  13. A Function Call • A function call creates a new frame • A called function gets access to the %g registers • A called function’s I registers are the calling function’s O registers • The called function gets its own L registers and O registers. These are called a register set.

  14. Register Set Figure 7.1

  15. Save Instruction • save %sp, some_value, %sp • The old sp, %o6 becomes %i6, %fp in the new register set after the save • The save instruction is also an ADD instruction. It adds some_value to the current %sp, putting the result into the new %sp, leaving the old %sp unchanged.

  16. Restore • The restore instruction restores the register window set. • If underflow occurs (the opposite of overflow), the system restores the registers from the frame where the %sp points

  17. Functions vs Subroutines • A subroutine / procedure can be viewed as a function that returns void (C/C++/Java) procedure print_list(Listtype L); (PASCAL) void print_list( Listtype L); • A function can be viewed as a procedure that returns a value (ALGOL) integer procedure factorial(integer N);

  18. function calls • Put parameters they should be • call function_name • If function returns a value, look for return value where it should be

  19. The call statement The call statement is equivalent to a jump long instruction call fun_name is equivalent set fun_name, %o0 jmpl %o0, %o7 The %pc at the call is saved in %o7 (= %i7 of the called function )

  20. The ret Statement • The ret statement is also a jmpl instruction ret restore is equivalent to jmpl %i7 + 8, %g0 // why %i7 + 8 restore

  21. functions .data data stuff .text .global fun_name fun_name: save %sp, value, %sp get parameters // code for function … put answer ret restore

  22. Where, and How, are the Arguments • After the call statement • On the stack • In registers • In memory • By value • By reference (address) • By address of values • By address of addresses

  23. After the call (1) call addem ! Addr = %o7 nop ! Addr = %o7 + 4 .word 3 ! Addr = %o7 + 8 .word 4 ! Addr = %o7 + 12 ! Return here Addr = %o7 + 16

  24. After the call (2) .text .global addem addem: save %sp, -64, %sp ld [%i7 + 8], %i0 ld [%i7 + 12], %i1 add %i0, %i1, %i0 jmpl %i7 + 16, %g0 restore

  25. Remarks • Very efficient • Non-recursive • Cannot compute the parameters Modern computers will not allow a program to store into the code section • Cannot have a variable number of parameters

  26. Arguments on Stack • This technique is very flexible • Allows recursion • Number of parameters can vary for the same function • But requires memory access to load and store parameters’ values and results

  27. Sparc Solution • First six arguments are placed into the O registers. • These O registers become the called functions I registers. • No memory references are necessary for these parameters.

  28. But … • More than six arguments requires the use of the stack • Each stack argument occupies one word, even byte arguments • Byte arguments must be moved into word quantities before stored in the stack. • First argument is at %sp + 68 • The frame size must allow for all the parameters • Frame size = 16 + locals + parameters + 68

  29. Example /**************************** * File: par_reg.m * main reads two numbers x * and y, and calls the function * f(x,y,&result) to compute* x*y + 2 and return the value * in result.*********************************

  30. Exercise Write a program to read in two integers a and b, compute a*a + b*b, and print out the sum. The program should loop until two integers are not read. Use a separate functions for each part. Equivalent C code is next. Pass parameters using registers.

  31. Exercise (con’t) void print_prompt(); int read(int & a, int & b); void write(int a, int b); while(print_prompt(), read(a, b) != 2) { s = fun(a, b); write(a, b, s); }

  32. Stack Parameters • Parameters may be put on the stack • Which frame: caller or called? • Many machines it is the called • For the Sparc it is the caller • Therefore, the caller refers to them using the stack pointer • The called function uses the frame pointer

  33. Example 2 /*************************************** * File: par_st.m * Purpose: To demonstrate * parameter passing using * the system stack. * Computes the same as par_reg.m ******************************************/

  34. Exercise Repeat the previous exercise, but pass the parameters using the stack.

More Related