Concise x86 assembly Language Tutorial (7)

Source: Internet
Author: User
Tags compact

5.0 Compilation Optimization Overview

Optimization is a very important thing. As a program designer, you certainly want your program to be small and fast. Many of the DOS times have mentioned that "a certain compiler can generate very compact code", in other words, the compiler will reduce the code as much as you can if you use the functionality it provides correctly. Currently, the popular C + + compiler on the Intel x86 system, including the Intel C + + Compiler, the GNU C + + Compiler, and the latest Microsoft and Borland compilers, can provide very compact code. Using these compilers correctly, you can get code that is good enough for performance.

But machines can't do creative things like people do at the moment. Thus, there are times when we may have to do something by hand.

Using assembly language to optimize code is a difficult and highly skilled job. Many compilers can be born into code that has been specially optimized for the processor, and once modified, these special optimizations may be compromised and invalidated. So before you decide to use your own assembler code, be sure to test whether the code generated by the compiler is better or better.

In this chapter, we will discuss some of the things that the compiler will do at some point (in a sense, this chapter is more like the relevant content in the course of compiling the principles of design, the principles of computer composition, the architecture of computers) in the basic course of computer science. Many of the contents of this chapter are not closely related to the assembly language program design itself, most of which are preparing for optimization using assembly language. The compiler does do these optimizations, but it does not always do so, and it does not, in the nature of the compiler's design, have the obligation to do so-the compiler does the equivalent transformations, not the equivalents. Consider the following code:

Program Section 1
int Gaussiansum () {
int I, j=0;

for (i=0; i<100; i++) j+=i;

Return J;
}

Well, first of all, the vast majority of compilers may not presume to "tamper" with

Program Section 1 (improved 1)
int Gaussiansum () {
int I, j=0;

for (I=1; i<100; i++) j+=i;

Return J;
}

Most (but indeed not all) compilers will not change it to

Program Section 1 (improved 2)
inline int gaussiansum () {
return 5050;
}

Both revisions are different from the semantics of the original program. First of all we see that it is not necessary to let I start from 0, because when j+=i, i=0 does nothing useful; then, in fact, it is not necessary to compute the 1+...+100 every time-it can be calculated in advance and returned when needed.

This example may not be appropriate (no one is expected to write the original version of the code), but this practice may indeed appear in the design. We refer to the improvement of 2 as a compile-time expression in advance calculation, while the improvement of 10% is reduced by cyclic strength.

However, some of the new compilers do make both of these optimizations. But don't panic, look at the following code:

Program Section 2
int getfactorial (int k) {
int I, j=1;

if ((k<0) | | (k>=10)) return-1;

if ((k<=1)) return 1

for (I=1; i<k; i++) j*=i;

Return J;
}

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.