Subroutines and Parameter Passing -...

Post on 06-Oct-2020

8 views 0 download

Transcript of Subroutines and Parameter Passing -...

Subroutines and Parameter PassingECE2893

Lecture 5

ECE2893 Subroutines and Parameter Passing Spring 2011 1 / 10

C/C++ Subroutines (Functions)

1 Like almost all programming languages, C and C++ allow andencourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

C/C++ Subroutines (Functions)1 Like almost all programming languages, C and C++ allow and

encourage the use of subroutines, which are often called functionsin C/C++.

2 Functions in C/C++ allow decomposition of a problem into smallersub–problems that are easier to comprehend and debug.

3 Functions in C/C++ (optionally) return a value. This is analagousto an algebraic function F (x , y) = x2 + 2y + 3, that computes avalue given inputs (independent variables) x and y .

4 Functions must be declared before they can be called. More onthis later.

5 Functions obviously can call other functions. This happensfrequently in most C/C++ programming.

6 A function can be called in C/C++ simply by having a singlestatement with the function name and arguments. More on thislater.

7 A function can also be called as a term in an expression. More onthis later also.

ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

Declaring Functions

1 A function must be declared before it can be called. This isanalogous to the need to declare a variable before it is referenced.

2 Functions can be declared in one of two ways.1 Use a function prototype, such as:

int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Declaring Functions1 A function must be declared before it can be called. This is

analogous to the need to declare a variable before it is referenced.

2 Functions can be declared in one of two ways.1 Use a function prototype, such as:

int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Declaring Functions1 A function must be declared before it can be called. This is

analogous to the need to declare a variable before it is referenced.2 Functions can be declared in one of two ways.

1 Use a function prototype, such as:int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Declaring Functions1 A function must be declared before it can be called. This is

analogous to the need to declare a variable before it is referenced.2 Functions can be declared in one of two ways.

1 Use a function prototype, such as:int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Declaring Functions1 A function must be declared before it can be called. This is

analogous to the need to declare a variable before it is referenced.2 Functions can be declared in one of two ways.

1 Use a function prototype, such as:int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Declaring Functions1 A function must be declared before it can be called. This is

analogous to the need to declare a variable before it is referenced.2 Functions can be declared in one of two ways.

1 Use a function prototype, such as:int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Declaring Functions1 A function must be declared before it can be called. This is

analogous to the need to declare a variable before it is referenced.2 Functions can be declared in one of two ways.

1 Use a function prototype, such as:int Average(int d[], int L);Notice we specfiy the return type (int in this case), the name ofthe function (Average in this case), the list of parameters to thefunction, followed by a semicolon.This tells the compiler that such a function exists and can be called,but the actual implementation of Average will be done elsewhere.

2 Provide a function implementation, such as:int Average(int d[], int L)

{int i = 0; // Index variable// Remainder omitted for brevity

}

3 If function A calls function B, and B also calls A, then a prototypeMust be used for one of the two.

4 A function can be declared with void return type, indicating thefunction in fact does not return a value.

ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

Calling Functions

1 A function can be called by simply using the function name in aprogram, such as:PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd);The PrintLn function has been declared and implemented inece2893.h.

2 A function that returns a value (a return type of anything exceptvoid) can be used anywhere a variable name or constant isexpected, such as:PrintLn("GCD(", m[i], ",", n[i], " = ",

Euclid(m[i], n[i]));Notice the call to the Euclid function as a parameter to thePrintLn function.

3 Another example of function calls in expressions is:double x = R * cos(theta);double y = R * sin(theta);In this example, both cos and sin are pre–defined functions thatcan be called anywhere a value is expected.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

Calling Functions1 A function can be called by simply using the function name in a

program, such as:PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd);The PrintLn function has been declared and implemented inece2893.h.

2 A function that returns a value (a return type of anything exceptvoid) can be used anywhere a variable name or constant isexpected, such as:PrintLn("GCD(", m[i], ",", n[i], " = ",

Euclid(m[i], n[i]));Notice the call to the Euclid function as a parameter to thePrintLn function.

