Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 14-2 Declarations (aka prototype) int.

Slides:



Advertisements
Similar presentations
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
Advertisements

Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
ISBN Chapter 10 Implementing Subprograms.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
1 Introduction to “C” Control Loops and Functions Patt and Patel Ch. 13 & 14.
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Semantics of Calls and Returns
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
Functions & Activation Records Recursion
Runtime Environments Compiler Construction Chapter 7.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 10 The Stack Stack data structure Activation records and function invocation.
ISBN Chapter 10 Implementing Subprograms.
CSC 8505 Compiler Construction Runtime Environments.
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
ISBN Chapter 10 Implementing Subprograms.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Section 5: Procedures & Stacks
Chapter 14 Functions.
Computer Architecture & Operations I
Storage Allocation Mechanisms
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Implementing Subprograms
Implementing Subprograms
Chapter 14 Functions.
Chapter 14 Functions.
More C Stack Frames / Pointer variables
Chapter 17 Recursion.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Chapter 14 Functions.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Chapter 14 Functions.
Topic 2b ISA Support for High-Level Languages
Chapter 17 Recursion.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Chapter 14 Functions

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int Factorial(int n); 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. Definition might be in a different file, written by a different programmer. include a "header" file with function declarations only compile separately, link together to make executable

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example 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

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Header Files – format 1.All function prototypes that you want to export 2.“extern” declarations for all variables you want to export 3.preprocessor directives to make sure prototypes are not multiply defined (header not included twice)

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Header Files – example header code.h #ifndef CODE_H #define CODE_H int myfunction(int arg1, char *arg2); extern int globalInt; extern char *globalPtr; #endif function prototype global variable declaration preprocessor directives

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Calls 1.Call function 2.Allocate memory for local variables 3.Execute function code 4.Place return value in specific location 5.Deallocate memory for local variables 6.Return to caller How did we learn this in LC-3?

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Simple Function - RecursiveSum int RecursiveSum(int i) { if (i <= 0) return 0; else return (i + (RecursiveSum(i-1)); }

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display RecursiveSum Implementation ; RecursiveSum: Recursively sum i to 0 RecursiveSum ; (int i in R0, return val in R0) STR3, FCR3; save registers STR7, FCR7 ADDR3, R0, #0; if (R3 = i) BRpELSE ; if (i <= 0) ANDR0, R0, #0; R0 = 0 to return BRnzp RTRN ; return 0 ELSEADDR0, R0, #-1; i = i - 1 JSRRecursiveSum; RecursiveSum(i-1) ADDR0, R3, R0; R0 = i + RecursiveSum(i-1) RTRNLDR3, FCR3; restore registers LDR7, FCR7 RET; and return FCR3.BLKW1 FCR7.BLKW 1

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display RecursiveSum – Memory storage ; RecursiveSum: Recursively sum i to 0 RecursiveSum STR3, FCR3; save registers STR7, FCR7; save return address ADDR3, R0, #0; if (R3 = i) BRpELSE ; if (i <= 0) ANDR0, R0, #0; R0 = 0 to return BRnzp RTRN ; return 0 ELSEADDR0, R0, #-1; i = i - 1 JSRRecursiveSum; RecursiveSum(i-1) ADDR0, R3, R0; R0 = i + RecursiveSum(i-1) RTRNLDR3, FCR3; restore registers LDR7, FCR7; restore return address RET; and return FCR3.BLKW1 FCR7.BLKW1 Memory allocated with function

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Step through function RecursiveSum(2) RecursiveSum(2) FCR3: R3: RecursiveSum(1) RecursiveSum(0) R0: OldR What went wrong? Why 2 rather than 3? 011

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Need separate memory for each invocation When you call a function, you can not overwrite information from previous call to same function Need a stack!

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Activation record Stores all information for a function invocation  arguments  local variables  preserved registers stored on run-time stack Calling function push new activation record copy values into arguments call function get result from stack Called function execute code put result in activation record pop activation record from stack return

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Run-Time Stack Recall that local variables are stored on the run-time stack in an activation record Frame pointer (R5) points to the beginning of a region of activation record that stores local variables for the current function Stack pointer (R6) points to the top of the 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.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Run-Time Stack main Memory R6 Watt Memory R6 main Memory main Before callDuring callAfter call R5 R6 R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Activation Record (details vary by language) int NoName(int a, int b) { int w, x, y;... return y; } NameTypeOffsetScope abwxyabwxy int NoName y x w dynamic link return address return value a b bookkeeping locals args R5 R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display 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

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display 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; }

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Calling the Function 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 q r w dyn link ret addr ret val a xFD00 new R6 Note: Caller needs to know number and type of arguments, doesn't know about local variables. R5 R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Starting the Callee Function ; 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 m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x xFD00 new R6 new R5 R6 R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Ending the Callee Function 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 m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x xFD00 R6 R5 new R6 new R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Resuming the Caller Function 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 ret val q r w dyn link ret addr ret val a xFD00 R6 R5 new R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Summary of LC-3 Function Call Implementation 1.Caller pushes arguments (last to first). 2.Caller invokes subroutine (JSR). 3.Callee allocates return value, pushes R7 and R5. 4.Callee allocates space for local variables. 5.Callee executes function code. 6.Callee stores result into return value slot. 7.Callee pops local vars, pops R5, pops R7. 8.Callee returns (JMP R7). 9.Caller loads return value and pops arguments. 10.Caller resumes computation…