C Stack Frames / Pointer variables - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

C Stack Frames / Pointer variables

Description:

C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining & using – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 18
Provided by: Larr155
Category:

less

Transcript and Presenter's Notes

Title: C Stack Frames / Pointer variables


1
C Stack Frames / Pointer variables
  • Stack
  • Local Variables
  • Pass Return values
  • Frame Ptr linkage (R5) and PC linkage (R7)
  • Pointer Variables
  • Defining using
  • Pass by value
  • Pass by reference (pointer)

2
Compiling C
3
Allocating Space for Variables
x0000
Trap Vectors
  • Global data section
  • All global variables stored here(actually all
    static variables)
  • R4 points to Global Variables
  • Run-time stack
  • Used for local variables
  • (among other things)
  • R6 points to top of stack
  • R5 points to top frame on stack
  • New frame created for each blockor scope (goes
    away when block exited)
  • Accessing a variable
  • Global LDR R1, R4, x
  • Local LDR R2, R5, -y
  • Offset distance from beginning of storage area

x0100
Intr Vectors
x0200
Op Sys
x3000
instructions
PC
R4
global data
Heap
R6
run-time stack
R5

xFE00
Device Registers
xFFFF
4
Example C program
  • include ltstdio.hgt
  • int main()
  • / Declare local variables /
  • int amount / The number of bytes to be
    transferred /
  • int rate / The average network transfer
    rate /
  • int time / The time, in seconds, for the
    transfer /
  • int hours / The number of hours for the
    transfer /
  • int minutes / The number of mins for the
    transfer /
  • int seconds / The number of secs for the
    transfer /
  • / Get input number of bytes and network
    transfer rate /
  • printf("How many bytes of data to be
    transferred? ")
  • scanf("d", amount)
  • printf("What is the transfer rate (in
    bytes/sec)? ")

5
Symbol Table
  • Like assembler, compiler needs to know
    information associated with identifiers
  • in assembler, all identifiers were labels and
    information is address
  • Compiler keeps more information
  • - Name (identifier)
  • - Type
  • - Location in memory
  • - Scope

Where are local variables stored? Why?
6
Local Variable Storage
  • Local variables are stored in an stack frame.
  • (also known as an activation record)
  • Symbol table offset gives thedistance from the
    base of the frame.
  • R5 is the frame pointer holds addressof the
    base of the current frame.
  • Because stack grows downward,base is the highest
    address of the frame,and variable offsets are lt
    0.

seconds minutes hours time rate amount
R5
7
Context Frame (Activation Record) Format(Note
you will see that there is some inconsistency as
to where the Frame begins)
Function stacked stuff ..
.. Local Variables Callers Frame Pointer
(R5) Callers R7 (contains callers callers
PC) Function Return Value Function Pass Value 1
.. Function Pass Value n
R6

R5
- - - - - - - -


Frame
.. Local Variables
Previous R5
- - - - - - - -
8
Function Call Implementation
  • Caller pushes arguments (last to first).
  • Caller invokes subroutine (JSR).
  • Callee allocates return value, pushes R7 and R5.
  • Callee allocates space for local variables.
  • Callee executes function code.
  • Callee stores result into return value slot.
  • Callee pops local variables, pops R5, pops R7.
  • Callee returns RET (or JMP R7).
  • Caller loads return value and pops arguments.
  • Caller resumes computation

9
Function Call Implementation (with nested calls)
  • Caller pushes arguments (last to first).
  • Caller invokes subroutine (JSR).
  • Callee allocates return value, pushes R7 and R5.
  • Callee allocates space for local variables.
  • Callee executes function code.
  • Caller pushes arguments (last to first).
  • Caller invokes subroutine (JSR).
  • Caller loads return value and pops
    arguments.
  • Caller resumes computation
  • Callee stores result into return value slot.
  • Callee pops local variables, pops R5, pops R7.
  • Callee returns RET (or JMP R7).

10
Context Frame or Activation Record Format
Function stacked stuff ..
.. Local Variables Callers Frame Pointer
(R5) Callers R7(contains ITS callers
PC) Function Return Value Function Pass Value 1
.. Function Pass Value n
R6
(Stack PTR)
Called Program

Called Program
R5
(Frame PTR)
Called Program
Called Program

Calling program
.. Local Variables
PUSHED on Stack By
11
Declaring Pointer variables
  • int ptr ptr is a pointer variable
    that points to an int type variable
  • char cp cp is a pointer variable
    that points to a character type variable
  • double dp dp is a pointer variable
    that points to a double type variable
  • is referred to as the indirection operator,
    or dereference operator
  • ptr returns the value of the variable
    pointed to by pointer variable ptr

12
Using Pointer Variables
  • A pointer is a variable which contains the
    address in memory of another variable.
  • We can have a pointer to any variable type.
  • The unary or monadic operator provides the
    address of a variable.
  • The indirection or dereference operator gives
    the contents of an object pointed
  • to by a pointer variable.
  • To declare a pointer to a variable int
    pointer

int x1, y2 / let x be at x3000 and y at 3001
/ int ip / ip is an int type pointer
/ ipx / ipx3000 x 1
y2 / yip / ipx3000 x 1
y1 / xip / ipx3000 xx3000
y1 / ip3 / ipx3000 x3
y1 /
Note ip ip 1 actually increments ip by
4. Why?
13
Example
  • Define local variables
  • int object
  • int ptr
  • Now, lets assign values to them
  • object 4
  • ptr object
  • ptr ptr 1
  • The last statement above is equivalent to
  • object object 1

What are the final values of object and ptr ?
14
Example Parameter Passing by Value
  • include ltstdio.hgt
  • void Swap(int firstVal, int secondVal)
  • int main()
  • int valueA 3
  • int valueB 4
  • Swap(valueA, valueB)
  • return 0
  • void Swap(int firstVal, int secondVal)
  • int tempVal / Holds firstVal when swapping
    /
  • tempVal firstVal
  • firstVal secondVal
  • secondVal tempVal

Snapshot before return from subroutine
15
Example Parameter Passing by Reference
  • include ltstdio.hgt
  • void NewSwap(int firstVal, int secondVal)
  • int main()
  • int valueA 3
  • int valueB 4
  • NewSwap(valueA, valueB)
  • return 0
  • void NewSwap(int firstVal, int secondVal)
  • int tempVal / Holds firstVal when swapping /

Snapshots During the Exchange
What happened differently using pass by
reference, rather than pass by value ?
16
Scanf( ) function
  • Recall reading from the keyboard in C
  • scanf(d, input)
  • Why do we use input ?

17
Pointer Example
  • include ltstdio.hgt
  • int IntDivide(int x, int y, int quoPtr, int
    remPtr)
  • int main()
  • int dividend / The number to be
    divided /
  • int divisor / The number to divide
    by /
  • int quotient / Integer result of
    division /
  • int remainder / Integer remainder of
    division /
  • int error / Did something go
    wrong? /
  • printf("Input dividend ")
  • scanf("d", dividend)
  • printf("Input divisor ")
  • scanf("d", divisor)
  • error IntDivide(dividend,divisor,quotient,r
    emainder)
Write a Comment
User Comments (0)
About PowerShow.com