C Language Program stack call (detail, diagram) __c language

Source: Internet
Author: User

before contact with the program, only know the program to write the right, generally can run out, but do not know how the program is step-by-step to compile each step of the link up, today we use the assembly to see how the program in the end is how to run in the program.

#include <stdio.h>
int Add (int x,int y)
{
	int sum = 0;
	sum = x+y;
	return sum;
}
int main ()
{
	int a = 2;
	int b = 3;
	int ret = ADD (a,b);
	printf ("%d\n", ret);
	return 0;
}
	


We write the program is to write the main function first, know that the main function is a system function, direct call on the line. However, the main function is actually called by three other functions, and the program in turn opens up a block of memory for the main function and then accesses the data in the form of a stack on the memory of the program we write. In this way, our team's call to the program can be clearly understood.




in the process of debugging, we go to the disassembly to write to see. After calling the main function, the the system first presses the EBP (stack bottom pointer) and ESP (the stack top pointer) into a stack (the Run-time stack, also called the stack frame), then the ESP and the 4cH want to subtract (that is, move the ESP pointer up 4c space size), and then press the Ebx,esi,edi separately to

in the stack, after Le Ah, Mov,mov three, the initial space of the main function is populated with 13h (bits 19) cc




To get a better idea of the stack creation and destruction, let's illustrate it graphically


when it's Ebx,esi,edi. After the three registers are pressed into the stack, after the 13h size space is initialized to CC, the ESP pointer moves up to the EDI, at which point the main function call ends. The program then goes down to the real main program, performing int a=2, int b=3, and see how the a=2 and b=3 in memory are stored .


have you noticed that just the EBP pointer is 0018ff3c, now 02 and 03 are just above the EBP pointer, once we can imagine that a program is stored first from the bottom of the stack, up and down



and then the program goes down into the function section.


00401020   push        ebp
00401021   mov         ebp,esp
00401023   Sub         esp,44h
00401026   Push        ebx
00401027   push        esi
00401028   push        EDI
00401029   Lea         EDI,[EBP-44H]
0040102C   mov         ecx,11h
00401031   mov         eax,0cccccccch
00401036   Rep STOs    dword ptr [edi]
4:        int sum = 0;
00401038   mov         dword ptr [ebp-4],0
5:        sum = x+y;
0040103F   mov         eax,dword ptr [ebp+8]
00401042   add         eax,dword ptr [ebp+0ch]
00401045   mov         dword ptr [Ebp-4],eax
6: Return        sum;
00401048   mov         eax,dword ptr [ebp-4]
7:    }
0040104B   pop         edi
0040104C   Pop         esi
0040104D   pop         ebx
0040104E   mov         esp,ebp
00401050   pop         EBP
00401051   ret


We find that when we call the Add function, we start with the main function as a first space, then we push the EBP and ESP into the stack frame, and then we initialize the 11h space to CC, when the bottom pointer and the top of the stack pointer are changed.


then came the fun time, first execute int sum = 0; The system allocates sum4 bytes of memory from the EBP to 0, then executes int x+y, looking at the corresponding assembly language, which adds ebp+8 and ebp+0ch together and assigns it to ebp-4. And we know that the memory that the program just allocated to sum = 0 is ebp-4, and ebp+8h and ebp+0ch are just the positions of a and B parameters. This means that the system adds the parameter a,b and places it in the memory pointing to sum.


The program is here, the stack is created, and the next step is to return the result of executing in the function to the pointer just created in the main function, and continue with the assembler.

0040104B   pop         edi
0040104C   pop         esi
0040104D   pop         ebx
0040104E   mov         esp,ebp
00401050   pop         ebp
00401051   ret

Down is the destruction of the stack that just created the function, first the pop (pop-up stack) edi,esi,ebx the three registers, and then the EBP to ESP, that is, to take back the space just above, and then jump back through RET to just call the function at the main function (onsite protection).

back to the call is the execution of add esp+8, which means that the top pointer of the stack is moved downward by 8 bits, which means just skipping the position of the holding parameter


  


after a compilation of the program so a walk, we saw that the value of the program A+b in the function, passed to the initial ebp-0ch, just above the argument B (the RET points to the memory), then printed out with the printf function, and then executed the step that just destroyed the add function. The stack creation and destruction of the entire function is done.


to tidy up the above steps is:

1, first to open space for the main function

2, the register and actual parameters are pressed to the stack

3, open up a form of parameter space, and then the actual parameters to the formal parameters

4, the Add function to open up a space, the register and function of the program into this piece of space

5, the parameters of the first operation, the results of the calculation in the space of the function

6, pass the result to the main function, and print out

7, the destruction of the Add function

8, the destruction of the main function

Related Article

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.