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)