C Programming: Stack Frames, Local Variables, and Pointer Variables, Slides of Electric Machines

An in-depth explanation of c stack frames, local variables, and pointer variables. It covers the concept of stack frames, the use of pointer variables, and the difference between pass by value and pass by reference. It also includes examples and code snippets.

Typology: Slides

2012/2013

Uploaded on 04/30/2013

ekaan
ekaan 🇮🇳

4.5

(4)

54 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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)
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C Programming: Stack Frames, Local Variables, and Pointer Variables and more Slides Electric Machines in PDF only on Docsity!

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)

Compiling C

Example C program

#include <stdio.h>
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)? ");
scanf("%d", &rate);
/* Calculate total time in seconds */
time = amount / rate;
/* Convert time into hours, minutes, seconds */
hours = time / 3600; /* 3600 seconds in an hour */
minutes = (time % 3600) / 60; /* 60 seconds in a minute */
seconds = ((time % 3600) % 60); /* remainder is seconds */
/* Output results */
printf("Transfer Time : %d h %d m %d s \n", hours, minutes, seconds);
return 0

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?

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 Caller’s Frame Pointer (R5) Caller’s R7 (contains caller’s caller’s PC) Function Return Value Function Pass Value 1 …….. Function Pass Value n R R …….. “Previous R5” Local Variables



_Frame_ 

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…

Context Frame or Activation Record Format

Function stacked stuff …….. …….. Local Variables Caller’s Frame Pointer (R5) Caller’s R7(contains ITS caller’s PC) Function Return Value Function Pass Value 1 …….. Function Pass Value n R R (Stack PTR) (Frame PTR) Calling program Called Program Called Program Called Program Called Program “PUSHED” on Stack By: …….. Local Variables

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 & is referred to as the unary or monadic operator &variable returns the address of the variable variable

Example

Define local variables:
int object;
int *ptr;
Now, let’s 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?

Example: Parameter Passing by Value

#include <stdio.h>
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;
return;
Snapshot before return from subroutine

Scanf( ) function

  • Recall reading from the keyboard in C: scanf(“%d”, &input);
  • Why do we use &input?

Pointer

Example

#include <stdio.h> 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,&remainder); if (!error) / !error indicates no error */ printf("Answer: %d remainder %d\n", quotient, remainder); else printf("IntDivide failed.\n"); } int IntDivide(int x, int y, int *quoPtr, int *remPtr) { if (y != 0) { quoPtr = x / y; / Modify *quoPtr */ remPtr = x % y; / Modify *remPtr */ return 0; } else return -1;

} Docsity.com