function Call stack

Source: Internet
Author: User

Go from http://www.cnblogs.com/rain-lei/p/3622057.html function call everyone is not unfamiliar, the caller passes some arguments to the callee, then executes the callee's code, and finally the caller returns the result to the caller. There are people familiar with the sentence, that is, the function call occurs on the stack, then in the computer inside exactly how to achieve it? For the program, the compiler allocates a memory for it, logically divided into code snippets, data segments, heaps, stack code snippets: Save program text, instruction pointer EIP is point to code snippet, can read executable non-writable data segment: Save the initialized global variables and static variables, Readable writable non-executable BSS: Uninitialized global variables and static variable heap (heap): Dynamically allocating memory, growing in the direction of increased address, readable writable executable stack (stack): Storing local variables, function parameters, current state, function call information, etc., to reduce the direction of the address of the growth, very very important , Readable writable executableRegister DeviceEAX: Accumulate (accumulator) register, commonly used for function return value EBX: base register, which accesses memory for base address ECX: Counter (Counter) register, commonly used as the counter in string and loop operation edx: Data register, Commonly used in multiplication and I/O pointers esi: source Change Register DSI: Destination register ESP: stack pointer register, pointing to Stack top EBP: Base point pointer Register, pointing to current stack bottom EIP: instruction register, pointing to the address of the next instructionSource Code
int print_out (int begin, int end) {printf ("%d", begin++); int *p; p = (int*) (int (&begin)-4); if (begin <= end)  *p-= 5; return 1;} int Add (int a, int b) {return a+b;} int Pass (int a, int b, int c) {char Buffe R[4] = {0}; int sum = 0; int *ret; ret = (int*) (buffer+28); (*ret) + = 0xA; sum = a + B + C; return sum;} int main () {print_out (0, 2); printf ("\ n"); int a = 1; int b = 2; int c; c = Add (A, b); Pass (A, B, c); int __sum; __asm {
   mov __sum, eax} printf ("%d\n", __sum); System ("Pause");}

Initialization of functions
  28:int Main ()    : {011c1540 push EBP//Stack, save EBP, note that the push operation implies esp-4011c1541 mov ebp,esp//Pass the value of ESP to EBP, set the current ebp011c1543 Sub esp,0f0h//Open space for function, range is (EBP, ebp-0xf0) 011c1549 push ebx011c154a push esi011c154b push edi011c154c Lea edi,[ebp-0f0h]// Value EDI to ebp-0xf0011c1552 mov ecx,3ch//Function space DWORD number, 0xf0>>2 = 0x3c011c1557 mov eax,0cccccccch011c155c rep stos DWORD ptr Es:[edi] The purpose of the//REP directive is to repeat the instructions above. The value of ECX is the number of repetitions. The role of the//stos directive is to copy the value in EAX to the address pointed to by Es:edi, and then edi+4

This command is used at the beginning of a general function, where the state register is saved, the stack register is saved, and the function memory space is initialized.function Call
30:print_out (0, 2); 013d155e push 2//second argument stack 013d1560 push 0//First argument stack 013d1562 call Print_out (13d10fah)//return address stack, in this case 013D 1567, then call print_out function 013d1567 add esp,8  //Two arguments out//note in the call command, the implied action is to stack the address of the next instruction, the so-called return address

The initialization of Print_out is exactly the same as the initialization of the main function except that the VS may add some security checksThe called function returns
013D141C mov eax,1  //return value incoming eax in 013d1421 pop edi   013d1422 pop esi   013d1423 pop ebx//register out stack 013d1424 add esp,0d0h The following 3 commands are called vs __rtc_checkesp, check stack overflow 013d142a cmp ebp,esp013d142c call @ILT +315 (__RTC_CHECKESP) (13d1140h) 013d1431 mov The value of the ESP,EBP//EBP is passed to ESP, which means that the value of the previous ESP is resumed 013d1433 pop ebp//pop EBP, restore EBP value 013d1434 ret  //return address written to EIP, equivalent to pop EIP

Call instruction implied operation push Eip,ret instruction implicit operation Pop EIP, two instructions completely correspond to write here we can analyze the main function called Print_out function before and after the stack (stacks) What happened, the following with a series of diagram descriptionThe next step is the return process, starting with the 013d1431 line code aboveBefore and after the Print_out function call, the stack frame of the main function is exactly the same, perfect! Now let's see what the Print_out function does.
int *p;p = (int*) (int (&begin)-4), if (Begin <= end)  *p-= 5;
According to the above call print_out function, you can know that P is actually pointing to the function of the return address addr, and then put addr-5, what happens? And look back at the disassembly code,
013d1560 Push 0//First argument stack 013d1562 call Print_out (13d10fah)//return address stack, in this case 013d1567, then print_out function 013d1567 Add esp,8  //Two actual arguments out of stack
The analysis shows that the value of the return address addr is 013d1567, addr-5 is 013d1562, the return address points to the call instruction, and the result is that the Print_out function is called again, so that the Print_out function implements all the numbers printed from begin to end , it can be said that the loop called the Print_out function for the Add function, mainly to show that the return value is stored in the register EAX. In addition, VS itself will provide some security checks Checkstackvar security checks http://blog.csdn.net/masefee/article/details/5630154, pass parameters through ECX and edx, local variables have arrays when used __security_check_cookie returns an address check that uses the __RTC_CHECKESP stack check when the array length is greater than or equal to 5, using the printf function

function Call stack

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.