Step-by-step write algorithm (function stack display)

Source: Internet
Author: User

Original: Step-by-step write algorithm (function stack display)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Before continuing with the discussion of the diagram, let's open a deserted today and discuss the fundamentals of the function stack. Friends who have had programming experience know that stack debugging is a feature that we often apply in program development. So have you ever thought about how the function stack started? In fact, we can write a function stack output function analysis.

As a general rule, the stack process for a function is this:

| Parameter Three |

| Parameter Two |

| Parameter One |

| address|

| EBP |

|     Variable | <---------------------------------EBP

So how does the content in the stack print?

void Stack_print () {int var_ebp;__asm mov var_ebp, ebp;printf ("%08x\n", * (int*) (VAR_EBP) + 1));}
The above code just prints the return address of the current function, so what if a continuous function prints? Print to the main function to begin.

void Stack_print () {int var_ebp;__asm mov var_ebp, ebp;do{        printf ("%08x\n", * ((int*) (VAR_EBP) + 1));        VAR_EBP = * (int*) (VAR_EBP);} while (VAR_EBP! = 0x0);}
The above code can see a pair of addresses, then how to match these addresses and function names, then you can only check the table. Where does the table for the function correspond? No hurry, looked at the following one, friends will understand what is going on.


Everybody so in VC compile time put generate Mapfile Select, can generate corresponding *.map file. The file contains the starting address of the main function in the current file and is sorted from low to high. So just look for the corresponding function to start, to determine if our function return address is in the middle of this function, then we can find the corresponding function name.


Summarize:

(1) Today summarizes the basic principles of function stack display;

(2) After knowing the basic principle of the function, it is convenient for us to understand many problems in essence. Even though many CPUs are handled differently and X86, we can quickly grasp them in a similar way;

(3) Stack principle is very important, friends should have a good look.



Step-by-step write algorithm (function stack display)

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.