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

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

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

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

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


与高级语言相比,ASM的易读性很差,而且实际上难以维护。

此外,ASM开发人员比其他更流行的语言(如C)要少得多。

此外,如果您使用高级语言,并且有新的ASM指令可用(例如SSE),您只需要更新您的编译器,您的旧代码就可以轻松使用新的指令。

如果下一个CPU有两倍多的寄存器呢?

这个问题的反义词是:编译器提供了什么功能?

我怀疑你能够/想要/应该比gcc -O3更好地优化你的ASM。


我喜欢用汇编语言编程,但是用高级语言做同样的事情需要更多的代码,而且代码行和错误之间有直接的联系。(这在几十年前的《人月神话》中就有解释。)

可以把C语言看作是“高级汇编”,但再往上走几步,你就进入了另一个世界。在c#中,你不需要三思就可以写这样的代码:

foreach (string s in listOfStrings) { /* do stuff */ }

这将是几十行,甚至几百行的汇编代码,每个实现它的程序员将采用不同的方法,下一个来的人将不得不找出它。因此,如果您相信(许多人都相信)程序主要是为其他人阅读而编写的,那么汇编的可读性就不如典型的HLL。

编辑:我积累了一个用于常见任务的个人代码库,以及用于实现类c控制结构的宏。但在90年代,当gui成为常态时,我遇到了瓶颈。太多的时间被花在了例行公事上。

我的上一个需要使用ASM的任务是在几年前,编写代码来对抗恶意软件。没有用户界面,所以只有有趣的部分,没有臃肿的部分。


我“过去”写了很多汇编程序,我可以向你保证,当我用高级语言写程序时,我的效率要高得多。


我相信有很多原因,但我能想到的两个原因是

汇编代码肯定更难读(我肯定编写它也更耗时) 当您有一个庞大的开发团队在开发一个产品时,将代码划分为逻辑块并通过接口进行保护是很有帮助的。


汇编在不同的微处理器之间是不可移植的。


我已经为6502、Z80、6809和8086芯片编写了大量的汇编程序。当C编译器在我所处理的平台上可用时,我就停止了这样做,并且立即提高了至少10倍的生产力。大多数优秀的程序员使用他们所使用的工具是出于合理的原因。


如果一个普通的生产程序有10万行代码,每一行大约有8-12条汇编指令,那就是100万条汇编指令。

即使您可以以相当快的速度手写所有这些代码(请记住,这是您必须编写的8倍代码),如果您想更改某些功能会发生什么?从这100万条指令中理解你几周前写的东西简直是一场噩梦!没有模块,没有类,没有面向对象的设计,没有框架,什么都没有。即使是为最简单的事情编写的类似代码的数量最多也令人生畏。

此外,你不能像高级语言那样优化你的代码。例如,C语言执行了大量的优化,因为你描述了你的意图,不仅仅是你的代码,在汇编程序中你只写代码,汇编程序不能对你的代码执行任何值得注意的优化。你写的就是你得到的,相信我,你不可能可靠地优化100万条指令,你在写的时候不断地打补丁。


跟我们不再去外面厕所的原因一样,也跟我们不再说拉丁语和阿拉姆语的原因一样。

技术的出现使事情变得更容易,更容易获得。

编辑——为了不冒犯别人,我删除了某些词语。


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

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

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,特别是高级语言,都可以为您提供开箱即用的抽象,以及相对容易地创建新抽象的能力。


除了其他人对可读性、可维护性、更短的代码从而更少的错误和更简单的回答之外,我还将添加一个额外的原因:

程序的速度。

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?


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

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


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

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


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


$$$

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

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


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

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


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

因此,您基本上是在说,通过熟练地使用复杂的汇编程序,您可以使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语言的努力就比想出更好的东西的努力更大。


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

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

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


C语言优于一个好的宏汇编器的地方是C语言类型检查。循环结构。自动栈管理。(几乎)自动变量管理。动态内存技术在汇编是一个巨大的痛苦在屁股。与C或更好的foo.insert()列表相比,正确地执行链表是非常可怕的。还有调试——嗯,谁更容易调试谁也不存在争议。他们在那儿轻而易举就赢了。

我几乎一半的职业生涯都是用汇编程序编写的,这让我很容易用汇编程序思考。它帮助我了解C编译器在做什么,这再次帮助我编写C编译器可以有效处理的代码。用C编写的一个经过深思熟虑的例程可以在汇编程序中输出你想要的东西——而且它是可移植的!由于跨平台的原因,我已经不得不将一些旧的asm例程重写回C,这并不有趣。

不,我将坚持使用C,并处理与使用HLL获得的生产时间相比偶尔出现的轻微性能放缓。


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

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

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


我只能回答为什么我个人不经常用汇编编写程序,主要原因是这样做比较乏味。此外,我认为在没有立即注意到的情况下,更容易把事情搞砸。例如,你可能在一个例程中改变了使用寄存器的方式,但在另一个地方却忘记了这一点。它会很好地组装起来,直到很久以后你才会注意到。

也就是说,我确实认为组装仍然有有效的用途。例如,我有许多非常优化的汇编例程来处理大量数据,使用SIMD并遵循偏执的“每一个比特都是神圣的”[引用V.Stob]方法。(但请注意,简单的程序集实现通常比编译器为您生成的要糟糕得多。)


