/*************************************** ******/
Text: Machine-level representation of the C program
Author: Arden Chao
Date: 2006-10-17
Email: arden1019@gmail.com
Version: 1.0.0
/*************************************** ******/
////////////////////////////////
// Memory space of the 2006-10-24 function <2>
////////////////////////////////
Then yesterday.
Our program, when calling _ fun, we have such a function stack in the process memory space. What should we do with this function stack?
What is the function? Let's think about this issue. Call a function in Main. after entering the function body and completing a series of function code
The next statement in main is executed. Therefore, when calling a function, we need to save the address of the code to be executed after the function is returned.
To save the parameters of the input function.
Let's look at the fun code:
_ Fun:
Pushl % EBP; Monitoring pointer pressure Stack
Movl % ESP, % EBP; stack pointer (save stack top address)-> detection pointer
Subl $4, % ESP; GCC habits
Movl 8 (% EBP), % eax; A-> % eax
Movl % eax,-4 (% EBP); % eax-> localvar
Movl $10, 12 (% EBP); 10-> B
Movl $1, % eax; return 1;
Leave; expand to: mov ebp esp pop EBP detection pointer out Stack
RET; expanded to: Pop EIP next instruction address
When we enter fun, we save the endpoint address in % EDP. When we return, we will assign it to % esp. In this way, the function Stack pointer
Then we went back to the original location.
| ---------------- |
High address |
| ---------------- |
|
| ---------------- |
| B |
| ---------------- |
| A |
| ---------------- |
|
| ---------------- |
EBP |
| ---------------- |
ESP | localvar |
| ---------------- |
|
Low address | ---------------- |
We put Fun in main:
...
Movl $30, 4 (% ESP)
Movl $20, (% ESP)
Call _ fun
Movl % eax,-4 (% EBP)
Movl-4 (% EBP), % eax
Leave
RET
Before call _ fun, we have saved the parameters to a proper location.
After fun returns, the function is returned from % eax.