为什么Solaris汇编程序生成的机器代码与GNU汇编程序不同?

fuz*_*fuz 5 linux assembly solaris x86-64

我为amd64编写了这个小程序集文件.代码的作用对于这个问题并不重要.

        .globl fib

fib:    mov %edi,%ecx
        xor %eax,%eax
        jrcxz 1f
        lea 1(%rax),%ebx

0:      add %rbx,%rax
        xchg %rax,%rbx
        loop 0b

1:      ret
Run Code Online (Sandbox Code Playgroud)

然后我继续组装,然后在Solaris和Linux上反汇编.

的Solaris

$ as -o y.o -xarch=amd64 -V y.s                            
as: Sun Compiler Common 12.1 SunOS_i386 Patch 141858-04 2009/12/08
$ dis y.o                                                  
disassembly for y.o


section .text
    0x0:                    8b cf              movl   %edi,%ecx
    0x2:                    33 c0              xorl   %eax,%eax
    0x4:                    e3 0a              jcxz   +0xa      <0x10>
    0x6:                    8d 58 01           leal   0x1(%rax),%ebx
    0x9:                    48 03 c3           addq   %rbx,%rax
    0xc:                    48 93              xchgq  %rbx,%rax
    0xe:                    e2 f9              loop   -0x7      <0x9>
    0x10:                   c3                 ret    
Run Code Online (Sandbox Code Playgroud)

Linux的

$ as --64 -o y.o -V y.s
GNU assembler version 2.22.90 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.22.90.20120924
$ objdump -d y.o

y.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <fib>:
   0:   89 f9                   mov    %edi,%ecx
   2:   31 c0                   xor    %eax,%eax
   4:   e3 0a                   jrcxz  10 <fib+0x10>
   6:   8d 58 01                lea    0x1(%rax),%ebx
   9:   48 01 d8                add    %rbx,%rax
   c:   48 93                   xchg   %rax,%rbx
   e:   e2 f9                   loop   9 <fib+0x9>
  10:   c3                      retq   
Run Code Online (Sandbox Code Playgroud)

生成的机器代码怎么会有所不同?太阳作为产生8b cfmov %edi,%ecx,而气体产生89 f9的非常相同的指令.这是因为在x86下编码相同指令的各种方法还是这两种编码真的有特别的区别吗?

Dre*_*wen 6

一些x86指令有多个编码,它们做同样的事情.特别是,任何作用于两个寄存器的指令都可以交换寄存器并反转指令中的方向位.

给定的汇编器/编译器选择哪一个只取决于工具作者选择的内容.