x86指令编码如何选择操作码 [英] x86 instruction encoding how to choose opcode

查看:127
本文介绍了x86指令编码如何选择操作码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Intel指令集参考手册中为x86-64编码指令 cmpw%ax -5 时,我有两个操作码可供选择:

When encode instructioncmpw %ax -5 for x86-64, from Intel-instruction-set-reference-manual, I have two opcodes to choose:

3D iw CMP AX, imm16 I Valid Valid Compare imm16 with AX.
83 /7 ib CMP r/m16, imm8 MI Valid Valid Compare imm8 with r/m16.

因此会有两个编码结果:

So there will be two encoding results:

66 3d fb ff ; this for opcode 3d
66 83 f8 fb ; this for opcode 83

那么哪个更好?

我在下面尝试了一些在线反汇编程序

I tried some online-disassembler below

  • https://defuse.ca/online-x86-assembler.htm#disassembly2 https://onlinedisassembler.com/odaweb/

两者都可以反汇编为原点指令。但是为什么 6683fb00 也能工作,而 663dfb 却不起作用。

Both can disassemble to origin instruction. But why 6683fb00 also works and 663dfb doesn't.

推荐答案

这两种编码的长度相同,因此无助于我们做出决定。

Both encodings are the same length, so that doesn't help us decide.

但是,如@Michael Petch所评论, imm16 编码将导致Intel CPU的解码器中的LCP停顿。 (由于没有 66 操作数大小的前缀,它将是 3D imm32 ,因此操作数大小的前缀会更改指令 rest 的长度,这就是为什么它被称为长度更改前缀停顿。AFAIK,使用32位立即数,您将在16位代码中获得相同的停顿。)

However, as @Michael Petch commented, the imm16 encoding will cause an LCP stall in the decoders on Intel CPUs. (Because without the 66 operand-size prefix, it would be 3D imm32, so the operand-size prefix changes the length of the rest of the instruction. This is why it's called a Length-Changing-Prefix stall. AFAIK, you'd get the same stall in 16bit code for using a 32bit immediate.)

imm8 编码不会在我所知道的任何微体系结构上引起问题,因此,请予以支持。请参见 Agner Fog的microarch.pdf ,以及标签Wiki。

The imm8 encoding doesn't cause a problem on any microarchitecture I know of, so favour it. See Agner Fog's microarch.pdf, and other links from the x86 tag wiki.

使用较长的指令来避免LCP停转。 (例如,如果您知道寄存器的高16位为零或符号扩展,则使用32位操作数大小可以避免LCP停顿。)

It can be worth using a longer instruction to avoid an LCP stall. (e.g. if you know the upper 16 bits of the register are zero or sign-extended, using 32bit operand size can avoid the LCP stall.)

Intel SnB系列CPU具有uop缓存,因此在执行之前不必总是对指令进行重新解码。不过,uop缓存很小,所以值得。

Intel SnB-family CPUs have a uop cache, so instructions don't always have to be re-decoded before executing. Still, the uop cache is small, so it's worth it.

当然,如果您要调整AMD,那么这并不是一个因素。我忘记了Atom和Silvermont解码器是否也有LCP停顿。

Of course, if you're tuning for AMD, then this isn't a factor. I forget if Atom and Silvermont decoders also have LCP stalls.

663d cmp ax,imm16 的前缀和操作码。 663dfb 不起作用,因为它消耗了以下指令的第一个字节。当解码器看到 66 3D 时,它将立即从指令流中获取接下来的2个字节。

663d is prefix+opcode for cmp ax, imm16. 663dfb doesn't "work" because it consumes the first byte of the following instruction. When the decoder see 66 3D, it grabs the next 2 bytes from the instruction stream as the immediate.

这篇关于x86指令编码如何选择操作码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