COMP3221: Microprocessors and Embedded Systems Lecture 13: Functions II Lecturer: Hui Wu Session 2, 2005.

Slides:



Advertisements
Similar presentations
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Advertisements

We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
COMP3221: Microprocessors and Embedded Systems Final Exam Lecturer: Hui Wu Session 1, 2005.
COMP3221: Microprocessors and Embedded Systems--Lecture 7 1 COMP3221: Microprocessors and Embedded Systems Lecture 7: Arithmetic and logic Instructions.
COMP3221: Microprocessors and Embedded Systems--Lecture 7 1 COMP3221: Microprocessors and Embedded Systems Lecture 7: Arithmetic and logic Instructions.
COMP3221: Microprocessors and Embedded Systems Lecture 16: Interrupts II Lecturer: Hui Wu Session 2, 2005.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
CS 536 Spring Code generation I Lecture 20.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Semantics of Calls and Returns
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
COMP3221: Microprocessors and Embedded Systems
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
COMP3221: Microprocessors and Embedded Systems--Lecture 9 1 COMP3221: Microprocessors and Embedded Systems Lecture 9: Data Transfer Instructions
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CPSC 388 – Compiler Design and Construction Runtime Environments.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Lecture 19: Control Abstraction (Section )
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
“Toy” Model: Fibonacci The Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, known as Fibonacci.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Function Calls in MIPS To call a function: jal func
CSCI206 - Computer Organization & Programming
COMP2121: Microprocessors and Interfacing
Review: recursion Tree traversals
Introduction to Compilers Tim Teitelbaum
Functions and Procedures
COMP2121: Microprocessors and Interfacing
COMP2121: Microprocessors and Interfacing
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
MIPS Instructions.
COMP3221: Microprocessors and Embedded Systems
Stack Frames and Functions
Module 1-10: Recursion.
UNIT V Run Time Environments.
Miscellaneous Topics.
Runtime Environments What is in the memory?.
COMP3221: Microprocessors and Embedded Systems
Where is all the knowledge we lost with information? T. S. Eliot
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

COMP3221: Microprocessors and Embedded Systems Lecture 13: Functions II Lecturer: Hui Wu Session 2, 2005

COMP3221/9221: Microprocessors and Embedded Systems 2 Overview Recursive Functions Computing the Stack Size for function calls

COMP3221/9221: Microprocessors and Embedded Systems 3 Recursive Functions A recursive function is both a caller and a callee of itself. Need to check both its source caller (that is not itself) and itself for register conflicts. Can be hard to compute the maximum stack space needed for recursive function calls.  Need to know how many times the function is nested (the depth of the calls).

4 An Example of Recursive Function Calls int sum(int n); int main(void) { int n=100; sum(n); return 0; } void sum(int n) { if (n<=0) return 0; else return (n+ sum(n-1)); } main() is the caller of sum() sum() is the caller and callee of itself

5 Call Trees A call tree is a weighted directed tree G = (V, E, W) where  V={v 1, v 2, …, v n } is a set of nodes each of which denotes an execution of a function;  E={v i  v j : v i calls v j } is a set of directed edges each of which denotes the caller-callee relationship, and  W={w i (i=1, 2, …, n): w i is the frame size of v i } is a set of stack frame sizes. The maximum size of stack space needed for the function calls can be derived from the call tree.

6 An Example of Call Trees int main(void) { … func1(); … func2(); } void func1() { … func3(); … } void func2() { … func4(); … func5(); … }

7 An Example of Call Trees (Cont.) main() func2() func1() func3()func4()func5() The number in red beside a function is its frame size in bytes.

8 Computing the Maximum Stack Size for Function Calls Step 1: Draw the call tree. Step 2: Find the longest weighted path in the call tree. The total weight of the longest weighted path is the maximum stack size needed for the function calls.

9 main() func2() func1() func3()func4()func5() The longest path is main()  func1()  func3() with the total weight of 110. So the maximum stack space needed for this program is 110 bytes. An Example

COMP3221/9221: Microprocessors and Embedded Systems 10 Fibonacci Rabbits Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. How many pairs will there be in one year? Fibonacci’s Puzzle Italian, mathematician Leonardo of Pisa (also known as Fibonacci) 1202.

