Pointer variables - Machine Organization - Lecture Slides, Slides of Electric Machines

Some concept of Machine Organization are Anatomy, Cache Access Time, Instruction Formats, Instruction Formats, Instruction Formats, Multidimensional Meshes, Network Processors, Snooping Protocol. Main points of this lecture are: Pointer Variables, Local Variables, Pass, Return Values, Frame Ptr Linkage, Linkage, Defining, Pass By Value, Pass By Reference, Global Variables

Typology: Slides

2012/2013

Uploaded on 04/30/2013

ekaan
ekaan šŸ‡®šŸ‡³

4.5

(4)

54 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
More
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
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Pointer variables - Machine Organization - Lecture Slides and more Slides Electric Machines in PDF only on Docsity!

More

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)

Allocating Space for Variables

  • 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 ā€œblockā€

or 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

instructions

global data

run-time

stack

Device Registers

x
xFFFF

PC

R

R

R

x
xFE
Trap Vectors

Op Sys

x

Heap

Intr Vectors
x

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

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

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…

Example

#include <stdio.h>

int Func1(int x); int Func2(int x); int Func3(int x); int Func4(int x);

int main() { int A = 3; int B = 5; int C;

C = A + B; C = Func1(C); C = C + 1;

return 2; }

int Func1(int x) { return Func2(x) + 1; }

int Func2(int x) { return Func3(x) + 1; }

int Func3(int x) { return Func4(x) + 1; }

int Func4(int x) { return x + 1; }

Example – Global Area

GLOBAL_DATA_START ; R4 (Global Stk Ptr)
  • Func1 .FILL lc3_Func1 ; + L1_second_prog .FILL lc3_L1_second_prog
  • L6_second_prog .FILL lc3_L6_second_prog ; +
  • Func2 .FILL lc3_Func2 ; +
  • L7_second_prog .FILL lc3_L7_second_prog ; +
  • Func3 .FILL lc3_Func3 ; +
  • L8_second_prog .FILL lc3_L8_second_prog ; +
  • Func4 .FILL lc3_Func4 ; +
  • L9_second_prog .FILL lc3_L9_second_prog ; +
  • scanf .FILL lc3_scanf ; +
  • printf .FILL lc3_printf ; +
  • L5_second_prog .FILL #2 ; +
  • L3_second_prog .FILL #5 ; +
  • L4_second_prog .FILL #1 ; +
  • L2_second_prog .FILL #3 ; +

Stack Snapshots

Stk in Func4: xEFE xEFE xEFE xEFE5 (Func4 local var) <- FramePtr (Func4) xEFE6 R5 FramePtr (xEFEA) xEFE7 R7 Saved PC (x3071) xEFE8 return value from Func4 = 9 xEFE9 pass value to Func4 (C = 8) xEFEA (Func3 local var) <- FramePtr (Func3) xEFEB R5 FramePtr (xEFEF) xEFEC R7 Saved PC (x3059) xEFED return value from Func3 space xEFEE pass value to Func3 (C = 8) xEFEF (Func2 local var) <- FramePtr (Func2) xEFF0 R5 FramePtr (xEFF4) xEFF1 R7 Saved PC (x3041) xEFF2 return value from Func2 space xEFF3 pass value to Func2 (C = 8) xEFF4 (Func1 local var) <- FramePtr (Func1) xEFF5 R5 FramePtr (xEFFB) xEFF6 R7 Saved PC (x3024) xEFF7 return value from Func1 space xEFF8 pass value to Func1 (C = 8) xEFF9 B = 5 xEFFA A = 3 xEFFB C = 8 (main local var) <- FramePtr (main) xEFFC R5 FramePtr (xEFFF) xEFFD R7 Saved PC (x300B) xEFFE return value from main space xEFFF <- StkPtr <- FramePtr (initially)

Stack Snapshots

Stk in Func4: xEFE xEFE xEFE xEFE5 (Func4 local var) <- FramePtr (Func4) xEFE6 R5 FramePtr (xEFEA) xEFE7 R7 Saved PC (x3071) xEFE8 return value from Func xEFE9 pass value to Func4 (C = 8) xEFEA (Func3 local var) <- FramePtr (Func3) xEFEB R5 FramePtr (xEFEF) xEFEC R7 Saved PC (x3059) xEFED return value from Func3 space xEFEE pass value to Func3 (C = 8) xEFEF (Func2 local var) <- FramePtr (Func2) xEFF0 R5 FramePtr (xEFF4) xEFF1 R7 Saved PC (x3041) xEFF2 return value from Func2 space xEFF3 pass value to Func2 (C = 8) xEFF4 (Func1 local var) <- FramePtr (Func1) xEFF5 R5 FramePtr (xEFFB) xEFF6 R7 Saved PC (x3024) xEFF7 return value from Func1 space xEFF8 pass value to Func1 (C = 8) xEFF9 B = 5 xEFFA A = 3 xEFFB C = 8 (main local var) <- FramePtr (main) xEFFC R5 FramePtr (xEFFF) xEFFD R7 Saved PC (x300B) xEFFE return value from main space xEFFF <- StkPtr <- FramePtr

Stk after return from Func4: xEFE xEFE xEFE xEFE8 return value from Func4 = 9 (C = 9) xEFE9 pass value to Func4 (C = 8) xEFEA (Func3 local var) <- FramePtr (Func3) xEFEB R5 FramePtr (xEFEF) xEFEC R7 Saved PC (x3059) xEFED return value from Func3 space xEFEE pass value to Func3 (C = 8) xEFEF (Func2 local var) <- FramePtr (Func2) xEFF0 R5 FramePtr (xEFF4) xEFF1 R7 Saved PC (x3041) xEFF2 return value from Func2 space xEFF3 pass value to Func2 (C = 8) xEFF4 (Func1 local var) <- FramePtr (Func1) xEFF5 R5 FramePtr (xEFFB) xEFF6 R7 Saved PC (x3024) xEFF7 return value from Func1 space xEFF8 pass value to Func1 (C = 8) xEFF9 B = 5 xEFFA A = 3 xEFFB C = 8 (main local var) <- FramePtr (main) xEFFC R5 FramePtr (xEFFF) xEFFD R7 Saved PC (x300B) xEFFE return value from main space xEFFF <- StkPtr <- FramePtr

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.

  • Theunary ormonadic operator & provides the ā€œaddress of a variableā€.
  • Theindirection ordereference operator ***** gives the ā€œcontents of an objectpointed

to by a pointer variableā€.

  • To declare a pointer to a variable: *int pointer ;

Note: ip = ip + 1 actually increments ip by 4. Why?

int x=1, y=2; /* let x be at x3000 and y at 3001 */
int ip; / ip is an int type pointer */
ip=&x; /* ip=x3000 x= 1 y=2 */
y=ip / ip=x3000 x= 1 y=1 */
x=ip; /* ip=x3000 x=x3000 y=1 */
ip=3; / ip=x3000 x=3 y=1 */

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 valueA; /* returns 3 */
void Swap(int firstVal, int secondVal)
int tempVal; /* Holds firstVal when swapping */
tempVal = firstVal;
firstVal = secondVal;
secondVal = tempVal;
return;

Snapshot before return from subroutine

4 

3 

?

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

Example C Program with Function Calls

int main ()

int a = 23;

int b = 14;

b = Watt(a); /* main calls both */

b = Volta(a,b);

int Watt(int c);

int w = 5;

w = Volta(w,10); /* Watt calls Volta */

return w;

int Volta(int q, int r)

int k = 3;

int m = 6;

... /* Volta calls no one */

return k+m;

Snapshots of Stack Before, During, and After Function Calls