Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Functions.

Similar presentations


Presentation on theme: "Chapter 14 Functions."— Presentation transcript:

1 Chapter 14 Functions

2 Agenda Functions Activation record Recursion

3 Function Smaller, simpler, subcomponent of program
Provides abstraction hide low-level details give high-level structure to program, easier to understand overall program flow enables separable, independent development C functions zero or multiple arguments passed in single result returned (optional) return value is always a particular type In other languages, called procedures, subroutines, ...

4 Functions in C Declaration (also called prototype)
int Factorial(int n); Function call -- used in expression a = x + Factorial(f + g); type of return value name of function types and all arguments 1. evaluate arguments 2, execute function 3. use return value in expression

5 gives control back to calling function and returns value
Function Definition State type, name, types of arguments must match function declaration give name to each argument (doesn't have to match declaration) int Factorial(int n) { int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result; } gives control back to calling function and returns value

6 Why Declaration? Since function definition also includes return and argument types, why is declaration needed? Use might be seen before definition. Compiler needs to know return and arg types and number of arguments.

7 Example declaration function call (invocation) definition
double ValueInDollars(double amount, double rate); main() { dollars = ValueInDollars(francs, DOLLARS_PER_FRANC); printf("%f francs equals %f dollars.\n", francs, dollars); ... } double ValueInDollars(double amount, double rate) { return amount * rate; declaration function call (invocation) definition

8 Agenda Functions Activation record Recursion

9 Implementing Functions: Overview
Activation record information about each function, including arguments, local variables, return address stored on run-time stack Calling function Called function copy values into arguments call function set up environment execute code put result in activation record pop activation record from stack return get result from stack

10 Run-Time Stack Recall that local variables are stored in a stack frame
A stack frame is in an activation record on the run-time stack When a new function is called, its activation record is pushed on the stack; when it returns, its activation record is popped off of the stack. instructions global data run-time stack 0x0000 0xFFFF PC R4 R6 R5

11 Example Run-Time Stack
int main() { int k; int m; k = Watt(m); } int Watt(int a) { int w, s, t; return w; }

12 Run-Time Stack Before call During call After call Memory Memory Memory
Watt R6 R6 R5 R5 main main main Before call During call After call

13 Activation Record int NoName(int a, int b) { int w, x, y; return y; } y x w dynamic link return address return value a b locals R5 bookkeeping args Name Type Offset Scope a b w x y int 4 5 -1 -2 NoName

14 Activation Record Bookkeeping
Return value space for value returned by function allocated even if function does not return a value Return address save pointer to next instruction in calling function convenient location to store R7 in case another function (JSR) is called Dynamic link caller’s frame pointer used to pop this activation record from stack

15 Example Function Call int Volta(int q, int r) { int k; int m; return k; } int Watt(int a) { int w; w = Volta(w,10); return w; }

16 Calling the Function w = Volta(w, 10);
dyn link ret addr ret val a w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta R6 R5 xFD00 Note: Caller needs to know number and type of arguments, doesn't know about local variables.

17 Calling the Function w = Volta(w, 10);
25 r w dyn link ret addr ret val a w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta R6 R5 xFD00 Note: Caller needs to know number and type of arguments, doesn't know about local variables.

18 Calling the Function w = Volta(w, 10); 25 10 q r w dyn link ret addr
ret val a w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta R6 R5 xFD00 Note: Caller needs to know number and type of arguments, doesn't know about local variables.

19 Calling the Function w = Volta(w, 10); 25 10 q r w dyn link ret addr
ret val a w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta R6 R5 xFD00 Note: Caller needs to know number and type of arguments, doesn't know about local variables.

20 JSR Instruction Jumps to a location (like a branch but unconditional), and saves current PC (addr of next instruction) in R7. saving the return address is called “linking” target address is PC-relative (PC + Sext(IR[10:0])) bit 11 specifies addressing mode if =1, PC-relative: target address = PC + Sext(IR[10:0]) if =0, register: target address = contents of register IR[8:6] 20

21 Starting the Callee Function
25 10 ret val q r w dyn link ret addr a ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 R6 R5 xFD00

22 Starting the Callee Function
x3100 25 10 ret addr ret val q r w dyn link a ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 R6 R5 xFD00

23 Starting the Callee Function
xFCFB x3100 25 10 dyn link ret addr ret val q r w a ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 R6 R5 xFD00

24 Starting the Callee Function
xFCFB x3100 25 10 dyn link ret addr ret val q r w a ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 R5 R6 xFD00

25 Starting the Callee Function
xFCFB x3100 25 10 m k dyn link ret addr ret val q r w a ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (caller’s frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 R6 R5 xFD00

26 Ending the Callee Function
-43 217 xFCFB x3100 25 10 m k dyn link ret addr ret val q r w a return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET R6 R5 xFD00

27 Ending the Callee Function
-43 217 xFCFB x3100 25 10 m k dyn link ret addr ret val q r w a return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET R5 R6 xFD00

28 Ending the Callee Function
-43 217 xFCFB x3100 25 10 m k dyn link ret addr ret val q r w a return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET R6 R5 xFD00

29 Ending the Callee Function
-43 217 xFCFB x3100 25 10 m k dyn link ret addr ret val q r w a return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET R6 R5 xFD00

30 Ending the Callee Function
-43 217 xFCFB x3100 25 10 m k dyn link ret addr ret val q r w a return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET R6 R5 xFD00

31 Resuming the Caller Function
217 25 10 ret val q r w dyn link ret addr a w = Volta(w,10); JSR Volta ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 R6 R5 xFD00

32 Resuming the Caller Function
217 25 10 ret val q r w dyn link ret addr a w = Volta(w,10); JSR Volta ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 R6 R5 xFD00

33 Resuming the Caller Function
217 25 10 ret val q r w dyn link ret addr a w = Volta(w,10); JSR Volta ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 R6 R5 xFD00

34 Resuming the Caller Function
217 25 10 ret val q r w dyn link ret addr a w = Volta(w,10); JSR Volta ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 R6 R5 xFD00

35 Summary of LC-3 Function Call Implementation
Caller pushes arguments (last to first). Caller invokes subroutine (JSR). Callee allocates return value, pushes R7 and R5. Callee allocates space for local variables. Callee executes function code. Callee stores result into return value slot. Callee pops local vars, pops R5, pops R7. Callee returns (JMP R7). Caller loads return value and pops arguments. Caller resumes computation…

36 Agenda Functions Activation record Recursion

37 Chapter 17 Recursion

38 What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces of data. In some cases, using recursion can make program easy to understand

39 Factorial: C Code f(n) = n * f(n-1) int factorial(int n){ if (n==1)
return 1; else return n*factorial(n-1); }

40 Activation Records fact(3) Whenever factorial is invoked, a new activation record is pushed onto the stack. main calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) R6 main 3 3*fact(2) R6 main 3 3*fact(2) 2 2*fact(1) R6 1 1 2*fact(1) 2 3*fact(2) 3 main

41 Activation Records (cont.)
factorial(1) returns factorial(2) returns factorial(3) returns R6 main 3 3*fact(2) 2 2*1 R6 main 3 3*2 R6 main


Download ppt "Chapter 14 Functions."

Similar presentations


Ads by Google