什么是产生GCC,G ++ ASM code的需要 [英] What is the need to generate ASM code in gcc, g++

查看:260
本文介绍了什么是产生GCC,G ++ ASM code的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要缩小我的问题,让我描述我的假设,我做...实验

To narrow down my question, let me describe my assumption and the experiment that I did...

我的假设:汇编语言编写,运行比C / C ++对手更快,也可执行文件的大小太大的code比是从C产生小得多/ C ++ code。

My assumption: A code written in assembly language will run much faster than its C/C++ counterpart and also the executable size to be much smaller than that generated from C/C++ code.

实验:我写了下面的程序bin2dec.c

The experiment: I wrote the below program in to bin2dec.c

#include <stdio.h>

int main()
{
    long int binary, decimal, reminder, exp;
    int i, j;

    for(i=0; i<10000; i++)
    {
        for(j=0; j<1000; j++)
        {
            binary = 11000101;

            exp = 1;
            decimal = 0;

            while(binary != 0)
            {
                reminder = binary % 10;
                binary = binary / 10;
                decimal = decimal + reminder * exp;
                exp *= 2;
            }   
        }
    }
    return 0;
}

然后生成的ASM code它 GCC -S bin2dec.c -o bin2dec.s

在我编这两个文件如下

gcc bin2dec.c -o bin2dec_c
gcc bin2dec.s -o bin2dec_s

测试1:找到了这两个文件的一些内部细节

Test 1: Found out some internal details of both the files

[guest@localhost ASM]$ size bin2dec_c bin2dec_s
   text    data     bss     dec     hex filename
    951     252       4    1207     4b7 bin2dec_c
    951     252       4    1207     4b7 bin2dec_s

结果:两者都是完全一样...

Result: Both are exactly same...

测试2:执行的文件,并计算出所需要的时间。

Test 2: Executed the files and calculated the time taken

[guest@localhost ASM]$ time ./bin2dec_c
real    0m1.724s
user    0m1.675s
sys     0m0.002s

[guest@localhost ASM]$ time ./bin2dec_s
real    0m1.721s
user    0m1.676s
sys     0m0.001s

结果:两者都是一样的。过了一段时间,从ASM生成的可执行跑慢: - (

Result: Both are same. Some time the executable generated from ASM ran slower :-(

所以现在的问题是,我的假设是否是错误的?
如果不是这样,这两个可执行文件bin2dec_c和bin2dec_s跑以相同的速度我做了什么错呢?
有没有更好的办法从C / C ++程序获得ASM code,或者我应该重写所有的逻辑从ASM划痕以提升运行速度和程序大小的优势在哪里?

So the question is, whether my assumptions were wrong? If not, what mistake I did so that both the executables bin2dec_c and bin2dec_s ran at the same speed? Is there any better way to get ASM code from a C/C++ program or I should rewrite all the logic from the scratch in ASM to gain the advantage of speed and program size?

推荐答案

这是一个古老的传统(在早期的Unix系统上世纪70年代,机器是如此之小,这是简单的生成一些汇编文件),一些编译器可以直接生成对象文件或机器code;可能是一些最新版本的锵/ LLVM ,或的 TinyCC (对于C只:快速编译时间,但速度很慢的可执行文件)也许是从IBM的一些专有XLC编译器,有的人海合会社区之间都在思考的(特别是对于 GCCJIT )。

It is an old tradition (in the 1970s on early Unix systems, the machine was so small that it was simpler to generate some assembler file), and some compilers can generate directly object files or machine code; probably some recent version of Clang/LLVM, or TinyCC (for C only: fast compilation time, but very slow executable!) perhaps some proprietary XLC compiler from IBM, and some people amongst the GCC community are thinking about that (notably for GCCJIT).

然而,生成汇编文件往往是编译器开发人员更容易。由于大多数编译器的工作发生在通过优化(这是的转化一些内部重presentations 的编译器),失去几毫秒开始汇编不是很重要。

However, generating an assembler file is often easier for compiler developers. And since most of the compiler work happens in optimization passes (which are transforming some internal representations in the compiler), losing a few milliseconds to start the assembler is not very important.

使用 GCC ,编译 GCC -time GCC -ftime-报告(和你平时的优化标志,例如:当然 -O2 )了解编译器在哪里它花费的时间。这是从来没有在汇编...

With GCC, compile with gcc -time and gcc -ftime-report (and of course your usual optimization flags, e.g. -O2) to understand where the compiler spends its time. It is never in the assembler...

您可能有时会发现寻找到生成的汇编文件非常有用。编译 G ++ -O2 -Wall -S -fverbose-ASM -std = C ++ 11的富 foo.cc C ++ 11的文件。 CC 然后看(有一些编辑器或寻呼机)到生成的 foo.s 汇编文件。

You might sometimes find useful to look into the generated assembler file. Compile your foo.cc C++11 file with g++ -O2 -Wall -S -fverbose-asm -std=c++11 foo.cc then look (with some editor or pager) into the generated foo.s assembler file.

您可以甚至 G ++ -fdump树,所有-O2 并获得数百编译转储文件从的 GCC 解释什么转换的编译器上的code一样。

You could even compile with g++ -fdump-tree-all -O2 and get hundreds of compiler dump files from GCC explaining what transformations the compiler did on your code.

BTW今日​​(超标量流水线)处理器(在桌面,笔记本电脑,平板电脑,您的服务器的)是如此复杂,在实践中的编译器可以优化比人类更好的程序员。所以实际上来说的通过的优化的一些实际大小C code编译器产生的汇编code(如几百行的C源文件)< STRONG>是经常比一个实验汇编人类程序员可以code 在几周内(不到一千汇编行)快。换句话说你的假设(即code人类用汇编写的是更快/优于code人类用C语言编写,并受到良好的编译的优化的编译器)的 在实践中

BTW today's (superscalar, pipelined) processors (the ones in your desktop, your laptop, your tablet, your server) are so complex that in practice a compiler can optimize better than a human programmer. So practically speaking the assembler code produced by an optimizing compiler from some realistically sized C code (e.g. a C source file of a few hundred lines) is often faster than what an experimented assembler human programmer can code in a few weeks (less than a thousand assembler lines). In other words your assumption (that code human-written in assembler is faster/better than code human-written in C and compiled by a good optimizing compiler) is wrong in practice.

(顺便说一句,一个优化编译器允许改变你的 bin2dec.c 程序,它没有可观察到的副作用,如没有输入和输出,成一个空的程序,并 GCC 5.2 确实与的gcc -O2 !!)

(BTW, an optimizing compiler is permitted to transform your bin2dec.c program, which has no observable side-effects, e.g. no input and output, into an empty program, and GCC 5.2 does that with gcc -O2 !!)

也阅读有关停机问题并的赖斯定理。有一种内在的局限性什么优化编译器或静态程序分析仪可以实现。

Read also about the halting problem and Rice's theorem. There is a intrinsic limitation in what optimizing compilers or static program analyzers can achieve.

这篇关于什么是产生GCC,G ++ ASM code的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