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

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

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

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

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


当前回答

我只能回答为什么我个人不经常用汇编编写程序,主要原因是这样做比较乏味。此外,我认为在没有立即注意到的情况下,更容易把事情搞砸。例如,你可能在一个例程中改变了使用寄存器的方式,但在另一个地方却忘记了这一点。它会很好地组装起来,直到很久以后你才会注意到。

也就是说,我确实认为组装仍然有有效的用途。例如,我有许多非常优化的汇编例程来处理大量数据,使用SIMD并遵循偏执的“每一个比特都是神圣的”[引用V.Stob]方法。(但请注意,简单的程序集实现通常比编译器为您生成的要糟糕得多。)

其他回答

我用汇编编程已经有一个月了。我经常用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.

我猜即使是x86(_64)上的ASM也有意义,因为您可以利用编译器难以优化的指令来获得很多好处。以X264为例,它使用了大量的asm编码,速度增益是巨大的。

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

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

我相信有很多原因,但我能想到的两个原因是

汇编代码肯定更难读(我肯定编写它也更耗时) 当您有一个庞大的开发团队在开发一个产品时,将代码划分为逻辑块并通过接口进行保护是很有帮助的。

我喜欢用汇编语言编程,但是用高级语言做同样的事情需要更多的代码,而且代码行和错误之间有直接的联系。(这在几十年前的《人月神话》中就有解释。)

可以把C语言看作是“高级汇编”,但再往上走几步,你就进入了另一个世界。在c#中,你不需要三思就可以写这样的代码:

foreach (string s in listOfStrings) { /* do stuff */ }

这将是几十行,甚至几百行的汇编代码,每个实现它的程序员将采用不同的方法,下一个来的人将不得不找出它。因此,如果您相信(许多人都相信)程序主要是为其他人阅读而编写的,那么汇编的可读性就不如典型的HLL。

编辑:我积累了一个用于常见任务的个人代码库,以及用于实现类c控制结构的宏。但在90年代,当gui成为常态时,我遇到了瓶颈。太多的时间被花在了例行公事上。

我的上一个需要使用ASM的任务是在几年前,编写代码来对抗恶意软件。没有用户界面,所以只有有趣的部分,没有臃肿的部分。