function invocation principle--Stack frames

Source: Internet
Author: User

Function call procedure------ Stack frames

Example: anatomy The principle of "comparing the size relationship between two numbers and returning a larger number":

Int max (int x, int y) {                  int z = 0;                  if  (x > y)                                   z = x;                  else                                  z = y;                  return z;} Int main () {                 int a = 10;                  int b = 20;                  int c  = 0;                 c = max (a, b);                 printf (  "%d\n",  c);                 system (  "pause");                  return 0;}

Analysis:


Figure One:

The main function is called by mainCRTStartup, so the ebp+1 of the main function stack frame is the mainCRTStartup stack frame EBP:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/1B/wKiom1cT6U7Sl1MRAABNCoZb8NY211.png "title=" Image.png "alt=" Wkiom1ct6u7sl1mraabncozb8ny211.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Observe the esp=0018ff4c of mainCRTStartup ebp=0018ff88


Figure II:

The following figure is the process of mainCRTStartup calling main and the creation of the main function stack frame:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s4.51cto.com/wyfs02/m02/7f/1b/wkiom1ct6xajooteaahuolxfpe0636.png "title=" figure A. png "alt=" Wkiom1ct6xajooteaahuolxfpe0636.png "/>

Figure II:

This diagram is the creation of the Max function stack frame:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/19/wKioL1cT6oywmVssAAEjx1svjJw322.png "title=" figure two. png "alt=" Wkiol1ct6oywmvssaaejx1svjjw322.png "/>

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Might

This image is the destruction process of the max stack frame after the Max function call is completed:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7F/1B/wKiom1cT6ebDKUt3AAJWnw5NHrs266.png "title=" figure three. png "alt=" Wkiom1ct6ebdkut3aajwnw5nhrs266.png "/>


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>

Throughout the execution of the program, the function of the stack frame can be abstracted as, notice that the stack is from the high address to the low address walk:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/7f/1b/wkiom1ct6frgjwaraaa4hplzaey107.png "title=" stack frame 1.png "alt=" Wkiom1ct6frgjwaraaa4hplzaey107.png "/>

Because the stack is aligned, the compiler allocates more memory, which is wasted.


This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1764882

function invocation principle--Stack frames

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.