Download presentation
Presentation is loading. Please wait.
Published byDuane Tagge Modified over 9 years ago
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 Sixteen General Registers In Local Out CWP -> WIM ->
7
Sparc Register File Sixteen General Registers In Local Out In Local Out CWP -> WIM ->
8
Sparc Register File Sixteen General Registers In Local Out In Local Out In Local Out CWP -> WIM ->
9
Window Overflow Sixteen General Registers In Local Out In Local Out CWP -> WIM ->
10
Window Overflow In Local Out In Local Out CWP -> WIM -> In Local CWP -> WIM -> Save to Frame where this %sp points Out In Local Out
11
Window Overflow In Local CWP -> WIM ->CWP -> WIM -> Save to Frame In Local Out In Local Out
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
The frame pointer %i6 The stack pointer %o6 R31 R30 R29 R28 R27 R26 R25 R24 R23 R22 R21 R20 R19 R18 R17 R16 R15 R14 R13 R12 R11 R10 R9 R8 I registers L registers O registers
14
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.
15
<- i6 = fp I registers L registers sp =o6 -> O registers I registers L registers sp =o6 -> O registers Register Set Figure 7.1
16
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.
17
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
18
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);
19
function calls Put parameters they should be callfunction_name If function returns a value, look for return value where it should be
20
The call statement The call statement is equivalent to a jump long instruction callfun_name is equivalent setfun_name, %o0 jmpl%o0, %o7 The %pc at the call is saved in %o7 (= %i7 of the called function )
21
The ret Statement The ret statement is also a jmpl instruction ret restore is equivalent to jmpl%i7 + 8, %g0// why %i7 + 8 restore
22
functions.data data stuff.text.globalfun_name fun_name:save%sp, value, %sp get parameters // code for function … put answer ret restore
23
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
24
After the call (1) calladdem! Addr = %o7 nop! Addr = %o7 + 4.word3! Addr = %o7 + 8.word4! Addr = %o7 + 12 ! Return here Addr = %o7 + 16
25
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
26
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
27
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
28
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.
29
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
30
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. *********************************
31
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.
32
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); }
33
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
34
Example 2 /*************************************** * File: par_st.m * Purpose: To demonstrate * parameter passing using * the system stack. * Computes the same as par_reg.m ******************************************/
35
Exercise Repeat the previous exercise, but pass the parameters using the stack.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.