3 Another example of function calls in expressions is:double x = R * cos(theta);double y = R * sin(theta);In this example, both cos and sin are pre–defined functions thatcan be called anywhere a value is expected.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

Calling Functions1 A function can be called by simply using the function name in a

program, such as:PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd);The PrintLn function has been declared and implemented inece2893.h.

2 A function that returns a value (a return type of anything exceptvoid) can be used anywhere a variable name or constant isexpected, such as:PrintLn("GCD(", m[i], ",", n[i], " = ",

Euclid(m[i], n[i]));Notice the call to the Euclid function as a parameter to thePrintLn function.

3 Another example of function calls in expressions is:double x = R * cos(theta);double y = R * sin(theta);In this example, both cos and sin are pre–defined functions thatcan be called anywhere a value is expected.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

Calling Functions1 A function can be called by simply using the function name in a

program, such as:PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd);The PrintLn function has been declared and implemented inece2893.h.

2 A function that returns a value (a return type of anything exceptvoid) can be used anywhere a variable name or constant isexpected, such as:PrintLn("GCD(", m[i], ",", n[i], " = ",

Euclid(m[i], n[i]));Notice the call to the Euclid function as a parameter to thePrintLn function.

3 Another example of function calls in expressions is:double x = R * cos(theta);double y = R * sin(theta);In this example, both cos and sin are pre–defined functions thatcan be called anywhere a value is expected.

ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

Passing Parameters

1 In C and C++, all parameters (with one exception discussed later)are passed by value.

2 This means that when calling a function, the current value of thearguments passed to the function are copied to the Stack.

3 A function is free to read or change the value of any parameter,but this does not affect the value in the caller’s variable.

4 An example is illustrated on the next slide.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

Passing Parameters1 In C and C++, all parameters (with one exception discussed later)

are passed by value.

2 This means that when calling a function, the current value of thearguments passed to the function are copied to the Stack.

3 A function is free to read or change the value of any parameter,but this does not affect the value in the caller’s variable.

4 An example is illustrated on the next slide.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

Passing Parameters1 In C and C++, all parameters (with one exception discussed later)

are passed by value.2 This means that when calling a function, the current value of the

arguments passed to the function are copied to the Stack.

3 A function is free to read or change the value of any parameter,but this does not affect the value in the caller’s variable.

4 An example is illustrated on the next slide.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

Passing Parameters1 In C and C++, all parameters (with one exception discussed later)

are passed by value.2 This means that when calling a function, the current value of the

arguments passed to the function are copied to the Stack.3 A function is free to read or change the value of any parameter,

but this does not affect the value in the caller’s variable.

4 An example is illustrated on the next slide.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

Passing Parameters1 In C and C++, all parameters (with one exception discussed later)

are passed by value.2 This means that when calling a function, the current value of the

arguments passed to the function are copied to the Stack.3 A function is free to read or change the value of any parameter,

but this does not affect the value in the caller’s variable.4 An example is illustrated on the next slide.

ECE2893 Subroutines and Parameter Passing Spring 2011 5 / 10

Pass by Value Example/ / Pass by Value i l l u s t r a t i o n/ / George F . Ri ley , Georgia Tech , ECE2893

i n t Func ( i n t x , i n t y ){

P r in tLn ( " He l lo from Func , x i s " , x , " y i s " , y ) ;x = 5 ; / / " Func " can change the value o f x and yy = 10;Pr in tLn ( " He l lo from Func , x i s " , x , " y i s " , y ) ;

}

i n t main ( ){

i n t x = 100;i n t y = 45;Pr in tLn ( " He l lo from main , x i s " , x , " y i s " , y ) ;/ / Ca l l Func , passing the value o f x and yFunc ( x , y ) ;P r in tLn ( " He l lo from main , x i s " , x , " y i s " , y ) ;

}

