The following describes in depth the large-scale C ++ compiler in C ++. The C ++ compiler is very complex, and the number of lines in the source program is also very large, when compiling a C ++ compiler, you must set a schedule to better operate the C ++ compiler.
Next, we will analyze its source program with the main line of compilation and running. The following is a simple C ++ example:
- #001 #include <stdio.h>
- #002
- #003 int main(void)
- #004 {
- #005 int nTest1 = 1;
- #006 int nTest2 = 2;
- #007 int nTest3;
- #008 int i;
- #009
- #010 nTest3 = nTest1 + nTest2;
- #011 printf("nTest3 = %d\r\n",nTest3);
- #012
- #013 for (i = 0; i < 5; i++)
- #014 {
- #015 printf("%d\r\n",nTest3+i);
- #016 }
- #017
- #018 printf(__TIME__" "__DATE__"\r\nhello world\n");
- #019 return 0;
- #020 }
- #021
The above program is an example of how the compiler works. It contains the header file stdio. h In the first line, because the printf function is called later to display the output to the screen. The second line is empty, and the third line is the main function, which is the entry function of the C program. In the main function, several local variables are defined, namely the variables in rows 5, 6, 7, and 8. Line 3 adds two variables nTest1 and nTest2, and then assigns the value to the variable nTest3. The value of nTest3 is displayed in Row 3, which is output in decimal format. The nTest3 + I value is output five times in rows 13th to 16. Output the Compilation Time of the program and the hello world string in row 18th.
The C ++ compiler task is to convert the source code above to the assembly code output, or to other intermediate code output. Here, the LCS compiler outputs assembly code, so it does not introduce other intermediate code output. So what kind of Assembly output does the source code above be converted? Let's take a look at its target Code as follows:
- #001 [global $main]
- #002 [section .text]
- #003 $main:
- #004 push ebx
- #005 push esi
- #006 push edi
- #007 push ebp
- #008 mov ebp, esp
- #009 sub esp, 16
- #010 mov dword [ebp + -12], 1
- #011 mov dword [ebp + -16], 2
- #012 mov edi, dword [ebp + -12]
- #013 mov esi, dword [ebp + -16]
- #014 lea edi, [esi + edi]
- #015 mov dword [ebp + -8], edi
- #016 mov edi, dword [ebp + -8]
- #017 push dword edi
- #018 lea edi, [$L2]
- #019 push dword edi
- #020 call $printf
- #021 add esp, 8
- #022 mov dword [ebp + -4], 0
- #023 $L3:
- #024 mov edi, dword [ebp + -8]
- #025 mov esi, dword [ebp + -4]
- #026 lea edi, [esi + edi]
- #027 push dword edi
- #028 lea edi, [$L7]
- #029 push dword edi
- #030 call $printf
- #031 add esp, 8
- #032 $L4:
- #033 inc dword [ebp + -4]
- #034 cmp dword [ebp + -4], 5
- #035 jl near $L3
- #036 lea edi, [$L8]
- #037 push dword edi
- #038 call $printf
- #039 add esp, 4
- #040 mov eax, 0
- #041 $L1:
- #042 mov esp, ebp
- #043 pop ebp
- #044 pop edi
- #045 pop esi
- #046 pop ebx
- #047 ret
- #048 [extern $printf]
- #049 [section .data]
- #050 times ($-$$) & 0 nop
- #051 $L8:
- #052 db '00:30:28 Apr 07 2007', 13, 10, 'hello world', 10, 0
- #053 times ($-$$) & 0 nop
- #054 $L7:
- #055 db '%d', 13, 10, 0
- #056 times ($-$$) & 0 nop
- #057 $L2:
- #058 db 'nTest3 = %d', 13, 10, 0
- #059
A c ++ compiler that can generate a lot of target code. Here we mainly introduce the code used to generate the NASM assembly of X86. The assembly code above is the NASM Assembly format. You can use NASM to compile and generate the target file, and then use the Connection Program to generate executable files. If you cannot understand the NASM assembly above, you need to go to the NASM manual, which has been downloaded online. If you want to have a better understanding of the process of generating machine code by means of assembly, you can also thoroughly analyze the implementation of NASM programs.
From the above C ++ and Assembly, we can see that the assembly code is more complex than the C ++ code, with a large number of lines and data segments and code segments. Therefore, using the C ++ compiler can greatly improve the production efficiency and be easier to understand. This can easily reduce the software cost and develop large-scale software engineering.
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design