DOC PREVIEW
Penn CIS 240 - Functions

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Based on slides © McGraw-HillAdditional material © 2004/2005 Lewis/MartinChapter 14Functions90CSE 240Function 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 (or parameters) passed in• Single result returned (optional)• Return value is always a particular type In other languages, called procedures, subroutines, ...91CSE 240Example of High-Level Structure main(){ setup_board(); /* place pieces on board */ determine_sides(); /* choose black/white */ /* Play game */ while (no_outcome_yet()){ whites_turn(); blacks_turn(); } }Structure of programis evident, even withoutknowing implementation.92CSE 240Functions in C Definition int factorial(int n) { int i; int result = 1; for (i = 1; i <= n; i++) { result = result * i; } return result; } Function call -- used in expression a = x + factorial(f + g);type ofreturn valuename offunctiontypes of allarguments1. evaluate arguments2. execute function3. use return value in expressionexits function withspecified return value293CSE 240Implementing Functions and Variables in LC-3 We’ve talked about…• VariablesLocalGlobal• FunctionsParameter passingReturn values What does the assembly code look like for these idioms? Important notes• Different compilers for different ISAs do things differently• As long as a compiler is consistent• We’re straying from the book’s version to simplify thingsLeaving out the R5 “frame pointer”94CSE 240Allocating Space for Variables Global data section• All global variables stored here(actually all static variables)• R4 points to beginning Run-time stack• Used for local variables• R6 points to top of stack• New frame for each block(goes away when block exited) Offset = distance from beginningof storage area• Global: LDR R1, R4, #4• Local: LDR R2, R6, #3instructionsglobal datarun-timestack0x00000xFFFFPCR4R695CSE 240Local Variable Storage Local variables stored in activation record (stack frame) Symbol table “offset” gives thedistance from the base of the frame• A new frame is pushed on therun-time stack each time block is entered• R6 is the stack pointer – holds addressof current top of run-time stack• Because stack grows downward,stack pointer is the smallest addressof the frame, and variable offsets are >= 0.amounthoursminutesecondsR696CSE 240Symbol Table Compiler tracks each symbol (identifiers) and its location• In assembler, all identifiers were labels• In compiler, identifiers are variables Compiler keeps more information Name (identifier) Type Location in memory ScopeName Type Offset Scopeamounthoursminutessecondsdoubleintintint0123mainmainmainmain397CSE 240Symbol Table Example int main() { int seconds; int minutes; int hours; double amount; … }Name Type Offset Scopeamounthoursminutessecondsdoubleintintint0123mainmainmainmainamounthoursminutesecondsR6R698CSE 240Example: Compiling to LC-3 #include <stdio.h> int inGlobal; main() { int inLocal; int outLocalA; int outLocalB; /* initialize */ inLocal = 5; inGlobal = 3; /* perform calculations */ outLocalA = inLocal & ~inGlobal; outLocalB = (inLocal + inGlobal) + outLocalB; /* print results */ printf("The results are: outLocalA = %d, outLocalB = %d\n", outLocalA, outLocalB); }main0intoutLocalBmain1intoutLocalAmain2intinLocalglobal0intinGlobalScopeOffsetTypeName99CSE 240Example: Code Generation ; main ; inLocal = 5 AND R0, R0, #0 ADD R0, R0, #5 ; inLocal = 5 STR R0, R6, #2 ; (offset = 2) ; inGlobal = 3 AND R0, R0, #0 ADD R0, R0, #3 ; inGlobal = 3 STR R0, R4, #0 ; (offset = 0)100CSE 240Example (continued) ; first statement: ; outLocalA = inLocal & ~inGlobal; LDR R0, R6, #2 ; get inLocal(offset = 2) LDR R1, R4, #0 ; get inGlobal NOT R1, R1 ; ~inGlobal AND R2, R0, R1 ; inLocal & ~inGlobal STR R2, R6, #1 ; store in outLocalA ; (offset = 1)4101CSE 240Example (continued) ;outLocalB = (inLocal + inGlobal) + outLocalA; LDR R0, R6, #2 ; inLocal LDR R1, R4, #0 ; inGlobal ADD R0, R0, R1 ; R0 is sum LDR R1, R6, #1 ; outLocalA ADD R2, R0, R1 ; R2 is sum STR R2, R6, #0 ; outLocalB (offset = 0) R0102CSE 240Implementing Functions Activation record• Information about each function,including arguments and local variables• Also stored on run-time stackCalling functioncopy args into stack or regscall functionget resultCalled functionallocate activation recordsave registersexecute codeput result in AR or regpop activation recordreturn103CSE 240Run-Time Stack for FunctionsmainMemoryR6funcMemoryR6mainMemorymainBefore call During call After callR6104CSE 240Activation Record int func(int a, int b){ int w, x, y; . . . return y;}Name Type Offset Scopeba“ret. value”wxyintintintintintint765210funcfuncfuncfuncfuncfuncyxwsave R0return addr. (R7)return valueabbookkeepinglocalsargsR65105CSE 240Activation 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 Save registers• Save all other registers used (but not R6, and often not R4)106CSE 240Function Call Example int main() { int x, y, val; x = 10; y = 11; val = max(x + 10, y); return val; } int max(int a, int b) { int result; result = a; if (b > a) { result = b; } return result; }“val”“y”“x”main return value“result”save R0save R1save R7max return value0123456R6 max’sviewR60123-3-2-1 main’sview“a”“b”107CSE 240Main Function (1 of 2) MAIN ADD R6, R6, #-4 ; allocate frame AND R0, R0, #0 ; x = 10 ADD R0, R0, #10 STR R0, R6, #2 AND R0, R0, #0 ; y = 11 ADD R0, R0, #11 STR R0, R6, #1 LDR R0, R6, #1 ; load y into R0 STR R0, R6, #-1 ; 2nd argument LDR R1, R6, #2 ; load x into R1 ADD R1, R1, #10 ; R1 = x + 10 STR R1, R6, #-2 ; 1st argument JSR MAX ; call max function … ; more here108CSE 240Max Function MAX ADD R6, R6, #-7 ; allocate frame STR R7, R6, #3 ; save R7 (link register) STR R1, R6, #2 ; save R1 STR R0, R6,


View Full Document

Penn CIS 240 - Functions

Download Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?