The resulting output from this example would be:Hello from main, x is 100 y is 45Hello from Func, x is 100 y is 45Hello from Func, x is 5 y is 10Hello from main, x is 100 y is 45

ECE2893 Subroutines and Parameter Passing Spring 2011 6 / 10

Array Arguments Passed by Reference

1 The exception to the pass by value rule is when the argument isan array.

2 In this case, the array argument is passed by reference.3 This means that the function with an array argument can change

the contents of the array (as in the pass by value case). However,in this case, the value of the array from the caller will be changed.

4 This makes sense from an efficiency standpoint. If the arraycontains hundreds of thousands of elements, it would clearly beinefficient to copy the entire array contents to the stack on eachcall.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

Array Arguments Passed by Reference1 The exception to the pass by value rule is when the argument is

an array.

2 In this case, the array argument is passed by reference.3 This means that the function with an array argument can change

the contents of the array (as in the pass by value case). However,in this case, the value of the array from the caller will be changed.

4 This makes sense from an efficiency standpoint. If the arraycontains hundreds of thousands of elements, it would clearly beinefficient to copy the entire array contents to the stack on eachcall.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

Array Arguments Passed by Reference1 The exception to the pass by value rule is when the argument is

an array.2 In this case, the array argument is passed by reference.

3 This means that the function with an array argument can changethe contents of the array (as in the pass by value case). However,in this case, the value of the array from the caller will be changed.

4 This makes sense from an efficiency standpoint. If the arraycontains hundreds of thousands of elements, it would clearly beinefficient to copy the entire array contents to the stack on eachcall.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

Array Arguments Passed by Reference1 The exception to the pass by value rule is when the argument is

an array.2 In this case, the array argument is passed by reference.3 This means that the function with an array argument can change

the contents of the array (as in the pass by value case). However,in this case, the value of the array from the caller will be changed.

4 This makes sense from an efficiency standpoint. If the arraycontains hundreds of thousands of elements, it would clearly beinefficient to copy the entire array contents to the stack on eachcall.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

Array Arguments Passed by Reference1 The exception to the pass by value rule is when the argument is

an array.2 In this case, the array argument is passed by reference.3 This means that the function with an array argument can change

the contents of the array (as in the pass by value case). However,in this case, the value of the array from the caller will be changed.

4 This makes sense from an efficiency standpoint. If the arraycontains hundreds of thousands of elements, it would clearly beinefficient to copy the entire array contents to the stack on eachcall.

ECE2893 Subroutines and Parameter Passing Spring 2011 7 / 10

Example Pass–by–Reference/ / I l l u s t r a t e passing ar ray arguments by re ference/ / George F . Ri ley , Georgia Tech , ECE2893#include " ece2893 . h "void Sub1 ( i n t d [ ] , i n t n ){ / / Sub1 can change ar ray d , and the c a l l e r " sees " the changes

i n t i = 0 ;while ( i < n )

{d [ i ] = d [ i ] ∗ 10; / / Change each element i n di = i + 1 ;

}}

i n t main ( ){

const i n t sz = 5; / / Size o f ar rayi n t d [ sz ] = { 1 , 2 , 3 , 4 , 5 } ;i n t i = 0 ;while ( i < sz )

{P r in tLn ( " Before Sub1 , d [ " , i , " ] i s " , d [ i ] ) ;i = i + 1 ;

}Sub1 ( d , sz ) ;i = 0 ;while ( i < sz )

{P r in tLn ( " A f t e r Sub1 , d [ " , i , " ] i s " , d [ i ] ) ;i = i + 1 ;

}}

The resulting output from this program is:

Before Sub1, d[0] is 1Before Sub1, d[1] is 2Before Sub1, d[2] is 3Before Sub1, d[3] is 4Before Sub1, d[4] is 5After Sub1, d[0] is 10After Sub1, d[1] is 20After Sub1, d[2] is 30After Sub1, d[3] is 40After Sub1, d[4] is 50

