简体   繁体   English

如何对64位寄存器的低32位进行BSWAP?

[英]How to BSWAP the lower 32-bit of 64-bit register?

I've been looking for the answer for how to use BSWAP for lower 32-bit sub-register of 64-bit register. 我一直在寻找有关如何将BSWAP用于64位寄存器的低32位子寄存器的答案。 For example, 0x0123456789abcdef is inside RAX register, and I want to change it to 0x01234567efcdab89 with a single instruction (because of performance). 例如, 0x0123456789abcdef位于RAX寄存器中,我想用一条指令将其更改为0x01234567efcdab89 (由于性能)。

So I tried following inline function: 所以我尝试了以下内联函数:

#define BSWAP(T) {  \
    __asm__ __volatile__ (  \
            "bswap %k0" \
            : "=q" (T)  \
            : "q" (T)); \
}

And the result was 0x00000000efcdab89 . 结果是0x00000000efcdab89 I don't understand why the compiler acts like this. 我不明白为什么编译器会这样。 Does anybody know the efficient solution? 有人知道有效的解决方案吗?

Ah, yes, I understand the problem now: 嗯,是的,我现在明白了这个问题:

the x86-64 processors implicitly zero-extend the 32-bit registers to 64-bit when doing 32-bit operations (on %eax, %ebx, etc). x86-64处理器在执行32位操作(%eax,%ebx等)时, 会将 32位寄存器隐式扩展为64位。 This is to maintain compatibility with legacy code that expects 32-bit semantics for these registers, as I understand it. 据我了解,这是为了与希望这些寄存器具有32位语义的旧代码保持兼容性。

So I'm afraid that there is no way to do ror on just the lower 32 bits of a 64-bit register. 因此,恐怕无法对64位寄存器的低32位执行ror了。 You'll have to do use a series of several instructions... 您必须使用一系列的一些说明...

Check the assembly output generated by gcc! 检查gcc生成的程序集输出! Use the gcc -s flag to compile the code and generate asm output. 使用gcc -s标志来编译代码并生成asm输出。

IIRC, x86-64 uses 32-bit integers by default when not explicitly directed to do otherwise, so this may be (part of) the problem. IIRC,x86-64在没有明确指示的情况下默认情况下使用32位整数,因此这可能是问题的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM