本文介绍了Intel x86_64汇编:将32位整数转换为64位int的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说。
我这样做:

As title says.My way to do this:

; eax holds 32bit integer
sal rax, 32
sar rax, 32
;after operation int is converted (rax holds the same value on 64 bits)

有更优雅/更好/更快的方法吗?

Is there more elegant/better/faster way to do this??

推荐答案

TL; DR movsxd ,或者对于eax特例, cdqe

TL;DR: movsxd, or for the eax special-case, cdqe.

就像我在你的一个,您可以通过查看编译器自己回答这些问题输出。 真的很容易编写一个符号扩展整数的函数,然后查看它在手册中使用的指令:

Like I explained in one of your previous questions, you can answer these yourself by looking at compiler output. It's really easy to write a function that sign-extends an integer, then go look up the instruction it uses in the manual:

#include <stdint.h>
int64_t sign_extend(int32_t a) { return a; }

    movsxd   rax, edi
    ret

来自 tp://www.felixcloutier.com/x86/\"rel =nofollow noreferrer>指令集参考,你就完成了。 (这是英特尔PDF的转换(。 8086没有 movsx 直到386,只有al - > ax cbw

There's a special-case for rax, called cdqe. 8086 didn't have movsx until 386, just the al -> ax cbw.

这是和其他只有ax的东西是为什么英特尔在 8086.AMD可能应该为AMD64收回这些内容,用于未来的指令集扩展,因为AMD64是第一个(并且很可能只是很长一段时间)打破向后兼容8086操作码或386的32位模式机器代码的机会。

This is and other ax-only stuff is why Intel spent 8 opcodes on single-byte encodings for xchg ax, reg in 8086. AMD arguably should have reclaimed these for AMD64, for use in future instruction-set extensions, since AMD64 was the first (and probably only for a very long time) opportunity to break backwards compatibility with 8086 opcodes, or 386's 32bit-mode machine-code.

这是单字节NOP( 90 )编码的来源: xchg eax,eax 被认为是一个有效执行的特例(不是免费的,但比 mov eax,eax 便宜)。在64位模式下,它不会将eax零扩展到rax中。 (英特尔文件为一个单独的指令(以及长NOP编码),并没有在)

This is where the single-byte NOP (90) encoding comes from: xchg eax,eax is recognized as a special-case that's executed efficiently (not free, but cheaper than mov eax,eax for example). In 64bit mode, it doesn't zero-extend eax into rax. (Intel documents nop as a separate instruction (along with the long-NOP encoding), and doesn't mention this special-case behaviour in the entry for xchg)

这篇关于Intel x86_64汇编:将32位整数转换为64位int的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 15:16