Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 14-2 Function Smaller, simpler, subcomponent.

Slides:



Advertisements
Similar presentations
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
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.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
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:
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
ISBN Chapter 10 Implementing Subprograms.
CS 536 Spring Code generation I Lecture 20.
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.
Run time vs. Compile time
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.
CS 201 Functions Debzani Deb.
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 &
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
ISBN Chapter 10 Implementing Subprograms.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
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.
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
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
Chapter 14 Functions.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Objective At the conclusion of this chapter you will be able to:
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.
Chapter 19 Data Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Data Structures A data structure.
ISBN Chapter 10 Implementing Subprograms.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
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.
Arrays and Strings in Assembly
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Section 5: Procedures & Stacks
Chapter 14 Functions.
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.
Run-Time Environments Chapter 7
Chapter 17 Recursion.
Implementing Subprograms
Chapter 12 Variables and Operators
Chapter 14 Functions.
Chapter 14 Functions.
More C Stack Frames / Pointer variables
Chapter 17 Recursion.
Understanding Program Address Space
Chapter 14 Functions.
Topic 3-a Calling Convention 1/10/2019.
Chapter 17 Recursion.
Runtime Environments What is in the memory?.
Chapter 14 Functions.
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 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,...

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example of High-Level Structure main() { SetupBoard(); /* place pieces on board */ DetermineSides(); /* choose black/white */ /* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet()); } Structure of program is evident, even without knowing implementation.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display 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 of all arguments 1. evaluate arguments 2, execute function 3. use return value in expression

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

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display 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. 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 Implementing Functions: Overview Activation record information about each function, including arguments and local variables 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 Stack pointer (R6) points to the beginning of a region of memory that stores local variables for the current function That region of memory is called an activation record 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 main Memory R6 main Memory R6 NoName Before callDuring callAfter call Note: Using R6 this way is incompatible with storing interrupt state on the stack.

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

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Activation Record Bookkeeping Return value always first word in activation record holds 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 address of previous activation record used to pop this activation record from stack

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Calling the Function b = NoName(a, 10); ; store a to 1st argument LDR R0, R6, #3 STR R0, R6, #8 ; store 10 to 2nd argument AND R0, R0, #0 ADD R0, R0, #10 STR R0, R6, #9 ; store R6 into dynamic link STR R6, R6, #7 ; move R6 to start of new record ADD R6, R6, #5 ; call subroutine JSR NoName ret val ret addr dyn link a b ret val ret addr dyn link a b w x y x3F00 13 x x4100 R6 Note: Caller needs to know number and type of arguments, doesn't know about local variables.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Starting the Callee Function ; store return address STR R7, R6, #1 ret val ret addr dyn link a b ret val ret addr dyn link a b w x y x3F00 13 x3100 x x4100 R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Ending the Callee Function return y; ; copy y into return value LDR R0, R6, #6 STR R0, R6, #0 ; load the return address LDR R7, R6, #1 ; load the dynamic link ; (pops the activation record) LDR R6, R6, #2 ; return control to caller RET ret val ret addr dyn link a b ret val ret addr dyn link a b w x y x3F x3100 x x4100 R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Resuming the Caller Function b = NoName(a,10); ; load return value LDR R0, R6, #5 ; store result into b STR R0, R6, #4 ret val ret addr dyn link a b ret val x3F x4100 R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example /* Function ToUpper: * If the argument is lower case, * return its upper case ASCII value */ char ToUpper(char inchar) { int outchar = inchar; if ('a' <= inchar && inchar <= 'z') outchar = inchar - ('a' - 'A'); return outchar; } Compile this function to LC-2 assembly language.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example: LC-2 Code for ToUpper ToUpper STR R7, R6, #1 ; save return addr LDR R0, R6, #3 ; load parameter (inchar) STR R0, R6, #4 ; initialize outchar LD R1, neg_a ; load -'a' ADD R1, R0, R1 ; inchar - 'a' BRn FALSE ; br if inchar 'z' LD R1, neg_upper ; load -('a' - 'A') ADD R0, R0, R1 ; inchar - ('a' - 'A') STR R0, R6, #4 ; store to outchar FALSE LDR R0, R6, #4 ; load outchar STR R0, R6, #0 ; store in result LDR R7, R6, #1 ; load return address LDR R6, R6, #2 ; load dynamic link RET