ECE2893 Subroutines and Parameter Passing Spring 2011 8 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24 PC → Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP → Sub0::ch[4] = 0SP → Sub0::x = ?

Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP → Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14 PC → int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP → Sub0::ch[4] = 0SP → Sub0::x = ?

Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?

Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15 PC → int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP → Sub0::ch[4] = 0

SP →

Sub0::x = ?

Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16 PC → char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17 PC → Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8 PC → int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

SP →

Sub1::k1 = 5

Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9 PC → Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3 PC → k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

Sub2::k = 5

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4 PC → }5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

Sub2::k = 4

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10 PC → }11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18 PC → Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3 PC → k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

SP →

Sub1::k1 = 5

Addr of Line 19

SP →

Sub2::k = 2Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4 PC → }5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →

SP →

Sub1::k1 = 5

Addr of Line 19

SP →

Sub2::k = 4Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19 PC → }21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25 PC → }

Stack Memory

SP →Addr of Line 10

SP →SP → Sub1::k1 = 5

SP →Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP → Sub0::ch[4] = 0SP → Sub0::x = ?

Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP → Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 9 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24 PC → Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP →

?

SP →

??

SP →

?????

SP →

?

SP →

????

SP →

???

SP → ?

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14 PC → int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP →

?

SP →

??

SP →

?????

SP →

?

SP →

????

SP → ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15 PC → int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP →

?

SP →

??

SP →

?????

SP →

?SP → ?

Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16 PC → char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP →

?

SP →

??

SP →

?????

SP → ?

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17 PC → Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP →

?

SP →

??

SP → ?Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8 PC → int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP →

?SP → ?

Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9 PC → Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

??

SP → ?

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3 PC → k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP → ?Addr of Line 10

SP →

Sub2::k = 5

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4 PC → }5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP → ?Addr of Line 10

SP →

Sub2::k = 4

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10 PC → }11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

?Addr of Line 10

SP → Sub2::k = 4

SP →

Sub1::k1 = 5Addr of Line 18

SP →

Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18 PC → Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

?Addr of Line 10

SP →

Sub2::k = 4

SP →

Sub1::k1 = 5Addr of Line 18

SP → Sub1::j = 1Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3 PC → k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

?Addr of Line 10

SP →

Sub2::k = 4SP → Sub1::k1 = 5

Addr of Line 19

SP →

Sub2::k = 2Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4 PC → }5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

?Addr of Line 10

SP →

Sub2::k = 4SP → Sub1::k1 = 5

Addr of Line 19

SP →

Sub2::k = 4Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19 PC → }21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25

PC →

}

Stack Memory

??

SP →

?Addr of Line 10

SP →

Sub2::k = 4

SP →

Sub1::k1 = 5Addr of Line 19

SP → Sub2::k = 4Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP →

Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10

Stack Usage Example (more realistic)1

PC →

void Sub2(int k)2

PC →

{3

PC →

k = 4;4

PC →

}5

PC →

6

PC →

void Sub1(int j)7

PC →

{8

PC →

int k1 = 5;9

PC →

Sub2(k1);10

PC →

}11

PC →

12

PC →

void Sub0(int i,int j)13

PC →

{14

PC →

int d[4];15

PC →

int x;16

PC →

char ch[]="Test";17

PC →

Sub1(i);18

PC →

Sub2(j);19

PC →

}21

PC →

22

PC →

int main()23

PC →

{24

PC →

Sub0(1, 2);25 PC → }

Stack Memory

??

SP →

?Addr of Line 10

SP →

Sub2::k = 4

SP →

Sub1::k1 = 5Addr of Line 19

SP →

Sub2::k = 4Sub0::ch[0] = ’T’Sub0::ch[1] = ’e’Sub0::ch[2] = ’s’Sub0::ch[3] = ’t’

SP →

Sub0::ch[4] = 0

SP →

Sub0::x = ?Sub0::d[0] = ?Sub0::d[1] = ?Sub0::d[2] = ?

SP →

Sub0::d[3] = ?Addr of Line 25Sub0::i = 1

SP → Sub0::j = 2

ECE2893 Subroutines and Parameter Passing Spring 2011 10 / 10