1 / 33

EC310 Lesson 7: User Defined Functions and Stack Mechanics

EC310 Lesson 7: User Defined Functions and Stack Mechanics. Libraries. 1 #include < stdio.h >. Library Functions To date we have seen C programs which consist solely of the main function and/or import C

Télécharger la présentation

EC310 Lesson 7: User Defined Functions and Stack Mechanics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EC310 Lesson 7:User Defined Functions and Stack Mechanics

  2. Libraries 1 #include <stdio.h> Library Functions To date we have seen C programs which consist solely of the main function and/or import C libraries to utilize pre-defined library functions like printf, scanf or strcpy by adding a #include line at the beginning of the program for each C library we draw library functions from.

  3. main( ) 1 #include <stdio.h> 2 int main() 3 { 4 int A = 5, B = 3, C; 5 C = A + B; 6 printf("\n%d\n\n", C); 7 }

  4. Functions 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  5. User Defined Functions User Defined Function: Although C has pre-defined functions for use there is also the capability for the programmer to write their own function. To do this, you must use the following format: User Defined Function Syntax: type_returnedfunction_name (type_1 parameter_1, …, type_xparameter_x); { body of the function } • Arguments vs. Parameters: • In a function call, the inputs to the function are called the arguments. The values of the arguments are plugged into the the parameters in the function definition before the body of the function is executed. • A parameter should be thought of as a placeholder that “stands in” for an argument. The person writing the function may not know the names chosen for the arguments, so he just picks his own parameter names that will serve to stand in for the arguments. • The return Statement: • The returnstatement consists of the keyword return followed by an expression. The value of the expression is what is returned to the statement that called the function. In other words, the value of the expression after the return keyword is the function’s output. The function ends when the return statement executes. NOTE: the number of arguments and parameters should be the same…

  6. Functions Functions: Good practice in writing code is to break it up into small manageable pieces (subprograms) and then test these subprograms. If the subprogram functions correctly then you will move on to the next subprogram. Theses subprograms are typically referred to as functions. Function Call: To use a function we must first use a function call. The function call specifies: • The function name • The function arguments (the inputs to the function) Function Call Syntax: variable = function_name(argument_1, …., argument_x); function_name (argument_1, …., argument_x); Void Functions: Functions that produce no values for the rest of the Program to use are called void functions. Why would we use this? An example would be to maybe send a comment to the display and we don’t need any feedback from this. The value that a function computes is termed the RETURN VALUE . This return value is the output of the function and there can only be, at most, ONE output for each function (sometimes a function has no return value)!

  7. Functions 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  8. Functions and Main Memory 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 } COMPILE COMPILE

  9. Return Address 1 #include <stdio.h> 2 intaddition( int X, int Y ) 3 { 4 intZ = X + Y; 5 return Z; 6 } 7 8 intmain() 9 { 10 intA = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  10. Functions and Main Memory 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 } COMPILE COMPILE Before we can CALL the function, we must… Place the FUNCTION ARGUMENTS on the Stack Place the RETURN ADDRESS on the Stack Place the PRIOR EBP on the Stack

  11. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  12. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  13. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  14. Return Address 1 #include <stdio.h> 2 intaddition( int X, int Y ) 3 { 4 intZ = X + Y; 5 return Z; 6 } 7 8 intmain() 9 { 10 intA = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  15. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  16. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  17. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  18. Functions and Stack 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  19. Debugger 1 #include <stdio.h> 2 int addition( int X, int Y ) 3 { 4 int Z = X + Y; 5 return Z; 6 } 7 8 int main() 9 { 10 int A = 5, B = 3, C; 11 C = addition( A, B ); 12 printf("\n%d\n\n", C); 13 }

  20. Debugger cont.

  21. Function Example Suppose you are given a function named sqrt, which is used to determine the square root of some number. y = sqrt ( x ); • What we know from this function: • The function name is sqrt • The x is the function argument (or the input) • The function call is sqrt ( x ) • The value computed by the function (the output) is the Return Value, which gets placed into the variable y • i.e. if x = 9, then the function output would be 3 therefore y = 3

  22. Function Example parameterdata_type #include<stdio.h> intAbsVal( int number) { int AV; if ( number >= 0) AV = number; else AV = -1*number; return AV; } int main ( ) { int x, y; printf (“Enter an integer: ”); scanf (“%d”, &x ); y = AbsVal ( x ); printf (“The absolute value of the integer is %d\n”, y); } intAbsVal( int number) parameter (This is place holder for the argument) return data_type return value Main Function function call y = AbsVal ( x ); argument function name

  23. Void Functions Void Functions: Functions that produce no values for the rest of the Program to use are called void functions. Why would we use this? An example would be to maybe send a comment to the display and we don’t need any feedback from this. For example the code on the previous slide uses a printf statement in main which produces no value for the Program to run. Its only purpose is to display a message to the monitor. #include<stdio.h> intAbsVal( int number) { int AV; if ( number >= 0) AV = number; else AV = -1*number; return AV; } void output ( intAbs_Num ) { printf (“The absolute value of the integer is %d\n”, Abs_Num); } int main ( ) { int x, y; printf (“Enter an integer: ”); scanf (“%d”, &x ); y = AbsVal ( x ); output( y ); //without the function call here output would never be accessed } Note that there are now 3 functions associated with this program (main, AbsVal and output) Note the new function named output. This function does not return a value to main, it simply displays the integer Abs_Num to the screen Key differences of a void function: 1) Keyword void is used instead of a return type. 2) There is no return value from the function; therefore the return statement is omitted. 3) The function call is not used as the right side of an assignment statement.

  24. The Main Function • In reality, you were introduced to functions basically in the first lecture. • The line int main ( ) is a function. It just happens to be the function where ALL programs begin executing the program.

  25. The Stack A Program in Memory: • Let’s think about what happens when a program is loaded into memory. Recall that the source code that we write is translated into machine language instructions, and these machine language instructions are fetched, decoded, and then executed, one-by-one. • The program itself must reside in main memory. • When the operating system executes a program, it allocates a block of memory for the machine language code that comprises the program. This section of memory is termed the text segment. • When the program is placed in the text segment, additional memory is given to the program to hold the values that it needs to successfully execute (e.g., values of variables). As we have mentioned, this section is called the stack.

  26. Functions and the Stack Source Code Statement Address 0x080483b4 #include<stdio.h> intAbsVal( int number) { int AV; if ( number >= 0) { AV = number; } else { AV = -1*number; } return AV; } int main ( ) { int x, y; printf (“Enter an integer: ”); scanf (“%d”, &x ); y = AbsVal ( x ); printf (“The absolute value of the integer is %d\n”, y); } 0x080483be 0x080483c4 0x080483cf The Stack 0x080483d6 0x080483da 0x080483e5 0x080483e9 0xbffff7ba 0x080483f7 0xbffff7be 0xbffff7de 0x080483fb 0x08048404 0xbffff7c8 0x0804846a 0x08048408 ebp_main esp_main 0xbffff7d2 esp_main 0x08048414 esp_AbsVal ebp_AbsVal 0x0804841e Main Function 0xbffff7d6 0x0804842b 0xbffff7da 0x0804842f 0x08048438 0xbffff7de 0x08048441 0x0804844d 5 number AV AV y y Garbage Garbage -5 5 5 0x08048458 0x0804846a x x Garbage -5 0x0804846e

  27. More on the Stack More on Memory: In a Program each function that is called gets its own section on the stack to work with, called a stack frame. In our example there were two stack frames. One was for the Abs_Val function ( 0xbffff7ba ) and the other was for main (0xbffff7be  0xbffff7da ). The stack frames are locations in memory that functions, including main, can store values of variables. Machine Language code (Instructions) Text Segment Stack Frame (Abs_Val) Program Stack Values of Variables 0xbffff7de 0x0804846a Stack Frame (Main) number AV y -5 5 5 x -5

  28. More on the Stack The stack also holds the key values to help the CPU ‘not get lost’ when running the function. • The Main’s Variables holds the values for the variable declared in the main function (in this case it was the user input value for x and the calculated value for y, which came later) • The Function Arguments are the arguments from the function call (the value here was from x (-5) placed into variable number– remember number is actually the function parameter but it acts as a placeholder for the value of -5 in this case) • The Return Address is the address for which the CPU will ‘go back’ to after the function has executed • The Saved Value of Prior ebpis the address reminding the CPU of the confines of the stack • The Function Variables (AV in the example) are those used in the function itself Function’s Variables Saved Value of Prior ebp 0xbffff7de Return Address 0x0804846a Function Arguments Function Stack Machine Language code (Instructions) Text Segment Main’s Variables Program Stack Values of Variables number AV y -5 5 5 x -5 Main Stack

  29. Practice Problem 6.4 Place the following elements in the order the will appear (from bottom to top) on the stack during a function call from main (while executing the instructions for that function). • Return Address • main’s Variables • Function’s Variables • Saved value of prior ebp • Function’s Arguments Function’s Variables Saved Value of Prior ebp Return Address Function Arguments Main’s Variables

  30. Practice Problem 6.5 part (a) • Given the following source code and debugger output, construct the stack frame for the function main in the diagram below part b. Show where the base pointer (label as EBP-Main) and stack pointer (label as ESP-Main) are pointing to, and show where the arguments to exam_function are stored in memory. #include<stdio.h> void exam_function( int x, int y, int z) { intsome_class; intbest_class; intmy_class; best_class= x; my_class= z; some_class= y; } intmain() { exam_function( 2005, 2003, 2015 ); } Disassemble Main Function:

  31. Practice Problem 6.5 part (b) (b) Using your answer from part a), and the additional debugger output below, construct the stack frame for the function exam_function. Show the location of the base pointer (label as EBP-Exam) and stack pointer (label as ESP-Exam) on the figure. Note on your figure: • the location of best_class, some_class, and my_class • the location of the return address • the location of the prior value of the base pointer (EBP-Main) Disassemble Exam_Function:

  32. Practice Problem 6.5 - Answers Function Stack 0x7DF (2015) my_class; ESP-Exam best_class 0x7D5 (2005) Function Variables some_class 0x7D3 (2003) 0xBFFFF818 (this is ebp_main) EBP-Exam (old_EBP) Return Address (Leave cmd; main function) 0x0804838A ESP - Main 0x7D5 (2005) 0x7D3 (2003) Function Arguments 0x7DF (2015) None for this program Main’s Arguments EBP - Main Main Stack

More Related