CPU/汇编器如何知道下一条指令的大小? [英] How does the CPU/assembler know the size of the next instruction?

查看:190
本文介绍了CPU/汇编器如何知道下一条指令的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了举例,假设我正在构建虚拟机.我有一个字节数组和一个while循环,如何知道从字节数组读取多少个字节以用于下一条指令来解释类似Intel-8086的指令?

For the sake of example, imagine I was building a virtual machine. I have a byte array and a while loop, how do I know how many bytes to read from the byte array for the next instruction to interpret an Intel-8086-like instruction?

CPU在指令指针处读取操作码,对于8086和CISC,您有一个字节和两个字节的指令.我怎么知道下一条指令是F还是FF?

The CPU reads the opcode at the instruction pointer, with 8086 and CISC you have one byte and two byte instructions. How do i know if the next instruction is F or FF?

我在 http://www.swansontec.com/sintel上的这篇文章中自己找到了答案. html

操作码或操作码位于任何可选的前缀之后.操作码告诉处理器要执行哪条指令.此外,操作码还包含一些位字段,这些位字段描述了期望的操作数的大小和类型.例如,NOT指令的操作码为1111011w.在此操作码中,w位确定操作数是字节还是字. OR指令的操作码为000010dw.在此操作码中,d位确定哪些操作数是源和目标,而w位又确定大小.一些指令具有几种不同的操作码.例如,当将OR与累加器寄存器(AX或EAX)和一个常数一起使用时,它具有特殊的节省空间的操作码0000110w,从而无需单独的ModR/M字节.从大小编码的角度来看,不需要存储确切的操作码位.大致了解对于特定指令可用的操作码类型更为重要.

The operation code, or opcode, comes after any optional prefixes. The opcode tells the processor which instruction to execute. In addition, opcodes contain bit fields describing the size and type of operands to expect. The NOT instruction, for example, has the opcode 1111011w. In this opcode, the w bit determines whether the operand is a byte or a word. The OR instruction has the opcode 000010dw. In this opcode, the d bit determines which operands are the source and destination, and the w bit determines the size again. Some instructions have several different opcodes. For example, when OR is used with the accumulator register (AX or EAX) and a constant, it has the special space-saving opcode 0000110w, which eliminates the need for a separate ModR/M byte. From a size-coding perspective, memorizing exact opcode bits is not necessary. Having a general idea of what type of opcodes are available for a particular instruction is more important.

推荐答案

cpu只是对指令进行解码.在8086的情况下,第一个字节告诉处理器要获得多少.它不必一定是第一个字节,第一个字节必须以某种方式表明您需要获取更多,更多则可以表明您需要更多.对于x86系列这样的8位指令集,您需要从一个字节开始,然后查看还需要多少字节,而且还没有对齐,就必须将指令流视为字节流才能对其进行解码.

the cpu simply decodes the instruction. IN the case of 8086 the first byte tells the processor how much more to get. It doesnt have to be the first byte the first byte does have to indicate in some way that you need to get more, that more can indicate you need even more. With 8 bit instruction sets like the x86 family where you start with one byte and then see how much more you need, and also being unaligned, you have to treat the instruction stream as a bytestream in order to decode it.

您应该为自己编写一个非常简单的指令集模拟器,只有少量指令,也许足以加载寄存器,向其中添加内容然后循环.对您要理解的内容非常有教育意义,如果要写的话,可能要花半个小时.

You should write yourself a very simple instruction set simulator, only a handful of instruction, maybe enough to load a register, add something to it and then loop. extremely educational for what you are trying to understand, and takes maybe a half an hour if that to write.

这篇关于CPU/汇编器如何知道下一条指令的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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