In-depth understanding of computer systems (3.1)---into the world of compilations

Source: Internet
Author: User

This article reprinted address : http://www.cnblogs.com/zuoxiaolong/p/computer13.html

Why to learn assembly language

For most of the ape friends, usually write is some high-level programming language, is the computer field of many great gods, after a few layers of encapsulation to let us enjoy such treatment. In this way, we can save a lot of trouble in the development process. Imagine, if you write a method, you also need to worry about which variables need to be placed in the register, which variables are placed in main memory, put in the register, which should be placed in the main memory of the words and should be placed in the area of the RAM, and so on this kind of bottom-level problem, And to remember all kinds of register names and their effects and so on and so on, would you collapse.

Therefore, it is not difficult to see that high-level language has brought us a lot of convenience, but things are not perfect, so the convenience has also attracted a number of problems. This is because the code we see is probably beyond recognition when actually executing them, so many times it can cause some puzzling problems to happen.

To give a small example, LZ once asked a similar question in the group, this LZ wrote a small program, you learned the Java Ape friends to see the results of this program.

 Public class main{  publicstaticvoid  Main (string[] args)  {      = 127  ;       = 127;       =;       =;       = = b)      ; = = d);}  }

Believe that a lot of people can not see the problem of this program, I think should output two true on the right. But the result of this program is a true and a false, if any ape friends do not believe, you can try it yourself. As for what the reason is, you are interested to study the Java Automatic Disassembly box, in addition to look at the integer object's valueof method cache scope, the answer will be automatically revealed.

The root cause of the problem, in fact, is because the compiler is a layer of fog to the developers, causing some developers only know it, and do not know why, they do not know how to write the program, actually how to run. Such a layer of fog is bound to reduce the level of developers, so in order to improve themselves, we need to uncover this layer of fog. to the developers of C + +, uncovering this layer of fog is actually the process of understanding assembly language.

assembly language for the C + + program ape, just like the class file for Java program Ape is the same, because they are the compiler processing of the product, we can from the simple understanding of the relationship between the two.

This figure should seem to be relatively clear, in fact, LZ said so much, just want to say one thing, that is to understand the knowledge of assembly language, for our usual development has the benefit of not negligible, especially for the developers engaged in C + +, the benefits are endless.

May have the ape friend thought, LZ is a Java to eat the guy, understand the assembly language is a bit superfluous, after all, the Java language from the assembly is a bit too far away. After all, Java is compiled into a class file, and then to the execution engine of the virtual machine, and the execution engine of the virtual machine is implemented by C + +, which needs to be preprocessed and compiled by the GCC compiler to eventually become assembly language. This is a snap, Java is really far away from assembly language.

But LZ wants to say, no matter what kind of job you are in, as long as you do is to command the computer to help you do something, then you have to understand how the computer to help you do these things, otherwise you will only command, and will not know how to do. The consequence of not knowing how to do it is that you don't know how to do it better and reflect the reality that you don't know how to write a better program. This is not difficult to understand, just imagine, you do not know how your program is actually running, how can you know how to write is better.

First Experience compilation

In the process of compiling a C language program, many steps have been made, such as precompilation, compilation, assembly processing, and link processing. We want to understand the assembly language, is the product after the compilation process. So we can add some parameters to the GCC compiler to control that it only generates assembly language, not assembly processing and link processing.

Let's look at the following simple C-language code, which is assumed to be sum.c.

int simple (int *xp,int  y) {    int t = *xp+y;     *xp=t;    return t;}

We use the GCC compiler with the-s parameter to compile this code, eventually we can get a SUM.S file, we use Cat to view this file.

.file    "SUM.C". Text.globl simple. Type simple, @functionsimple: PUSHL%EBP MOVL%ESP,%EBP Subl $ -, %ESP MOVL8(%EBP),%eax//This step is from the main access variable XP MOVL (%EAX),%eax//Take *xp value Addl A(%EBP),%The eax//calculates the *xp+y and coexists to the%EAX register MOVL%eax,-4(%EBP)//assign *xp+y to variable t movl8(%EBP),%eax//again take XP MOVL-4(%EBP),%edx//take T movl%edx, (%eax)//execute T->*XP MOVL-4(%EBP),%eax//put T into%eax ready to return to leave Ret. Size simple.-Simple . Ident"GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3". Section. Note. GNU-stack,"", @progbits

Here we mainly look at how assembly language describes our computational process, so LZ simply add a few comments, to roughly describe the above procedure of the calculation process. It is necessary to note that the beginning of the register, with the parentheses of the primary storage.

The ape friends familiar with GCC should know that we can control the compiler's level of optimization, so we use another way to compile the sum.c, and we add a-o1 parameter on the basis of-S. Then use cat to open the Sum.s file.

.file    "SUM.C". Text.globl simple. Type simple, @functionsimple: PUSHL%EBP MOVL%ESP,%EBP MOVL8(%EBP),%edx//take XP movl A(%EBP),%eax//take y Addl (%edx),%eax//Compute *xp+y coexist to%eax register, ready to return MOVL%eax, (%edx)//deposit *xp+y to *xp popl%EBP ret. Size simple,.-Simple . Ident"GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3". Section. Note. GNU-stack,"", @progbits

It is clear that the number of assembly instructions has been drastically reduced, and the LZ has added a simple comment. As can be seen from the LZ simple note, the optimization here is mainly to remove the existence of the variable T, thus reducing the number of instructions.

If any ape friends really do not understand the meaning of these two pieces of assembly language, can be ignored, here LZ just let you experience the format of assembly language, as well as personal contact with assembly language, our purpose is not to understand its meaning. believe that after 3. The series of x, you will be able to look at these two pieces of assembly code, it should be easy to see the meaning of the.

Article Summary

This chapter dragged a little longer, mainly because the LZ as a Java developer, to the assembly language learning is somewhat difficult, after all, LZ is not good at c/S. Another reason is that the LZ wants to be as clear as possible to avoid misleading some ape friends.

Of course, even so, LZ also do not dare to guarantee that the content of the 3.X is now well-understood, so if there is any understanding of the ape friends of the place, I hope that the ape friends despite the proposed. Not only can avoid misleading look at the blog of the Ape Friends, but also help LZ to correct the wrong understanding.

Well, the main purpose of this article is to bring your apes closer to the world of assembly, so it's just a brief introduction. Next, we'll go into the discussion of registers, data formats, and some assembly instructions.

In-depth understanding of computer systems (3.1)---into the world of compilations

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.