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

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

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

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

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


当前回答

你好,我是一个编译器。

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语言的原因。

不同之处在于,汇编程序是一门代码的艺术,是一幅出自艺术家之手的好画。你比蹩脚的编译器代码聪明吗?如果你是,使用它或照顾你的绘画与c和汇编一起。

因为事情总是这样:时间流逝,美好的东西也会消逝:(

但是当你编写asm代码时,这与编写高级语言的感觉完全不同,尽管你知道它的效率要低得多。这就像你是一个画家:你可以自由地画任何你喜欢的东西,完全没有限制(嗯,只有CPU的功能)……这就是我喜欢它的原因。很遗憾这种语言消失了。但只要有人还记得它并对它进行编码,它就永远不会消亡!

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

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

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

我现在正在comp组织学习汇编,虽然它很有趣,但它也非常低效。你必须在脑子里记住更多的细节才能让事情顺利进行,而且写同样的东西也会慢一些。例如,c++中一个简单的6行For循环可以等于18行或更多的汇编。

就我个人而言,学习硬件层面的工作方式非常有趣,它让我对计算的工作方式有了更多的了解。