gas vs. nasm: which assembler produces the best code?

折月煮酒 提交于 2019-12-03 12:48:27

When you're writing in assembler, you are precisely describing the instructions to generate so it doesn't depend on the assembler. It depends on you. There's a one-to-one correspondence between the mnemonics you write and actual instructions in machine code.

I don't know about these two specific tools, but there are some instructions that can be encoded differently:

  • ADD AX,1 is either 05 01 or 81 c0 01 or fe c0
  • INT 3 is either cc or cd 03
  • New AVX instructions that extend two-byte SSE instructions will either have a 2-byte or 3-byte prefix. All 2-byte prefixes can be encoded as 3-byte prefixes as well.

These are just a few examples off the top of my head of how assemblers can encode the same instruction differently, so the question does in fact make sense.

As a sidenote on the syntax-matter. You can have GAS work perfectly fine with Intel syntax by putting the following line at the top of your source file:

.intel_syntax noprefix

I am using Intel syntax too for all my assmebly needs. It seems far more natural than the AT&T syntax. And it saves some keystrokes :-).

It is assember... it does not optimize code. It just translates as is. So the fastest and cleanest code is produced by programmer or compiler

Obviously nasm because Intel syntax looks much cleaner than AT&T syntax.

@Brian: that was not the question ...

@cyber98834: Well, an assembler does what every assembler must do : translate every instruction to its opcode .

There's no optimization .

Oh and also, there's not such a thing as a "fastest code" ... Can I ask you a question ? The CPU's speed is static, isn't it ?

So, you can't make a code run faster because you can't change the CPU's speed .

But, you can shrink the code so that the CPU handles less amount of instructions, and so takes less time to run .

I hope you understand what I'm trying to say .

I suggest you to buy ( or to look for some pdf's, but I don't know if that's legal ) Michael Abrash's Graphics Programming Black Book which covers many optimization lessons .

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!