在x86架构指令译码 [英] Instruction Decoding in x86 architecture

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

问题描述

我工作的一个操作系统项目为我的实验室,我已经指令指针和指令操作code工作。现在,我需要知道的是它是什么类型的指令。对于我读通过指令指针所指向的地址中的数据。从这些数据中的第一个字节给我的指令类型。例如,如果第一个字节是 0xC6 这是一个 MOVB 指令。现在也有一些情况下,当指令指针的第一个字节是为0x0F 。根据文档为0x0F 这意味着它是一个两字节的指令。我的问题是这种类型的指令。我不知道如何找出两个字节的指令的指令类型。

I am working on a operating system project for my lab where I've to work with the instruction pointer and instruction opcode. Right now all I need to know is what type of instruction it is. For that I'm reading the data from the address pointed by instruction pointer. The first byte from this data gives me the instruction type. For example if first byte is 0xC6 it is a MOVB instruction. Now there are some cases when the first byte of instruction pointer is 0x0F. According to documentation 0x0F which means it is a two byte instruction. My problem is with this type of instruction. I'm not sure how to find out the instruction type for two byte instruction.

在我的第2优先级是二找出指令的操作数。我不这样做,从code的知识。任何样品code将pciated AP $ P $

After that my 2nd priority is two find out the operands of the instruction. I've no knowledge of doing that from code. Any sample code will be appreciated

三来,需要找出指令的大小。由于86的长度是可变的,我想知道的每一个指令的大小。起初,我打算用一个查找表,其中我会保持指令名称和它的大小。但后来我发现,相同的指令具有可变长度。例如,当我使用的对象转储在的.o 文件我发现了两个指令 C6 00 62 这是 MOVB $ 0X62,(%EAX)&安培; 85 C6 2C FF FF FF 00 这是 MOVB $为0x0,-0xD4(EBP%)。瞧这两个指令类型相同( C6 ),但具有不同的长度。

Third comes the need to find out the size of the instruction. As x86 is variable length, I want to know the size of each instructions. At first I planned to use a look up table where I'll maintain the instruction name and its size. But then I discovered that the same instruction can have variable length. For example when I used object dump on a .o file I found two instruction C6 00 62 which is for MOVB $0x62,(%EAX) & C6 85 2C FF FF FF 00 which is for MOVB $0x0,-0xD4(%EBP). Look here both instruction type is same(C6) but the are of different length.

所以我需要这些问题的答案的。这将是非常美联社preciated,如果有人可以给我一些解决方案。

So I'm in need of answers to those questions. It'll be highly appreciated if someone can give me some solutions.

推荐答案

基本上你需要设置嵌套case语句,实施有限状态机的扫描仪,其中每个级别检查一些字节的内容(通常从左到右)运算的code,以确定它做什么。

Basically what you need is set of nested case statements, implementing a finite state machine scanner, where each level inspects some byte (typically left to right) of the opcode to determine what it does.

您顶级case语句将pretty多是256的情况下,为每个运算code字节;
你会发现一些运算codeS(尤其是所谓的preFIX字节)导致顶层循环(捡拾多preFIX字节precede主运code字节)。子的情况下会根据在x86的运算code结构获得结构;你几乎肯定会以MODRM结束和SIB寻址模式字节去codeRS /子程序。

Your top level case statement will pretty much be 256 cases, one for each opcode byte; you'll find some of the opcodes (especially the so-called "prefix" bytes) cause the top level to loop (picking up multiple prefix bytes the precede main opcode byte). Sub cases will acquire structure according the opcode structure of the x86; you'll almost certainly end up with a MODRM and SIB addressing mode byte decoders/subroutines.

我已经做到了这一点;工作是烦人,因为细节,但并不难。您可以在几百行code的获得pretty很好的解决方案,如果你是细心。如果你坚持这样整个指令集,你很可能有更大的东西落得(向量寄存器和OP codeS,尤指为Haswell的等);英特尔一直干扰指令到每一个黑暗的角落都可以找到。

I've done this; the work is annoying because of details but not hard. You can get a pretty good solution in several hundred lines of code if you are careful. If you insist on doing the whole instruction set (vector registers and opcodes, esp. for haswell etc.) you're likely to end up with something bigger; Intel has been jamming instructions into every dark corner they can find.

您真的需要一个运算code图;我是pretty肯定有一个在英特尔手册。我发现这个链接是pretty有用: HTTP:// WWW。 ref.x86asm.net/$c$cr32.html

You really need an opcode map; I'm pretty sure there is one in the Intel manuals. I've found this link to be pretty useful: http://www.ref.x86asm.net/coder32.html

编辑2015年9月:在这里,在我SO提供C code实现这一点:
http://stackoverflow.com/a/23843450/120163

EDIT Sept 2015: Here at SO I provide C code that implements this: http://stackoverflow.com/a/23843450/120163

这篇关于在x86架构指令译码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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