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

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

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

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

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


当前回答

早期的发现之一(你可以在布鲁克斯的《神秘的人月》中找到,这是20世纪60年代的经验)是,人们使用一种语言的效率或多或少与使用另一种语言的效率一样高,每天调试的代码行数也差不多。显然,这并不是普遍的事实,如果做得太过分,可能会崩溃,但对于Brooks时代的高级语言来说,这是普遍的事实。

因此,提高工作效率的最快方法是使用一行代码就能做更多工作的语言,这确实有效,至少对于像FORTRAN和COBOL这样复杂的语言,或者给出一个更现代的例子C。

其他回答

合理水平的汇编能力是一项有用的技能,特别是当您从事任何类型的系统级或嵌入式编程时,这并不是因为您必须编写那么多汇编程序,而是因为有时了解盒子的实际功能非常重要。如果您对汇编程序的概念和问题没有低级的理解,这可能会非常困难。

然而,至于在汇编器中实际编写大量代码,有几个原因导致它做得不多。

There's simply no (almost) need. Except for something like the very early system initialization and perhaps a few assembler fragments hidden in C functions or macros, all very low-level code that might once have been written in assembler can be written in C or C++ with no difficulty. Code in higher-level languages (even C and C++) condenses functionality into far fewer lines, and there is considerable research showing that the number of bugs correlates with the number of lines of source code. Ie, the same problem, solved in assembler and C, will have more bugs in assembler simply because its longer. The same argument motivates the move to higher level languages such as Perl, Python, etc. Writing in assembler, you have to deal with every single aspect of the problem, from detailed memory layout, instruction selection, algorithm choices, stack management, etc. Higher level languages take all this away from you, which is why are so much denser in terms of LOC.

从本质上讲,以上所有内容都与汇编程序与C或其他语言中可用的抽象级别有关。汇编程序迫使您自己制作所有的抽象,并通过您自己的自律来维护它们,而任何中级语言,如C,特别是高级语言,都可以为您提供开箱即用的抽象,以及相对容易地创建新抽象的能力。

$$$

一家公司雇佣一名开发人员来帮助将代码转化为$$$。越快地生成有用的代码,公司就能越快地将代码转化为利润。

高级语言通常更擅长生成大量有用的代码。这并不是说集会没有它的位置,因为在有些时候和地方,没有别的方法可以。

你好,我是一个编译器。

I just scanned thousands of lines of code while you were reading this sentence. I browsed through millions of possibilities of optimizing a single line of yours using hundreds of different optimization techniques based on a vast amount of academic research that you would spend years getting at. I won't feel any embarrassment, not even a slight ick, when I convert a three-line loop to thousands of instructions just to make it faster. I have no shame to go to great lengths of optimization or to do the dirtiest tricks. And if you don't want me to, maybe for a day or two, I'll behave and do it the way you like. I can transform the methods I'm using whenever you want, without even changing a single line of your code. I can even show you how your code would look in assembly, on different processor architectures and different operating systems and in different assembly conventions if you'd like. Yes, all in seconds. Because, you know, I can; and you know, you can't.

附言:哦,顺便说一下,你没有使用你写的一半代码。我帮了你一个忙,把它扔了。

可移植性始终是一个问题——如果不是现在,至少最终是这样。编程行业每年花费数十亿美元移植旧软件,这些软件在编写时“显然”没有任何可移植性问题。

人们似乎忘记了还有另一个方向。

为什么你一开始要用汇编语言写东西?为什么不用一种真正低级的语言来编写程序呢?

而不是

mov eax, 0x123
add eax, 0x456
push eax
call printInt

你还是写吧

B823010000
0556040000
50 
FF15.....

这有很多好处,你知道你的程序的确切大小,你可以重用指令的值作为其他指令的输入,你甚至不需要汇编程序来编写它,你可以使用任何文本编辑器……

你仍然喜欢汇编程序的原因,是其他人喜欢C语言的原因。