[The first week of Linux kernel analysis] is a compilation of the C language program that observes the interaction of CPU registers with memory

Source: Internet
Author: User

Monensin "Linux kernel Analysis" first week experiment

Zou Le

Original works reproduced please indicate the source.

Course Information:

"Linux kernel Analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

---------------------------the body of the experiment---------------------------

This experiment is carried out under the 64-bit liinux virtual machine in the experimental building.

The C code is as follows:

int increment5 (int  x) {  return5;} int Solve (int  x) {  return2;} int Main (void) {  return solve;}

C Program execution logic is: Take the main function as the entrance, call the solve function, the literal value 2017 as the parameters of the solve function, in the solve function to execute the call increment5 function, increment5 function accept 2017, and add 5, Returns the solve function minus 2, and finally returns to the main function to return the value.

The assembly code MAIN.S by the-s and-m32 parameters in the same directory will assemble the code with '. ' The code that begins with the row deletion is as follows:

INCREMENT5:PUSHL%ebp movl%esp,%EBP movl8(%EBP),%eax Addl $5,%eax popl%ebpretSolve:PUSHL%ebp movl%esp,%EBP pushl8(%EBP)Pagerincrement5 Addl $4,%esp Subl $2,%eaxLeave    retMain:PUSHL%ebp movl%esp,%EBP PUSHL $ .    PagerSolve Addl $4,%espLeave    ret

The experiment is as follows:

In the third chapter of the textbook Csapp, the part of the stack that the memory allocates for a single process (function) is called the stack frame. The top stack frame is defined by two pointers,%EBP points to the bottom of the stack,%ESP to the top of the stack, each push command will let%esp minus 4 (the In-memory stack grows downward), and then writes the value at the new address. The pop reads the value of the current%ESP position before subtracting the stack by 4.

Description of the change in memory stack----------------program Runtime---------------

First, the code with the line number is given as a benchmark.

This Assembly code performs the following procedures:

You can observe that every time you call a function, the command executed first is

PUSHL%EBP

MOVL%esp,%EBP

If the main function entry in memory, both ESP and EBP point to the No. 0 grid. What these two lines of code do is to save the information in the No. 0 grid to the 1th, then move the origin of the stack to the 1th grid, with the 1th lattice as the bottom of the process.

Line 20th, the thing to do is to write a literal value of 2017 to the 2nd grid, when EBP points to the 1th grid, esp points to the 2nd grid.

Line 21st, the call command writes the EIP=22 command to the 3rd grid, and then makes eip=9.

Lines 9th and 10th, enter the solve function, first create the stack frame: the main function of the bottom of the stack information (bottom =1) saved to the 4th grid, and then the 4th lattice as the bottom of the process. Notice that at this point 8 (%EBP) points to the 2nd grid, which is the parameter of the solve function. For a single-parameter function, often after entering the new function, its argument is located in the position of 8 (%EBP),%EBP itself saved the last (to return the function) of the bottom of the information, and then a second to save the EIP pointer to return the position.

On line 11th, the ESP is incremented by four, and in the 5th it writes the literal value 2017.

Line 12th, the call command saves eip=13 to the 6th grid, and then makes eip=2.

Lines 2nd and 3rd, create the stack frame, save the stack bottom information of the Slove function (bottom =4) to the 7th grid, and then use the 7th grid as the bottom of the function. 8 (%EBP) then points to the value of the 5th grid, which is 2017.

Line 4th writes the value 2017 of the 5th grid to the EAX register.

Line 5th, increase the value in EAX by 5.

Line 6th, the information of the 7th grid (bottom =4) is assigned to EBP, that is, the EBP points to the 4th grid, and the ESP fallback 1, pointing to the 6th lattice.

The 7th line, the meaning of RET is popl%eip, that is, esp points to the 6th lattice, the content of the EIP point 13. This command modifies the EIP to point to line 13th and causes ESP to fall back to the 5th grid.

In line 13th, the ESP is then rolled back 1, pointing to the 4th grid. (At this point the stack top and the stack are low are 4th grid). Notice that each call command will be followed by a Addl $4,%esp. In this example, a single function parameter is returned for the call function.

Line 14th, minus 2 in%eax.

Line 15th, leave instructions. The semantics of the leave directive are first MOVL%EBP,%esp, then Popl%EBP. The information stored in our%EBP is the bottom of the main function (1th), and the effect is to let%EBP point back to the bottom 1 of the main function, and the ESP points to the 3rd grid (eip=22).

Line 16th, ret. This merriness EIP returns to the main function, the 22nd line of the program, and the ESP goes back to the 2nd grid.

Line 22nd, return the ESP to a grid and return to the 1th grid.

The 23rd line, the bottom of the stack equals 0, and the top of the stack equals 0.

The 24th line of the program ends, and the final result is saved in%eax.

The specific memory information and EBP, ESP changes are as follows:

The above process shows that compared to high-level languages such as C language, assembly language has a large part of the cumbersome underlying function calls, the value of the information. As we call the function calls stack, it is done by saving the parent function to continue executing the address and the parent function after the original stack address, create a new bottom of the stack.

Through this experiment, I have a "feeling" for the 8 (%EBP) and Leave+ret in the 32-digit compilation, and I feel the importance of the abstract work of C language relative to the compilation, and also have a preliminary understanding of the machine-level representation of the program.

[The first week of Linux kernel analysis] is a compilation of the C language program that observes the interaction of CPU registers with memory

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.