Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for...

18
Chapter 14 Functions

Transcript of Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for...

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

Chapter 14Functions

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

14-2

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

FunctionSmaller, 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, ...

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

14-3

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example of High-Level Structuremain(){ SetupBoard(); /* place pieces on board */

DetermineSides(); /* choose black/white */

/* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet());}

Structure of programis evident, even withoutknowing implementation.

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

14-4

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Functions in CDeclaration (also called prototype)

int Factorial(int n);

Function call -- used in expression

a = x + Factorial(f + g);

type ofreturn value

name offunction

types of allarguments

1. evaluate arguments

2, execute function

3. use return value in expression

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

14-5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Function DefinitionState 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 tocalling function and

returns value

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

14-6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Why Declaration?Since function definition also includesreturn 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

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

14-7

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Exampledouble 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

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

14-8

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Implementing Functions: OverviewActivation record

• information about each function,including arguments and local variables

• stored on run-time stack

Calling function

push new activation recordcopy values into argumentscall function

get result from stack

Called function

execute codeput result in activation recordpop activation record from stackreturn

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

14-9

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Run-Time StackRecall that local variables are storedon the run-time stack

Stack pointer (R6) points to the beginning of aregion of memory that stores local variables forthe 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.

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

14-10

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Run-Time Stack

main

Memory

R6main

Memory

R6

main

Memory

R6

NoName

Before call During call After call

Note: Using R6 this way is incompatible withstoring interrupt state on the stack.

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

14-11

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Activation Recordint NoName(int a, int b){ int w, x, y; . . . return y;}

Name Type Offset Scope

abwxy

intintintintint

34567

NoNameNoNameNoNameNoNameNoName

return valuereturn addressdynamic link

abwxy

bookkeeping

locals

args

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

14-12

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Activation Record BookkeepingReturn 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

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

14-13

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Calling the Functionb = NoName(a, 10);; store a to 1st argumentLDR R0, R6, #3STR R0, R6, #8; store 10 to 2nd argumentAND R0, R0, #0ADD R0, R0, #10STR R0, R6, #9; store R6 into dynamic linkSTR R6, R6, #7; move R6 to start of new recordADD R6, R6, #5; call subroutineJSR NoName

ret valret addrdyn linkabret valret addrdyn linkabwxy

x3F00

13

x41001310

x4100

R6

Note: Caller needs to know number and type of arguments,doesn't know about local variables.

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

14-14

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Starting the Callee Function; store return addressSTR R7, R6, #1

ret valret addrdyn linkabret valret addrdyn linkabwxy

x3F00

13

x3100 x41001310

x4100

R6

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

14-15

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Ending the Callee Functionreturn y;

; copy y into return valueLDR R0, R6, #6STR R0, R6, #0; load the return addressLDR R7, R6, #1; load the dynamic link; (pops the activation record)LDR R6, R6, #2; return control to callerRET

ret valret addrdyn linkabret valret addrdyn linkabwxy

x3F00

13

40x3100 x41001310203040

x4100

R6

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

14-16

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Resuming the Caller Functionb = NoName(a,10);

; load return valueLDR R0, R6, #5; store result into bSTR R0, R6, #4

ret valret addrdyn linkabret val

x3F00

134040

x4100

R6

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

14-17

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.

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

14-18

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example: LC-2 Code for ToUpperToUpper 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 < 'a' LD R1, neg_z ; load -'z' ADD R1, R0, R1 ; inchar - 'z' BRp 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 outcharFALSE 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