Interpretation of the C ++ compiler compilation function

Source: Internet
Author: User

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:

 
 
  1. #001 #include <stdio.h> 
  2. #002   
  3. #003 int main(void)  
  4. #004 {  
  5. #005  int nTest1 = 1;  
  6. #006  int nTest2 = 2;  
  7. #007  int nTest3;  
  8. #008  int i;  
  9. #009    
  10. #010  nTest3 = nTest1 + nTest2;  
  11. #011  printf("nTest3 = %d\r\n",nTest3);  
  12. #012    
  13. #013  for (i = 0; i < 5; i++)  
  14. #014  {  
  15. #015         printf("%d\r\n",nTest3+i);  
  16. #016  }  
  17. #017    
  18. #018  printf(__TIME__" "__DATE__"\r\nhello world\n");  
  19. #019  return 0;  
  20. #020 }  
  21. #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:

 
 
  1. #001 [global $main]  
  2. #002 [section .text]  
  3. #003 $main:  
  4. #004 push ebx  
  5. #005 push esi  
  6. #006 push edi  
  7. #007 push ebp  
  8. #008 mov ebp, esp  
  9. #009 sub esp, 16  
  10. #010 mov dword [ebp + -12], 1  
  11. #011 mov dword [ebp + -16], 2  
  12. #012 mov edi, dword [ebp + -12]  
  13. #013 mov esi, dword [ebp + -16]  
  14. #014 lea edi, [esi + edi]  
  15. #015 mov dword [ebp + -8], edi  
  16. #016 mov edi, dword [ebp + -8]  
  17. #017 push dword edi  
  18. #018 lea edi, [$L2]  
  19. #019 push dword edi  
  20. #020 call $printf  
  21. #021 add esp, 8  
  22. #022 mov dword [ebp + -4], 0  
  23. #023 $L3:  
  24. #024 mov edi, dword [ebp + -8]  
  25. #025 mov esi, dword [ebp + -4]  
  26. #026 lea edi, [esi + edi]  
  27. #027 push dword edi  
  28. #028 lea edi, [$L7]  
  29. #029 push dword edi  
  30. #030 call $printf  
  31. #031 add esp, 8  
  32. #032 $L4:  
  33. #033 inc dword [ebp + -4]  
  34. #034 cmp dword [ebp + -4], 5  
  35. #035 jl near $L3  
  36. #036 lea edi, [$L8]  
  37. #037 push dword edi  
  38. #038 call $printf  
  39. #039 add esp, 4  
  40. #040 mov eax, 0  
  41. #041 $L1:  
  42. #042 mov esp, ebp  
  43. #043 pop ebp  
  44. #044 pop edi  
  45. #045 pop esi  
  46. #046 pop ebx  
  47. #047 ret  
  48. #048 [extern $printf]  
  49. #049 [section .data]  
  50. #050 times ($-$$) & 0 nop  
  51. #051 $L8:  
  52. #052 db '00:30:28 Apr 07 2007', 13, 10, 'hello world', 10, 0  
  53. #053 times ($-$$) & 0 nop  
  54. #054 $L7:  
  55. #055 db '%d', 13, 10, 0  
  56. #056 times ($-$$) & 0 nop  
  57. #057 $L2:  
  58. #058 db 'nTest3 = %d', 13, 10, 0  
  59. #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.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.