Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the compilation function of C++ Compiler

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to understand the compilation function of C++ compiler, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

The following is an in-depth description of the large-scale C++ compiler in C++. The C++ compiler has a strong complexity, and the number of lines of the source program is also very large, so when compiling the C++ compiler, be sure to make a schedule. In this way, we can better operate on the C++ compiler.

Then follow the main line of the compilation to analyze its source program. Let's first take a look at a simple example of C++, as follows:

# 001 # include # 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; iTunes +) # 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 * * line, because the output is displayed on the screen later by calling the printf function. The second blank line 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, which are 5, 6, 7 and 8 lines respectively. Line 10 adds the two variables nTest1 and nTest2, and then assigns a value to the variable nTest3. Line 11 shows the value of the variable nTest3, which is displayed with decimal output. The nTest3+ I value is output five times in lines 13 to 16. In line 18, output the time the program was compiled and the string of hello world.

The task of C++ compiler is to transform the above source program into assembly code output, or into other intermediate code output. Here the LCC compiler outputs assembly code, so no other intermediate code output is introduced. So what kind of assembly output does LCC turn the above source program into? Let's take a look at its object code first, 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 years, 13, 10,' hello world', 10,0 # 053 times ($- $) & 0 nop # 054 $L7: # 055 db'% dwells, 13,10 0 # 056 times ($- $) & 0 nop # 057 $L2: # 058 db 'nTest3 =% dwells, 13, 10, 0 # 059

LCC is a C++ compiler that can generate a lot of object code. Here we mainly introduce the code that generates x86 NASM assembly. The assembly code above is the assembly format of NASM. You can use NASM to compile and generate the object file, and then use the linker to generate the executable file. If you can't understand the NASM assembly above, you need to read the NASM manual, which is available for download online. If you want to have a deeper understanding of the process of assembling and generating machine code, you can also analyze the program implementation of NASM in depth.

From the C++ and assembly above, we can also see that the assembly code is more complex and has more lines than C++ code, and it is divided into data segments and code segments. Therefore, the use of C++ compiler can greatly improve production efficiency, and easier to understand, so it is easy to reduce the cost of software, easy to develop large-scale software engineering.

This is the end of the answer to the question on how to understand the compilation function of the C++ compiler. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report