Zhu Yuxiang + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000
As we all know, the current computer is mainly to follow the so-called "von Neumann framework". What is the von Neumann framework, in fact, is the computer through the bus from memory to read the program and data, stored in their own registers in a line to execute, as shown in.
Today, we will discuss the process of computer work by compiling a specific C program.
First write a C program like this:
1 //linux.c2 intg (x)3 {4 returnx+3;5 }6 intf (x)7 {8 returng (x);9 }Ten intMain () One { A returnFTen)+1; -}
In the Linux environment, enter the following command:
gcc –s–o linux.s linux.c-m32
Then open the LINUX.S and you can see the code we compiled (directly on)
will be inside with "." The line at the beginning is removed (this is for linking) and the compiled code is obtained:
1 g:2Pushl%EBP3MOVL%esp,%EBP4Movl8(%EBP),%eax5Addl $3, %eax6Popl%EBP7 ret8 F:9Pushl%EBPTenMOVL%esp,%EBP OneSUBL $4, %ESP AMovl8(%EBP),%eax -MOVL%eax, (%ESP) - Call G the Leave - ret - Main: -Pushl%EBP +MOVL%esp,%EBP -SUBL $4, %ESP +MOVL $Ten, (%ESP) A Call F atAddl $1, %eax - Leave -Ret
Next, we will analyze the process of changing the procedure.
At the beginning of the program, the IP register of the CPU points to line 18th of the Assembly code, assuming that the stack in memory address is 0,1,2,3 ... Both the stack base pointer Register (EBP) and the stack top pointer register (ESP) point to stack segment 0.
Line 18th to 21st first opens up a new memory area for the main function, and then passes the argument 10 into the stack, where the stack segment looks like this:
Then the program calls the call function, the IP into the stack, the IP points to the code line F 9th.
At the code of the F function, we first open a new area of memory for the F function, then assign the passed parameter 10 to EAX and put the eax into the stack, where the stack segment memory is as follows:
The program calls call here to enter the G function. In the G function, it also first opens up the memory space, then passes the parameter to EAX, and adds the value of EAX to 3.
After that the EBP is out of the stack and the RET command is called. At this point the IP is re-pointing to the command after the F function call, the stack memory is as follows:
After that, we constantly call the leave and RET commands, jump out of the current memory area, go back to the memory area of the upper function, and add the value of EAX to 3 until you jump out of the main function, and the program ends.
From the above analysis, I think the following points can be summed up:
1. The running process of the computer is to follow the von Neumann framework, the CPU reads the code and data in memory into its own registers, and then makes further operation according to a command call register.
2. Before entering each program, the CPU will stack the EIP and EBP on the previous level, which is equivalent to re-opening up a new memory space for the new function, until the function is exited.
Each of the 3.CPU registers has a different division of labor, such as an EIP pointing to the code to be executed, EAX storing the return value, and so on. They run through the entire process of program execution, and generally do not easily change when you write programs.
See how the computer works from the assembly of a piece of code