在x86 Assembly中将Little Endian四字节长的整数转换为Big Endian的最佳方法,反之亦然

tan*_*rwj 2 x86 assembly

所以我想出了如何在C语言中反转一个四字节长的int的字节序:

unsigned int swapWord (unsigned int n){
   return (n>>24) | ((n<<8) & 0x00FF0000) |((n>>8) & 0x0000FF00) | (n<<24);
}
Run Code Online (Sandbox Code Playgroud)

现在,我知道我可以反汇编它了,而且确实做到了,这是不错的代码,但是我敢肯定,有一种更有效的方法可以逆转固有性。有任何想法吗?

编辑:这是反汇编的代码:

00000000004005a3 <swapWord>:
      4005a3:   55                      push   %rbp
      4005a4:   48 89 e5                mov    %rsp,%rbp
      4005a7:   89 7d fc                mov    %edi,-0x4(%rbp)
      4005aa:   8b 45 fc                mov    -0x4(%rbp),%eax
      4005ad:   c1 e8 18                shr    $0x18,%eax
      4005b0:   89 c2                   mov    %eax,%edx
      4005b2:   8b 45 fc                mov    -0x4(%rbp),%eax
      4005b5:   c1 e0 08                shl    $0x8,%eax
      4005b8:   25 00 00 ff 00          and    $0xff0000,%eax
      4005bd:   09 c2                   or     %eax,%edx
      4005bf:   8b 45 fc                mov    -0x4(%rbp),%eax
      4005c2:   c1 e8 08                shr    $0x8,%eax
      4005c5:   25 00 ff 00 00          and    $0xff00,%eax
      4005ca:   09 c2                   or     %eax,%edx
      4005cc:   8b 45 fc                mov    -0x4(%rbp),%eax
      4005cf:   c1 e0 18                shl    $0x18,%eax
      4005d2:   09 d0                   or     %edx,%eax
      4005d4:   5d                      pop    %rbp
      4005d5:   c3                      retq   
      4005d6:   66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
      4005dd:   00 00 00 
Run Code Online (Sandbox Code Playgroud)

Bre*_*dan 5

对于80486及更高版本,有一条称为的指令可bswap在dword中交换字节。

对于80386较旧的CPU,我很想使用如下代码:

                     ;eax = 0xAABBCCDD
    xchg ah,al       ;eax = 0xAABBDDCC
    rol eax,16       ;eax = 0xDDCCAABB
    xchg ah,al       ;eax = 0xDDCCBBAA
Run Code Online (Sandbox Code Playgroud)

对于甚至更旧的CPU(8086至80286),也必须使用一对16位寄存器(因为没有32位寄存器),结果如下:

                     ;dx:ax = 0xAABBCCDD
                     ;ax:dx = 0xCCDDAABB
    xchg ah,al       ;ax:dx = 0xDDCCAABB
    xchg dh,dl       ;ax:dx = 0xDDCCBBAA
Run Code Online (Sandbox Code Playgroud)

注意:xchg ah,al可以使用ror ax,8或代替使用rol ax,8。我怀疑您使用这3条指令中的哪条会有很大差异。