你好,我是一个编译器。

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.

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


当你将汇编语言与比C更高级的语言(如Java、Python或Ruby)进行比较时,HLL的优势甚至更大。例如,这些语言有垃圾收集:不需要担心什么时候释放内存块,也不会因为过早释放而导致内存泄漏或错误。


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

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

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


作为一名大部分时间都在嵌入式编程领域工作的开发人员,我认为汇编语言还远远没有成为一种死亡/过时的语言。有某种接近金属级别的编码(例如,在驱动程序中)有时不能用高级语言准确或有效地表达。我们几乎所有的硬件接口例程都是用汇编程序编写的。

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.

最后,任何语言都有适用的时间和地点。人们使用他们最熟悉和最有成效的东西。在程序员的程序库中可能总会有汇编的位置,但是大多数程序员会发现他们可以用一种高级语言编写代码,这种语言在更少的时间内几乎同样高效。


浏览这些答案,我敢打赌9/10的回复者从未使用过组装。

这是一个经常出现的老问题,你得到的答案都是一样的,而且大多是错误的答案。如果不是为了便携性,我仍然会自己组装所有的东西。即便如此,我还是用C编写代码,就像用汇编一样。


C是一个宏汇编器!这是最好的一个!

它可以做几乎所有汇编程序可以做的事情,它是可移植的,在大多数极少数情况下,当它不能做一些事情时,你仍然可以使用嵌入式汇编代码。这就只留下了一小部分程序,你绝对需要用汇编来编写,而且只需要汇编。

更高层次的抽象和可移植性使大多数人更值得用c语言编写系统软件。尽管你现在可能不需要可移植性,但如果你在编写某个程序上投入了大量的时间和金钱,你可能不想把自己限制在将来可以使用它的地方。


我猜即使是x86(_64)上的ASM也有意义,因为您可以利用编译器难以优化的指令来获得很多好处。以X264为例,它使用了大量的asm编码,速度增益是巨大的。


为什么?简单。

比较一下:

        for (var i = 1; i <= 100; i++)
        {
            if (i % 3 == 0)
                Console.Write("Fizz");
            if (i % 5 == 0)
                Console.Write("Buzz");
            if (i % 3 != 0 && i % 5 != 0)
                Console.Write(i);
            Console.WriteLine();
        }

with

.locals init (
    [0] int32 i)
L_0000: ldc.i4.1 
L_0001: stloc.0 
L_0002: br.s L_003b
L_0004: ldloc.0 
L_0005: ldc.i4.3 
L_0006: rem 
L_0007: brtrue.s L_0013
L_0009: ldstr "Fizz"
L_000e: call void [mscorlib]System.Console::Write(string)
L_0013: ldloc.0 
L_0014: ldc.i4.5 
L_0015: rem 
L_0016: brtrue.s L_0022
L_0018: ldstr "Buzz"
L_001d: call void [mscorlib]System.Console::Write(string)
L_0022: ldloc.0 
L_0023: ldc.i4.3 
L_0024: rem 
L_0025: brfalse.s L_0032
L_0027: ldloc.0 
L_0028: ldc.i4.5 
L_0029: rem 
L_002a: brfalse.s L_0032
L_002c: ldloc.0 
L_002d: call void [mscorlib]System.Console::Write(int32)
L_0032: call void [mscorlib]System.Console::WriteLine()
L_0037: ldloc.0 
L_0038: ldc.i4.1 
L_0039: add 
L_003a: stloc.0 
L_003b: ldloc.0 
L_003c: ldc.i4.s 100
L_003e: ble.s L_0004
L_0040: ret 

它们在功能上是相同的。 第二个甚至不是汇编程序,而是。net IL(中间语言,类似于Java的字节码)。第二次编译将IL转换为本地代码(即几乎是汇编程序),使其更加神秘。


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


Oh hai, I'm a dataflow system. This App I'm running, are full of varrrious components. It's a distributed app, and it resides in 3 computers, on a powerful x86 and two smaller ARMs. Most of the components are written in C++, but there is a critical one written in ASM for the x86. Also, most components have several variations: compiled for different processors, also some components have special GPU versions. Shame or not, I have a script component (a wrapper component calls a script), which prints report, but only once a year. It doesn't even hurt that it's just a script, a slow script.

作为一个智能数据流应用程序,我知道,我的架构只适合特定的任务,其中信号/数据通过图形流动,例如自动化,视频/图像处理,音频处理(所有合成器使用数据流),但幸运的是,这些应用领域是非常需要功率的,其中优化是必不可少的。

我很确定有一天会出现一些其他的架构,它们也是关于优化(以及其他让编程更容易等的东西),它们将能够覆盖更多或其他领域,而我,数据流不能。

因此,“C vs ASM”的话题并不是一个真正的困境。这就像是在争论数字合成器还是模拟合成器更好(就像我之前提到的,我一直在研究合成器)。我建议,做好听的音乐。或倾听。无论什么。C不是相对于ASM。无论如何,我从未见过一个C程序被ASM攻击,反之亦然。

对不起,我的英语不好,尽管我不是一个新技术,我不是广为人知,我是一个孩子。很有希望。来,看看我的资料!


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

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

而不是

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

你还是写吧

B823010000
0556040000
50 
FF15.....

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

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