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

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

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

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

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


当前回答

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

其他回答

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

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

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

正如其他人之前提到的,任何工具存在的原因都是它的工作效率。由于hll可以完成与许多行asm代码相同的工作,我想汇编被其他语言取代是很自然的。对于接近硬件的操作——有C语言的内联汇编和每种语言的其他变体。 保罗·卡特博士在PC汇编语言中说

"...更好地理解如何 计算机实际上是在较低的水平上工作的 而不是编程语言 帕斯卡。通过获得更深层次的 了解计算机的工作原理, 读者通常可以做得更多 生产开发软件 高级语言,如C和 c++。学习汇编编程 语言是一种极好的方法 实现这个目标。”

在我的大学课程中有装配入门。这将有助于理清概念。然而,我怀疑我们中是否有人会用汇编编写90%的代码。今天深入的组装知识有多大的相关性?

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

当你用汇编语言写作时,你不必只写指令代码。您可以使用宏、过程和您自己的约定来进行各种抽象,使程序更模块化、更可维护、更易于阅读。

因此,您基本上是在说,通过熟练地使用复杂的汇编程序,您可以使ASM代码越来越接近C(或者您自己发明的另一种低级语言),直到最终您的工作效率与C程序员一样高。

这是否回答了你的问题?: -)

我并不是在无所事事地说:我已经使用这样的汇编器和系统进行了编程。更好的是,汇编程序可以以虚拟处理器为目标,并且一个单独的翻译程序可以为目标平台编译汇编程序的输出。与LLVM的IF很相似,但其早期形式比它早了大约10年。因此,有了可移植性,再加上为特定目标汇编器编写例程的能力,以提高效率。

Writing using that assembler was about as productive as C, and with by comparison with GCC-3 (which was around by the time I was involved) the assembler/translator produced code that was roughly as fast and usually smaller. Size was really important, and the company had few programmers and was willing to teach new hires a new language before they could do anything useful. And we had the back-up that people who didn't know the assembler (e.g. customers) could write C and compile it for the same virtual processor, using the same calling convention and so on, so that it interfaced neatly. So it felt like a marginal win.

这是在开发汇编程序技术、库等方面花费了许多人多年的工作。不可否认的是,其中大部分都是为了使其可移植,如果它只针对一种架构,那么所有唱歌所有跳舞的汇编器就会容易得多。

总而言之:你可能不喜欢C语言,但这并不意味着使用C语言的努力就比想出更好的东西的努力更大。

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

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

而不是

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

你还是写吧

B823010000
0556040000
50 
FF15.....

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

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