Principle of C pointer (87)-Analysis of C program assembly in helloworld (3)

Source: Internet
Author: User

". The memory marked by LC0 is located in the static allocation Area of the application. This area is allocated after the program runs, that is, "hello, world" is arranged as a C-language String constant in the static allocation area.

Another very important memory allocation area is the stack, which is a special memory area for temporary access to function transmission parameters and data in the program, it is usually the memory area at the end of the application's memory range. To facilitate the access to stack data, there is a stack pointer (stack top pointer) pointing to the next memory location in the stack, this means that the top pointer of the stack does not adopt any offset address mechanism (the offset address can be adjusted with the base address as the center. For example, when accessing a variable, the base address of the variable is 0x400, if the offset address is 0x16, the final address of this variable is 0x416), you can only access the variables stored in the internal stack in the order of first and last.

Put data into the stack in assembly language, use pushl enable, and pop up the data from the stack, use popl enable, every time stack data is put and popped up, the top pointer of the stack changes, because the top pointer always points to the next available address in the stack. The following Assembly completes the process of Pushing 10 to the stack and then popping 10 to the ebx register.

Pushl $10

Popl % ebx

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

(2) C Program Execution

The source code of C language is translated into several lines of assembly code, which is composed of several simple commands to generate a binary file. This binary file is executed and helloworld is executed.

Movl, addl, and subl

Movl completes data replication, while addl completes data addition and subl completes data subtraction.

The syntax format of these three Enis is:

Target data of the source data

For example, the assembly code for this static variable allocation:

Myvalue:

. Long 190

Mess:

. Ascii "hello"

Using addl and movl, you can add the long type variable 190 indicated by myvalue to 100, and then subtract 20.

Movl myvalue, % ebx

Addl $100, % ebx

Subl $20, % ebx

Movl % ebx, myvalue


The assembly language code is placed in the. text section and analyzed in the above helloworld disassembly code section:

. Text

. P2align 4, 15

. Globl main

. Type main, @ function

The globl command specifies the main function as the entry function (the function executed at the startup of the Program), and then defines the composition of the main function:

Main:

Leal 4 (% esp), % ecx

Andl $-16, % esp

Pushl-4 (% ecx)

Pushl % ebp

Movl % esp, % ebp

Pushl % ecx

Subl $4, % esp

Movl $. LC0, (% esp)

Call puts

Movl $0, % eax

Addl $4, % esp

Popl % ecx

Popl % ebp

Leal-4 (% ecx), % esp

Ret

. Size main,.-main

. Ident "GCC: (GNU) 4.2.1 20070831 patched [FreeBSD]"

. Section. note. GNU-stack, "", @ progbits

Observe the assembly code, which is filled with comments such as pushl, popl, movl, subl, and addl, in the end, C Programs are executed through simple operations such as replication, inbound stack, outbound stack, addition, and subtraction. Observe the following lines in the Code:

Leal 4 (% esp), % ecx

Andl $-16, % esp

Pushl-4 (% ecx)

Pushl % ebp

Movl % esp, % ebp

Pushl % ecx

Subl $4, % esp

Movl $. LC0, (% esp)

Call puts

The print ("helloworld") Output string of the C statement is implemented through the preceding lines. Besides the last line of call puts (call Command), the function of calling the puts function of C language to output the string is completed, the puts function outputs a string to the terminal. The unique parameter is char * str, which indicates the string to be output, all the work done by other rows is to call the unique parameter of the puts function (pointing to the string "helloworld" address identifier ". LC0 ") into the stack for calling the puts function, the second to last row. copy the address marked by LC0 to the top of the current stack, allocate the stack in the first few lines, and adjust the top pointer of the stack, add the registers to the stack (because calling the puts function will destroy the values of the existing registers, which is called the storage field). After the puts function is complete, the register values in the stack will be popped back to their respective registers (called recovery field ).


Related Article

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.