See what kind of assembly code the GNU compiler has generated

Source: Internet
Author: User

At ordinary times the work of the Assembly, the emergence of a moment, I would like to see the GNU compiler generated by the assembly code is what.

1. Generate assembly Code

I wrote a very simple C language code, as follows

Then, execute thegcc-s SIMPLE_PROGRAM.S simple_program.c"Generate assembly code (SIMPLE_PROGRAM.S)。

The following is a commented assembly code, which is not commented out by the compiler generated assembly code.

2. Compilation of knowledge, say a few more points

In the assembly code, I added a lot of comments. Some questions, I think the comment is not clear, here concise two points.

2.1 In the Assembler programsegmentProblem

In assembler, different data is placed in different segments.

The code is placed in the code snippet, which is in the . Text field.

The data is placed in the data segment, and the data segment has the . data segment and the . BSS segment. The global variable that assigns the initial value is placed in the . Data segment, and global variables that do not have an initial value are placed in the . BSS segment.

2.2 Function call issues in assembler

The assembly code is generated automatically by the GNU compiler. Although it is automatic, there must be an agreement idiomatic the norm is working, or not a mess.

The specification of a function call, perhaps you have seen a bit of a clue from the assembly code.

Let's talk about the function parameters first. Function parameters are passed through the stack space.

Before calling the function, the function parameter is put into the stack, the order of the stack is followed by the function parameter n, the function parameter n-1, ... function parameter 1.

After the assembler executes call function_name, the assembler automatically presses the return address of the function, which is the command address of the next line of call Function_name, into the stack.

In the assembler function, in order to ensure that the return address is not accidentally changed, the EBP is pressed into the stack, specifically to save the value of ESP with ebp .

Thus, in the assembler function, the function parameter 1 is stored in the location of ESP+8, the function parameter 2 is stored in the location of the ESP+12, and so on. ESP+0 holds the value of EBP , andesp+4 holds the return address of the function.

The return value of the assembly function is entered in the Register EAX .

Local variables are also implemented through stacks.


The article was written in brief. If there is any ambiguity. Please forgive me.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

See what kind of assembly code the GNU compiler has generated

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.