Functions & Activation Records in C Programming, Study notes of Computer Architecture and Organization

This document from wright state university's computer science & engineering department discusses functions in c programming, including their purpose, implementation, and activation records. It covers function calls, activation record bookkeeping, and the run-time stack. The document also provides an example function call and guidelines for good subroutines.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-x09
koofers-user-x09 🇺🇸

5

(1)

10 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 14Chapter 14
Functions & Activation Records
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Functions & Activation Records in C Programming and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Chapter 14Chapter 14

Functions & Activation Records

2 Wright State University, College of Engineering CEG 320/

FunctionFunction

 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 the same type
  • by convention, only one function named main (this defines the initial PC)  In other languages, called procedures, subroutines, ...

4 Wright State University, College of Engineering CEG 320/

Why Declaration?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  What if we need to pass more parameters than we have registers?!?  All the compiler needs to know
  • (1) what symbol to call the function (its name),
  • (2) the type of the return value (for proper usage/intermediate storage)
  • (3) how to set up the stack to call the function (arguments)  This allows the compiler to inserted the necessary code for function activation when it is called

5 Wright State University, College of Engineering CEG 320/

Implementing a Function Call: OverviewImplementing a Function Call: Overview

 Making a function call involves three basic steps

  • The parameters of the call are passed from the caller to the callee
  • The callee does its task
  • A return value is returned to the caller  Functions in C are caller-independent
  • Every function must be callable from any other function  Activation record
  • information about each function, including arguments and local variables
  • also includes bookkeeping information  values that must always be saved by the caller before doing the JSR
  • arguments and bookeeping info pushed on run-time stack by caller
  • callee responsible for other uses of stack (such as local variables)
  • popped of the stack by caller after getting return value  R5 (the frame pointer) is the “bottom” of the stack as far as the callee function definition is aware.
  • It should not modify anything existing on the stack before its execution!

7 Wright State University, College of Engineering CEG 320/

Activation RecordActivation Record

 int funName(int a, int b) { int w, x, y; . . . return y; } Name Type Offset Scope a b w x y int int int int int 4 5 0

  • 1
  • 2 funName funName funName funName funName y x w dynamic link return address return value a b bookkeeping locals args

R

R

8 Wright State University, College of Engineering CEG 320/

Activation Record BookkeepingActivation 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 to protect it from change in case another function (JSR) is called  Dynamic link
  • caller’s frame pointer (basically a caller save of its own frame pointer R5)
  • used to “pop” this activation record from stack  In the LC-3 the format for an activation record includes only (and always) these three words pushed in that order!

10 Wright State University, College of Engineering CEG 320/

Example Function CallExample Function Call

int Callee (int q, int r) {

int k, m;

return k;

int Caller (int a) {

int w = 25;

w = Callee (w,10);

return w;

w dyn link ret addr ret val a

xFD

R

R

11 Wright State University, College of Engineering CEG 320/

The Function Call: Preparation to JumpThe Function Call: Preparation to Jump

int Callee (int q, int r) …  int Caller (int a) …… w = Callee(w, 10);…  ; push second arg AND R0, R0, # ADD R0, R0, # ADD R6, R6, #- 1 STR R0, R6, # ; push first argument LDR R0, R5, # ADD R6, R6, #- 1 STR R0, R6, #  JSR CALLEE ; Jump! Note: Caller needs to know number and type of arguments, doesn't know about local variables. q r w dyn link ret addr ret val a

xFD

R

R

New R

13 Wright State University, College of Engineering CEG 320/

Ending the Callee FunctionEnding the Callee Function

 return k;  ; copy k into return value LDR R0, R5, # STR R0, R5, # ; pop local variables ADD R6, R5, # ; pop dynamic link (into R5) LDR R5, R6, # ADD R6, R6, # ; pop return addr (into R7) LDR R7, R6, # ADD R6, R6, # ; 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 217 25 10 25 xFD

R

R

new R new R

14 Wright State University, College of Engineering CEG 320/

Resuming the Caller FunctionResuming the Caller Function

 w = Callee(w,10);  ; JSR Callee ; LAST COMMAND ; load return value (top of stack) LDR R0, R6, # ; perform assignment of return value STR R0, R5, # ; pop return value and arguments ADD R6, R6, # … Continue caller code ret val q r w dyn link ret addr ret val a

xFD

R

R

new R

16 Wright State University, College of Engineering CEG 320/

In Summary: The StackIn Summary: The Stack

 Since our program usually starts at a low memory address and grows upward, we start the stack at a high memory address and work downward.  Purposes

  • Temporary storage of variables
  • Temporary storage of program addresses
  • Communication with subroutines  Push variables on stack  Jump to subroutine  Clean stack  Return

17 Wright State University, College of Engineering CEG 320/

Parameter passing on the stackParameter passing on the stack

 If we use registers to pass our parameters:

  • Limit of 8 parameters to/from any subroutine.
  • We use up registers so they are not available to our program.  So, instead we push the parameters onto the stack.
  • Parameters are passed on the stack
  • Return values can be provided in registers (such as R0) or on the stack.
  • Generally, only R6 should be changed by a subroutine.  Other registers that are changed should must be callee saved/restored.  Subroutines should be transparent  Both the subroutine and the main program must know how many parameters are being passed!
  • In C we would use a prototype: int power (int number, int exponent);  In assembly, you must take care of this yourself.  After you return from a subroutine, you must also clear the stack.
  • Clean up your mess!

19 Wright State University, College of Engineering CEG 320/

Know how to…Know how to…

 Push parameters onto the stack  Access parameters on the stack using base + offset addressing mode  Draw the stack to keep track of subroutine execution

  • Parameters
  • Return address  Clean the stack after a subroutine call

20 Wright State University, College of Engineering CEG 320/

Practice problemsPractice problems

 14.2, 14.4, 14.9, 14.10, 14.15 (good!)  The convention in LC-3 assembly is that all registers are callee-saved except for R5 (the frame pointer) R6 (the stack pointer) and R7 (the return link).

– Why is R5 not callee-saved?

– Why is R6 not callee-saved?

– Why is R7 not callee-saved?

 Is it true that any problem that can be solved recursively can be solved iteratively using a stack data structure? Why or why not?