




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Stack:
Pointer Variables:
Device Registers
PC
R
R
R
Op Sys
Context Frame (Activation Record) Format
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
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ā¦
#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; }
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)
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
to by a pointer variableā.
Example: Parameter Passing by Value
4 ļ
3 ļ
?ļ
Scanf( ) function
scanf(ā%dā, &input);
#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", ÷nd); printf("Input divisor: "); scanf("%d", &divisor);
error = IntDivide(dividend,divisor,"ient,&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;
Snapshots of Stack Before, During, and After Function Calls