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

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

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

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

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


当前回答

其他人说的大多都是这样。

在C语言发明之前的美好时光里,当唯一的高级语言是像COBOL和FORTRAN这样的东西时,有很多事情不借助汇编程序是不可能完成的。这是获得全面灵活性的唯一方法,能够访问所有设备等等。但是后来C语言被发明了,在C语言中,几乎所有在汇编中可能实现的事情都可以实现。从那以后,我很少写汇编。

也就是说,我认为这是一个非常有用的练习,新程序员学习编写汇编程序。不是因为他们会经常使用它,而是因为这样你就能理解计算机内部到底发生了什么。我见过很多编程错误和低效的代码,它们来自程序员,他们显然不知道比特、字节和寄存器到底发生了什么。

其他回答

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

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

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

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

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#.

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

$$$

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

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

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