This article for the purpose of the completion of the work, but also summarize their own learning experience, consolidate the results of learning. is a completely real work process. Please keep the following information if you want to reprint:
Chen Tie + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000.
Today computers have become an important inseparable part of our lives, from portable handsets to supercomputers, most of which follow the von Neumann architecture: stored procedures, sequential executions. After the program is prepared, the input device is provided to the computer in order to execute. As long as a person can describe the problem that needs to be solved as a sequence of instructions that the computer can execute sequentially, the computer can give the corresponding result. So people have compiled computer language to describe the problem, modern computer language is divided into low-level language and high-level language, lower-level language closer to the machine, high-level language closer to human. In order to describe the working process of the computer, we use the assembly language, which is close to the machine, to describe the computer's execution process.
The experimental environment of the host operating system is WINDOWS7 64-bit, running VirtualBox 4.3.20 Edition, virtual machine installation CentOS7.0 64bit,linux kernel 3.10.0. GCC version 4.8.2,gdb version GNU GDB (gdb) Red Hat Enterprise Linux 7.6.1-51.el7.
The following is a description of the job process:
The 1.C language source code is as follows:
#include <stdio.h>
int g (int x) {
return x+2;
}
int f (int x) {
return g (x);
}
int main () {
Return F (7) +5;
}
2. Execute the command to Gcc-s main.s MAIN.C generate assembly code to facilitate our stepping analysis of the execution of the code. Execution gcc-g main.c-o main generates execution code that can be debugged with GDB. The following code is executed under the Linux terminal:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5A/36/wKioL1T6j-nBaT5KAAEImRuKXiQ934.jpg "title=" BYXC. PNG "alt=" Wkiol1t6j-nbat5kaaeimrukxiq934.jpg "/>
3. We can also get the right answer by analyzing the code flow:
The program executes from main, invokes the F function, and assigns the argument 7 to X. The F function calls the G function again, the X is 7 pass, the G function obtains the value of the parameter x is 7, returns 7+2=9 to the F function, the F function returns 9 to the main function, and the main function returns 9+5=14 as the execution result of the program. Under Linux terminal, 14 is saved in the system variable $?.
4. Computer systems We can simplify the abstraction to the CPU, memory, input and output several parts. Let's take a look at this program in my environment, how the computer is mechanically out of this 14. A program that exists in a function has a lot of memory stack operations, and simple addition operations are not introduced here, focusing on the operation of the stack. The following is the assembly code listed in Cat MAIN.S, which retains only the executable part.
G: Pushq%RBP Movq%RSP,%RBP Movl%edi, -4 (%RBP) Movl-4 (%RBP),%eax Addl,%eax POPQ%RBP Ret F: Pushq%RBP Movq%RSP,%RBP SUBQ $8,%RSP Movl%edi, -4 (%RBP) Movl-4 (%RBP),%eax MOVL%eax,%edi Call G Leave Ret Main Pushq%RBP Movq%RSP,%RBP MOVL $7,%edi Call F Addl,%eax POPQ%RBP Ret
|
(1) Execute GDB main into debugging, the L command can display the C language code. Break main sets a breakpoint so that the program stops at the beginning of the program and then steps through to see how the computer works. The Run command causes the program to start executing. Pushq%rbp;movq%rsp,%RBP, this time rip points to rip=0x40051a.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5A/3A/wKiom1T6nhXiPrWIAAD7JKsRU-c586.jpg "title=" Brrun. PNG "alt=" Wkiom1t6nhxiprwiaad7jksru-c586.jpg "/>
(2) Execute the info registers command to see the register. The stack pointer RBP and RSP point to the same address 0x7fffffffe550, indicating that the current program stack is empty. 650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5A/36/wKioL1T6n8HzxPK9AAKPTqpeevM709.jpg "title=" R1. PNG "alt=" Wkiol1t6n8hzxpk9aakptqpeevm709.jpg "/>
The assembly code then saves the stack's original pointer, and the operating system starts calling the main function. You can see the rip pointing to the next address where you want to execute the instruction.
(3) There is a function call, we execute the STEPI command in GDB. Execute MOVL $7,%edi.
(GDB) Stepi 0x000000000040051f return F (7) +5;
(gdb) Print $rip $ = (void (*) ()) 0x40051f <main+9> (gdb) Print $edi $ = 7
|
(4) Then the program passed to the function F 7 saved into the Register EDI, continue to execute, call the F function. Call F, the operation is the current rip=0x00400524 value stack (in GDB can perform the X-%rsp command view), Rsp-8,f function address into the RIP. The computer executes the Pushq $RBP in the F function, Movq%rsp,%rbp;subq $8,%RSP is actually a pointer to the main function before the F function is saved. At this time rbp=0x7fffffffe540,rsp=0x7fffffffe538, while (RBP) holds the top address of the stack stack before the call, of course, the top of the stack moves 8 bytes to accept the parameters of the descendant.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5A/36/wKioL1T6sJKBVs0QAABm80xKEpw225.jpg "title=" R2. PNG "alt=" wkiol1t6sjkbvs0qaabm80xkepw225.jpg "/>650) this.width=650; src=" Http://s3.51cto.com/wyfs02/M00/5A/37 /wkiol1t6w8hxb6qsaabq4dhchli438.jpg "title=" r0. PNG "alt=" Wkiol1t6w8hxb6qsaabq4dhchli438.jpg "/>
(5) Execute 3 times Stepi command, Movl%edi, -4 (%RBP), movl-4 (%RBP),%eax;movl%eax,%edi These three lines of instructions are clear, the parameters passed from main are stored in the stack space, and then saved through the EAX register, This is put into EDI and ready to be passed to the G function.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/37/wKioL1T6xMSRqXxEAAE0ROZPtco453.jpg "title=" R5. PNG "alt=" Wkiol1t6xmsrqxxeaae0rozptco453.jpg "/>
(6) Call G:rip value stack when calling G function; Rsp-8;g address is assigned to RIP.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5A/3B/wKiom1T6xM_Af9tcAAGql9M-r-A704.jpg "title=" R6. PNG "alt=" Wkiom1t6xm_af9tcaagql9m-r-a704.jpg "/>
Next execute two initialization instructions Pushq%rbp;movq%RSP,%RBP, after saving RBP, RSP becomes 0x7fffffffe528. Movl%edi, -4 (%RBP), movl-4 (%RBP),%eax;addl $,%eax, accepts incoming arguments, performs addition through EAX, and the result is saved in EAX. The G function then performs recovery processing, POPQ%rbp;ret,rsp points to 0x7fffffffe538.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5A/36/wKioL1T6t2ygiag8AAEwFi2z_00052.jpg "title=" R3. PNG "alt=" Wkiol1t6t2ygiag8aaewfi2z_00052.jpg "/>
(7) When the G function returns, the result is saved in EAX. Return to the F function code to continue execution. Where the leave instruction is equivalent to
Movq%RBP,%RSP
POPQ%RBP
After the execution of the program registers the following:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5A/37/wKioL1T6yeCR8m37AABhvAF94sQ270.jpg "title=" R7. PNG "alt=" Wkiol1t6yecr8m37aabhvaf94sq270.jpg "/>
rsp=0x7fffffffe548 after the RET instruction is executed
(8) After the completion of the F function call, return to the main function to execute ADDL $%eax statement. The results are saved in EAX.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5A/36/wKioL1T6uLPwsTMwAAEXKM8g8Cw505.jpg "title=" R4. PNG "alt=" Wkiol1t6ulpwstmwaaexkm8g8cw505.jpg "/>
(9) The following code is the completion of the main function return, the principle and the general function call the same, not in this analysis.
Summary: In my initial contact with the computer, when the machine is still very extravagant things, then gave themselves to develop a learning method: first of all, according to the teaching of knowledge, in the mind to simulate how the computer will be executed, assuming that the textbook examples are correct, deduce the machine should give what kind of results, Verify when you have the opportunity to work on your computer. Now it seems that the idea was right, but because of the lack of perseverance, today's computer level is general. In essence, today's computer is really the simulation of human operation process, programmers how to design the program, the computer will be executed.
This article is from the "Studypark" blog, make sure to keep this source http://swordautumn.blog.51cto.com/1485402/1618288
How does a computer work?