汇编编程比高级语言(如c)花费更长的时间,更难编程,这似乎是一种主流观点。因此,出于这些原因以及更好的可移植性,似乎建议或假设用高级语言编写更好。
最近我一直在用x86汇编语言写作,我开始意识到这些原因可能都不是真的,除了可移植性。也许这更多的是一个熟悉的问题,知道如何写好汇编。我还注意到在汇编中编程与在HLL中编程有很大的不同。也许一个好的、有经验的汇编程序员可以像一个有经验的C程序员用C写程序一样轻松、快速地编写程序。
也许是因为汇编编程与hll有很大的不同,因此需要不同的思维、方法和方式,这使得对不熟悉的人编写程序看起来非常尴尬,因此给汇编编程带来了不好的名声。
如果可移植性不是问题,那么C语言比NASM这样的优秀汇编器有什么优势呢?
编辑:
我只是想指出。当你用汇编语言写作时,你不必只写指令代码。您可以使用宏、过程和您自己的约定来进行各种抽象,使程序更模块化、更可维护、更易于阅读。这就是熟悉如何编写好的汇编的原因。
人们似乎忘记了还有另一个方向。
为什么你一开始要用汇编语言写东西?为什么不用一种真正低级的语言来编写程序呢?
而不是
mov eax, 0x123
add eax, 0x456
push eax
call printInt
你还是写吧
B823010000
0556040000
50
FF15.....
这有很多好处,你知道你的程序的确切大小,你可以重用指令的值作为其他指令的输入,你甚至不需要汇编程序来编写它,你可以使用任何文本编辑器……
你仍然喜欢汇编程序的原因,是其他人喜欢C语言的原因。
除了其他人对可读性、可维护性、更短的代码从而更少的错误和更简单的回答之外,我还将添加一个额外的原因:
程序的速度。
Yes, in assembly you can hand tune your code to make use of every last cycle and make it as fast as is physically possible. However who has the time? If you write a not-completely-stupid C program, the compiler will do a really good job of optimizing for you. Probably making at least 95% of the optimizations you'd do by hand, without you having to worry about keeping track of any of it. There's definitely a 90/10 kind of rule here, where that last 5% of optimizations will end up taking up 95% of your time. So why bother?
作为一名大部分时间都在嵌入式编程领域工作的开发人员,我认为汇编语言还远远没有成为一种死亡/过时的语言。有某种接近金属级别的编码(例如,在驱动程序中)有时不能用高级语言准确或有效地表达。我们几乎所有的硬件接口例程都是用汇编程序编写的。
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.
最后,任何语言都有适用的时间和地点。人们使用他们最熟悉和最有成效的东西。在程序员的程序库中可能总会有汇编的位置,但是大多数程序员会发现他们可以用一种高级语言编写代码,这种语言在更少的时间内几乎同样高效。