Hello, C ++ (4) 2.1.3 my father and mother: The Story Behind compiler and linker 2.1.4 C ++ program execution, 2.1.32.1.4

Source: Internet
Author: User

Hello, C ++ (4) 2.1.3 my father and mother: The Story Behind compiler and linker 2.1.4 C ++ program execution, 2.1.32.1.4
2.1.3 my father and mother: compiler and linker

On the surface, I was created by Visual Studio. On the top of the page, I was responsible for the compilation source code to create the executable program helloworld.exe, but it was the integrated c‑compiler cl.exeand link.exe in Visual Studio. They are both old and I am my real father and mother.

In order to facilitate the compilation, reading and maintenance of people, our source files are written in a C ++ high-level programming language that people can understand. However, computers do not understand this advanced language, so they cannot directly execute the source files written in the advanced language. Therefore, we need to translate the C ++ advanced language that people can understand in the source file into the machine language that the machine can understand. My dad compiler is actually a translator. His job is to compile source files in C ++ (. cpp) is translated into a target file (. obj.

In Visual studio, the name of my old user is cl.exe. You can find the "VS2012 developer command prompt" in the Start menu, and then ask the old user to execute the cl command in the DOS window, compile a cpp source file into the corresponding obj target file. For example, to let my dad compile my source file HelloWorld. cpp into the corresponding target file HelloWorld. obj, run the following command:

Cl/c/ehshelloworld. cpp

Cl is the instruction that calls the compiler, and the subsequent options are used to specify the compilation behavior of the compiler. The "/c" here indicates that only compilation is not linked; "/ESCs" specifies the exception handling model used by the compiler; the last option is HelloWorld. cpp is the C ++ source file to be compiled. Source File HelloWorld. after being compiled by my dad compiler, cpp only gets a target file HelloWorld that cannot be directly executed. obj. You also need to use the mom linker to compare the target file with the standard library target file provided by Visual C ++ (for example, libcpmt. lib) integrated into the final executable file (from the standard library target file to find the program target file used by external functions and other symbols, then fill in the program target file to generate the final executable file), this process is called a link. In the "VS2012 developer command prompt", you can use the following command to complete the link process:

Link HelloWorld. obj

Of course, the whole compilation link work can also be completed by the interpreter cl.exe:

Cl/ehshelloworld. cpp

After the compilation process of my mom, I changed from a source file (helloworld.cpp;to an executable file (helloworld.exe), So I fell to the ground. The entire process is as follows:

Figure 2-6 link compilation process

2.1.4 execution process of the C ++ Program

Once an executable file is generated, you can issue commands to the operating system to run the file. The execution of a program starts from its main function. However, before the main function is executed, the operating system will make a lot of preparations. For example, when an operating system receives a command to execute a program, it must first create a process and allocate private process space; the loader maps the data segment and code segment of the executable file to the virtual memory space of the process. The operating system then initializes the global variables defined in the program. After completing these preparations, the program can start to execute the main function.

After entering the main function, the program will give me the life plan according to the source code, execute one statement one by one, and go down step by step. You must remember that my source code is like this:

Int main () {// output "Hello World!" on the screen !" String cout <"Hello World! "<Endl; return 0 ;}

As you can see from this, after entering the main function, my first statement is:

cout<<"Hello World!"<<endl;

This statement shows "Hello World!" in the DOS window !" As a result, I began to control the DOS window, display the string, and complete the task that the programmer handed over to me through this line of code.

The following statement is:

return 0;

This short statement is the end of my life. It indicates the completion of the main function and the execution of the entire program. Figure 2-7 shows my short and glorious life!

 

Figure 2-7 short and glorious life of the Hello World Program

Learn More: The Story Behind C ++ Program Execution

In the preceding example, we can see that the execution process of a C ++ program is executed one by one starting from the main () function. This process looks very simple, but there are more stories behind each statement.

In the disassembly view in Visual Studio debugging mode (opened in debug mode using the Alt + 8 Shortcut Key), we can see the assembly code corresponding to each statement in the C ++ program. Now, what the statements in the program do and how the functions are implemented are clearly displayed. Although the HelloWorld program simply outputs a string, when we split the program, we can find that many things are done behind it. The HelloWorld program in the Assembly view is as follows (the assembly code is too long, so we only keep the key operations in it ):

# Include <iostream> using namespace std; int main () {// complete preparation 00DC4EC0 push ebp 00DC4EC1 mov ebp, esp 00DC4EC3 sub esp, 0C0h //... 00DC4EDC rep stos dword ptr es: [edi] // complete the task // output "Hello World!" on the screen !" String cout <"Hello World! <Endl; 00DC4EDE mov esi, esp 00DC4EE0 mov eax, dword ptr ds: [00DD031Ch] 00DC4EE5 push eax 00DC4EE6 push 0DCCC70h 00DC4EEB mov ecx, dword ptr ds: [0DD0318h] 00DC4EF1 push ecx // call the operators in the standard library to complete the task 00DC4EF2 call std: operator <std: char_traits <char> (0DC12A3h) 00DC4EF7 add esp, 8. mov ecx, eax 00DC4EFC call dword ptr ds: [0dd0324 H] 00DC4F02 cmp esi, esp 00DC4F04 call _ RTC_CheckEsp (0DC132Ah) return 0; 00DC4F09 xor eax, eax}

When we start a program, the operating system will create a new process to execute the program. A process is an instance of an application. When the operating system creates a process, it will allocate a certain amount of memory space (default heap) for it as its private virtual address space. Generally, the execution of an application corresponds to a process, and the process is responsible for managing everything during the running of the program, such as resource allocation and scheduling. However, as the scheduler of program execution, it is not responsible for program execution. The specific execution is completed by the thread it creates. Each process has a main thread. For multi-threaded applications, there can be multiple auxiliary threads. A thread does not have resources (it uses the resources of the process to which it belongs), but it has its own execution entry, execution sequence series, and an execution end point.

Here, when the main thread responsible for executing the program is created, it will enter the main () function to start execution. It will first execute some initialization work, such as saving the on-site environment, initializing the heap, and passing program parameters, and then execute the specific program code. Although the C ++ program code has only one line, it is broken down into multiple steps in the Assembly view. The execution of the main function is only for some register operations and library function calls. For example, the first sentence of the main () function is to use "push ebp" to save the current address (in assembly code, ebp represents the current address ). Here we will be wondering why the first thing we do after entering the main () function is not to output a string in the C ++ program code, but to save the current address? In fact, what we see in the program code is our description of the functions to be implemented. to truly implement these functions, the C ++ program will accomplish many things for us. The "push ebp" here saves the current address, so that after the main () function is executed, it can return the original address and continue to execute. In addition to register operations (push, mov, pop, and other Assembly commands), assembly code is more important to call other functions through the "call" command. For example, the "call _ RTC_CheckEsp (0DC132Ah)" call Command calls the _ RTC_CheckEsp () function (added by the compiler in the debugging version) check whether the stack is balanced after the program is executed.

In the Assembly view, we can see that every C ++ statement is followed by a story. Only by understanding the story behind each statement can we truly understand this statement. This also tells us that if we find that the behavior of a statement is abnormal and we cannot find the cause from the code level, we need to find the real cause behind this statement.

2.1.5 two major tasks of the program: data description and Data Processing

People write programs to solve problems in the real world. It is observed that all these problems are based on data as input, then the data is processed, and finally the result data is obtained to solve the problem. Therefore, since I am used to help people solve problems, my tasks are naturally inseparable from data description and data processing. From 2 to 8.

People use the formula to give me a definition:

Data + algorithm = Program

Data can be seen as an abstraction and description of all things in the real world. For example, in a C ++ program, we abstract all kinds of data in the real world into various data types. For example, we can abstract integers into the int type and decimal places into the double type. In turn, these types of variables are used to describe a specific data we encounter in our lives. For example, the nWidth variable defined by the int type is used to describe the width of a rectangle, and the strName variable defined by the string type is used to describe a person's name; we can even create custom data types to describe more complex things. For example, we can create a Human data type to abstract the complex things of "person, then define a variable to describe a specific person. In short, using data to abstract and describe things in the real world is my first task.

Figure 2-8 my goals

It is not my ultimate goal to describe the real world with data. my ultimate goal is to process the data to obtain the desired result data. For example, we use nWidth and nHeight to describe the width and height of a rectangle. However, this is not the result data we want, but the area of the rectangle. Therefore, we must also process the nWidth and nHeight data and use the "*" symbol to calculate the product of the two numbers to obtain the area of the rectangle we want. Abstraction of data processing is called an algorithm. My second task is to describe and express algorithms and process the data to obtain the final result.

Learn more: Data Structure + algorithm = Program

The equation "Data + algorithm = Program" is transformed by the famous "Data Structure + algorithm = program. It was first proposed by Mr. Niklaus Wirth, father of Pascal and pioneer in structured programming, it abstracts the core content of a program, which is the data structure used to express data and the algorithm used to process data. The "Data + algorithm = Program" proposed here specifically describes the data that a program processes and the algorithms that process the data. The two equations are both correct, but they only describe the different degrees of the program.

Data and algorithms accompany my life. In helloworld.exe, data and algorithms also exist. For example, output "Hello World!" to the screen !" Statement:

Cout <"Hello World! "<Endl;

"Hello World !" Is a string of data to be output to the screen. The entire statement represents the processing of the string data: the string is displayed on the screen. Data and algorithms are always inseparable and become the two tasks that I will accomplish for my life.




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.