Analysis of the source program of the LSS Compiler (1) objective of the C Compiler

Source: Internet
Author: User
This large-scale C compiler is analyzed from a simple goal. After all, its functions are complex and the number of lines in the source program is also very large. Therefore, set a simple goal and then analyze it. Next, we will analyze its source program with the main line of compilation and running. Next, let's take a look at a simple C example, as follows: #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} # The above program is an example of how the compiler works. It contains the header file s in the first line. Tdio. H, because the printf function is called later, the output is displayed on 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 above source code to the output of assembly code, or to other intermediate code. 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 100', 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 ASM assembly code. 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 make it easier to understand. This can easily reduce the software cost and develop large-scale software engineering.

 

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.