Machine-level representation of the C program-3

Source: Internet
Author: User

/*************************************** ******/
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.

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.