COMP3221/9221: Microprocessors and Embedded Systems 11 Fibonacci Rabbits (Cont.) The number of pairs of rabbits in the field at the start of each month is 1, 1, 2, 3, 5, 8, 13, 21, 34,.... In general, the number of pairs of rabbits in the field at the start of month n, denoted by F(n), is recursively defined as follows. F(n) = F(n-1) + F(n-2) Where F(0) = F(1) = 1. F(n) (n=1, 2, …, ) are called Fibonacci numbers.

COMP3221/9221: Microprocessors and Embedded Systems 12 C Solution of Fibonacci Numbers int month=4; int main(void) { fib(month); } int fib(int n) { if(n == 0) return 1; if(n == 1) return 1; return (fib(n - 1) + fib(n - 2)); }

COMP3221/9221: Microprocessors and Embedded Systems 13 AVR Assembler Solution Return address r16 r17 r28 r29 n Empty Frame structure for fib() Y X X–2 X–3 X–4 X–5 X–6 X–8 r16, r17, r28 and r29 are conflict registers. An integer is 2 bytes long in WINAVR

14 Assembly Code for main().cseg month:.dw 4 main: ; Prologue ldi r28, low(RAMEND) ldi r29, high(RAMEND) out SPH, r29 ; Initialise the stack pointer SP to point to out SPL, r28 ; the highest SRAM address ; End of prologue ldi r30, low(month<<1) ; Let Z point to month ldi r31, high(month<<1) lpm r24, z+ ; Actual parameter 4 is stored in r25:r24 lpm r25, z rcall fib ; Call fib(4) ; Epilogue: no return loopforever: rjmp loopforever

15 Assembly Code for fib() fib: push r16 ; Prologue push r17 ; Save r16 and r17 on the stack push r28 ; Save Y on the stack push r29 in r28, SPL in r29, SPH sbiw r29:r28, 2 ; Let Y point to the bottom of the stack frame out SPH, r29 ; Update SP so that it points to out SPL, r28 ; the new stack top std Y+1, r24 ; Pass the actual parameter to the formal parameter std Y+2, r25 cpi r24, 0 ; Compare n with 0 clr r0 cpc r25, r0 brne L3 ; If n!=0, go to L3 ldi r24, 1 ; n==0 ldi r25, 0 ; Return 1 rjmp L2 ; Jump to the epilogue

16 Assembly Code for fib() (Cont.) L3: cpi r24, 1 ; Compare n with 1 clr r0 cpc r25, r0 brne L4 ; If n!=1 go to L4 ldi r24, 1 ; n==1 ldi r25, 0 ; Return 1 rjmp L2 ; Jump to the epilogue L4: ldd r24, Y+1 ; n>=2 ldd r25, Y+2 ; Load the actual parameter n sbiw r24, 1 ; Pass n-1 to the callee rcall fib ; call fib(n-1) mov r16, r24 ; Store the return value in r17:r16 mov r17, r25 ldd r24, Y+1 ; Load the actual parameter n ldd r25, Y+2 sbiw r24, 2 ; Pass n-2 to the callee rcall fib ; call fib(n-2) add r24, r16 ; r25:r25=fib(n-1)+fib(n-2) adc r25, r17

17 Assembly Code for fib() (Cont.) L2: ; Epilogue adiw r29:r28, 2 ; Deallocate the stack frame for fib() out SPH, r29 ; Restore SP out SPL, r28 pop r29 ; Restore Y pop r28 pop r17 ; Restore r17 and r16 pop r16 ret

18 Computing the Maximum Stack Size Step 1: Draw the call tree. main() fib(4) fib(3)fib(2) fib(1) fib(0) fib(1)fib(0) The call tree for n=4

19 Computing the Maximum Stack Size (Cont.) Step 1: Find the longest weighted path. main() fib(4) fib(3)fib(2) fib(1) fib(0) fib(1)fib(0) The longest weighted path is main()  fib(4)  fib(3)  fib(2)  fib(1) with the total weight of 32. So a stack space of 32 bytes is needed for this program.