汇编编程比高级语言(如c)花费更长的时间,更难编程,这似乎是一种主流观点。因此,出于这些原因以及更好的可移植性,似乎建议或假设用高级语言编写更好。

最近我一直在用x86汇编语言写作,我开始意识到这些原因可能都不是真的,除了可移植性。也许这更多的是一个熟悉的问题,知道如何写好汇编。我还注意到在汇编中编程与在HLL中编程有很大的不同。也许一个好的、有经验的汇编程序员可以像一个有经验的C程序员用C写程序一样轻松、快速地编写程序。

也许是因为汇编编程与hll有很大的不同,因此需要不同的思维、方法和方式,这使得对不熟悉的人编写程序看起来非常尴尬,因此给汇编编程带来了不好的名声。

如果可移植性不是问题,那么C语言比NASM这样的优秀汇编器有什么优势呢?

编辑: 我只是想指出。当你用汇编语言写作时,你不必只写指令代码。您可以使用宏、过程和您自己的约定来进行各种抽象,使程序更模块化、更可维护、更易于阅读。这就是熟悉如何编写好的汇编的原因。


当前回答

随着汇编变得越来越不常见,出现了一个恶性循环:随着高级语言的成熟,汇编语言指令集的构建越来越少地是为了方便程序员,而更多地是为了方便编译器。

So now, realistically, it may be very hard to make the right decisions on, say, which registers you should use or which instructions are slightly more efficient. Compilers can use heuristics to figure out which tradeoffs are likely to have the best payoff. We can probably think through smaller problems and find local optimizations that might beat our now pretty sophisticated compilers, but odds are that in the average case, a good compiler will do a better job on the first try than a good programmer probably will. Eventually, like John Henry, we might beat the machine, but we might seriously burn ourselves out getting there.

Our problems are also now quite different. In 1986 I was trying to figure out how to get a little more speed out of small programs that involved putting a few hundred pixels on the screen; I wanted the animation to be less jerky. A fair case for assembly language. Now I'm trying to figure out how to represent abstractions around contract language and servicer policy for mortgages, and I'd rather read something that looks close to the language that the business folks speak. Unlike LISP macros, Assembly macros don't enforce much in the way of rules, so even though you might be able to get something reasonably close to a DSL in a good assembler, it'll be prone to all sorts of quirks that won't cause me problems if I wrote the same code in Ruby, Boo, Lisp, C# or even F#.

如果您的问题很容易用高效的汇编语言来表达,那么您的能力就更强了。

其他回答

浏览这些答案,我敢打赌9/10的回复者从未使用过组装。

这是一个经常出现的老问题,你得到的答案都是一样的,而且大多是错误的答案。如果不是为了便携性,我仍然会自己组装所有的东西。即便如此,我还是用C编写代码,就像用汇编一样。

作为一名大部分时间都在嵌入式编程领域工作的开发人员,我认为汇编语言还远远没有成为一种死亡/过时的语言。有某种接近金属级别的编码(例如,在驱动程序中)有时不能用高级语言准确或有效地表达。我们几乎所有的硬件接口例程都是用汇编程序编写的。

That being said, this assembly code is wrapped such that it can be called from C code and is treated like a library. We don't write the entire program in assembly for many reasons. First and foremost is portability; our code base is used on several products that use different architectures and we want to maximize the amount of code that can be shared between them. Second is developer familiarity. Simply put, schools don't teach assembly like they used to, and our developers are far more productive in C than in assembly. Also, we have a wide variety of "extras" (things like libraries, debuggers, static analysis tools, etc) available for our C code that aren't available for assembly language code. Even if we wanted to write a pure-assembly program, we would not be able to because several critical hardware libraries are only available as C libs. In one sense, it's a chicken/egg problem. People are driven away from assembly because there aren't as many libraries and development/debug tools available for it, but the libs/tools don't exist because not enough people use assembly to warrant the effort creating them.

最后,任何语言都有适用的时间和地点。人们使用他们最熟悉和最有成效的东西。在程序员的程序库中可能总会有汇编的位置,但是大多数程序员会发现他们可以用一种高级语言编写代码,这种语言在更少的时间内几乎同样高效。

如果一个普通的生产程序有10万行代码,每一行大约有8-12条汇编指令,那就是100万条汇编指令。

即使您可以以相当快的速度手写所有这些代码(请记住,这是您必须编写的8倍代码),如果您想更改某些功能会发生什么?从这100万条指令中理解你几周前写的东西简直是一场噩梦!没有模块,没有类,没有面向对象的设计,没有框架,什么都没有。即使是为最简单的事情编写的类似代码的数量最多也令人生畏。

此外,你不能像高级语言那样优化你的代码。例如,C语言执行了大量的优化,因为你描述了你的意图,不仅仅是你的代码,在汇编程序中你只写代码,汇编程序不能对你的代码执行任何值得注意的优化。你写的就是你得到的,相信我,你不可能可靠地优化100万条指令,你在写的时候不断地打补丁。

与高级语言相比,ASM的易读性很差,而且实际上难以维护。

此外,ASM开发人员比其他更流行的语言(如C)要少得多。

此外,如果您使用高级语言,并且有新的ASM指令可用(例如SSE),您只需要更新您的编译器,您的旧代码就可以轻松使用新的指令。

如果下一个CPU有两倍多的寄存器呢?

这个问题的反义词是:编译器提供了什么功能?

我怀疑你能够/想要/应该比gcc -O3更好地优化你的ASM。

我用汇编编程已经有一个月了。我经常用C写一段代码,然后把它编译成汇编来帮助我。也许我没有充分利用C编译器的优化功能,但似乎我的C asm源包含不必要的操作。所以我开始看到,一个好的C编译器胜过一个好的汇编编码器的说法并不总是正确的。

Anyways, my assembly programs are so fast. And the more I use assembly the less time it takes me to write out my code because it's really not that hard. Also the comment about assembly having poor legibility is not true. If you label your programs correctly and make comments when there is additional elaboration needed you should be all set. In fact in ways assembly is more clear to the programmer because they are seeing what is happening at the level of the processor. I don't know about other programmers but for me I like knowing what's happening, rather than things being in a sort of black box.

With that said the real advantage of compilers is that a compiler can understand patterns and relationships and then automatically code them in the appropriate locations in the source. One popular example are virtual functions in C++ which requires the compiler to optimally map function pointers. However a compiler is limited to doing what the maker of the compiler allows the compiler to do. This leads to programmers sometimes having to resort to doing bizarre things with their code , adding coding time, when they could have been done trivially with assembly.

Personally I think the marketplace heavily supports high level languages. If assembly language was the only language in existence today then their would be about 70% less people programming and who knows where our world would be, probably back in the 90's. Higher level languages appeal to a broader range of people. This allows a higher supply of programmers to build the needed infrastructure of our world. Developing nations like China and India benefit heavily from languages like Java. These countries will fast develop their IT infrastructure and people will become more interconnected. So my point is that high level languages are popular not because they produce superior code but because they help to meet demand in the world's marketplaces.