缓冲区中的 SSE 指令
如果我有 x86 的指令缓冲区,是否有一种简单的方法可以检查指令是否是 SSE 指令,而不必检查操作码是否在 SSE 指令的范围内?我的意思是是否有可以检查的通用指令前缀或处理器状态(例如寄存器)?
If I have an instruction buffer for x86 is there an easy way to check if an instruction is an SSE instruction without having to check if the opcode is within the ranges for the SSE instructions? By this I mean is there a common instruction prefix or processor state (such as a register) that can be checked?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(已更新)
根据您如何定义简单,答案是
是或否:)指令格式在 Intel 64 和 IA-32 架构软件开发人员手册
第 2A 卷和第 2B 卷合并:指令集参考,AZ。有问题的部分之一是前缀。其中一些对于某些 SSE 指令 (
66
F2
F3
) 是必需的,而它们对于其他操作码具有不同的含义(操作数大小覆盖、REPNZ
和REPZ
)。要了解如何使用前缀来区分不同的指令,请考虑将两个 xmm 寄存器相加的这 4 种形式(通过 objdump -D -b binary -m i386:x86-64:intel --insn-width 获得的输出=12):
似乎默认是添加两个单精度标量,
66
(通常:操作数大小覆盖前缀)选择双精度版本,F3
(repz
) 选择打包的单版本,最后F2
(repnz
) 选择打包的双版本。此外,它们有时可以组合在一起,在 64 位模式下,您还必须担心 REX 前缀 (第 2-9 页)。下面的示例是 64 位模式下具有不同前缀的大致相同基本指令的不同版本。我不知道您是否关心 AVX 指令,但无论如何我都包含了一个作为示例:
因此据我所知,您始终必须循环遍历所有前缀来确定指令是否是 SSE 指令。
更新:
另一个复杂之处是存在仅 ModRM 编码不同的指令。考虑一下:
要找到这些以及所有其他可以编码的方式,最简单的方法是使用操作码映射。
因为我一直想考虑编写一个反汇编程序,所以我认为看看它需要做什么将是一个有趣的挑战。它应该找到大多数 SSE 指令,尽管显然我不能也不会保证它。我将上面的操作码映射转换为代码通过的一系列测试(tests.c - 太大而无法内联)。该代码测试一系列包含操作码编码的十六进制数字的文本字符串(它在第一个非十六进制数字处停止解析,字符串中的最后一个字符表示它是否是 SSE 指令)。
它首先扫描所有前缀,然后使用操作码表来测试指令是否与额外逻辑匹配,以处理多字节操作码所需的嵌套表以及匹配后续 modrm 字节中的数字的需要。
sseDetect.c:
inst_table.h:
(Updated)
Depending on how you define easy the answer is
either yes orno :)The instruction format is described in section 2 of the Intel 64 and IA-32 Architectures Software Developer's Manual
Combined Volumes 2A and 2B: Instruction Set Reference, A-Z. One of the problematic parts is the prefixes. Some of these are mandatory for some SSE instructions (
66
F2
F3
), while they have a different meaning for other opcodes (operand size override,REPNZ
andREPZ
).To see how the prefixes are used to distinguish between different instructions, consider these 4 forms of adding two xmm registers together (output obtained with
objdump -D -b binary -m i386:x86-64:intel --insn-width=12
):It seems that the default is to add two single precision scalars,
66
(normally: operand size override prefix) selects the double precision version,F3
(repz
) selects the packed single version and finallyF2
(repnz
) selects the packed double version.Additionally they can sometimes be combined and in 64-bit mode you also have to worry about the REX prefix (pg. 2-9). Here is an example are different versions of roughly the same base instructions with different prefixes in 64-bit mode. I don't know if you care about AVX instructions but I included one anyway as an example:
So as far as I can see you will always have to loop over all prefixes to determine if an instruction is an SSE instruction.
Update:
An additional complication is the existence of instructions that only differ in their ModRM encoding. Consider:
To find these and all the other ways they can be encoded it's easiest to use an opcode map.
Because I've been meaning to look at writing a disassembler anyway I figured it would be a fun challenge to see what it takes. It should find most SSE instructions, though obviously I can't and won't guarantee it. I transformed the above opcode map into a series of tests that the code passes (tests.c - too big for inlining). The code tests a series of text strings containing hex digits of the opcode encoding (it stops parsing at the first non-hex digit, the last character in the string signifies whether it is an SSE instruction or not).
It first scans all prefixes, then uses the opcode tables to test if the instruction matches with extra logic to handle the nested tables needed for multi-byte opcodes and the need to match digits in the following modrm byte.
ssedetect.c:
inst_table.h:
没有明确的 SSE 前缀。有些SSE指令以0F开头,有些以F3开头,但并非所有0F和F3指令都是SSE指令。您需要一个更全面的解码器来判断指令是否是 SSE。由于 x86 指令是可变长度的,因此您无论如何都需要它。
There's no unambiguous SSE prefix. Some SSE instructions start with 0F and some with F3 but not all 0F and F3 instructions are SSE instructions. You'll need a more comprehensive decoder to tell whether an instruction is SSE. Since x86 instructions are variable-length, you'd need that anyway.