简体   繁体   English

为什么 I 型立即数从 16 位扩展到 32 位?

[英]Why does the I-type immediate get extended from 16 bit to 32 bit?

I am preparing for a University Exam and i don't understand why the immediate field in the I-type gets extended from 16 bit to 32 bit is there an explanation I need to know or is it just something that you have to know that happens in the architecture without knowing why?我正在准备大学考试,我不明白为什么 I 类型中的直接字段从 16 位扩展到 32 位,我需要知道什么解释,还是只是您必须知道发生的事情在不知道为什么的架构中?

Also while calculating the branch address也在计算分支地址时

PC <– PC + 4 + (sign_ext(Imm16) << 2)

Why does the immediate get shifted by 2?为什么立即数会移位 2?

As already answered in the many comments...正如许多评论中已经回答的那样......

Although MIPS does have a 16 bit instruction format the instruction set everyone is familiar with is a 32 bit fixed length instruction set, on aligned boundaries.尽管 MIPS 确实有 16 位指令格式,但每个人都熟悉的指令集是 32 位固定长度指令集,在对齐的边界上。

The various operations use 32 bit operands.各种操作使用 32 位操作数。

You cannot have a 32 bit immediate in an instruction set that is fixed 32 bits;在固定为 32 位的指令集中不能有 32 位立即数; there would be no bits left for an opcode.没有位留给操作码。 So MIPS has different instruction formats with different immediate sizes to balance a rich array of instructions without as much pain at using immediates or forcing all immediates to be loaded from a nearby pool.因此,MIPS 具有不同的指令格式和不同的立即数大小,以平衡丰富的指令数组,而不会像使用立即数或强制从附近的池中加载所有立即数那样痛苦。 Worst case you can fall back on that but for a good number of things you can use the instructions as designed.在最坏的情况下,您可以依靠它,但是对于很多事情,您可以按照设计使用说明。

So the immediate fields are much less than 32 bits, but the operands they represent are generally 32 bits, so they need to be extended in some way, either shifted 16 and padded with zeros at the bottom or extended from the top unsigned or not (speaking generically not just for this type of mips) to make a 32 bit value.所以立即数字段远小于32位,但它们所代表的操作数一般都是32位,因此需要以某种方式进行扩展,要么移位16位并在底部填充零,要么从顶部扩展无符号或不(一般而言,不仅仅是针对这种类型的 mips)来生成 32 位值。

How the immediate is used should be looked up per instruction in the documentation.应根据文档中的说明查找立即数的使用方式。

Why do branches shift the immediate by 2?为什么分支将立即数移动 2?

The instructions are on aligned 32 bit boundaries which means the lower 2 bits are always zero.指令在对齐的 32 位边界上,这意味着低 2 位始终为零。 If you encoded those zeros into the immediate you would always have to make them zero and basically lose four times the possible range of your pc relative branch.如果您将这些零编码到立即数中,您将始终必须将它们设为零,并且基本上会丢失 pc 相对分支的可能范围的四倍。 Think of the immediate in this case to be the number of INSTRUCTIONS I want to branch rather than number of BYTES of address.在这种情况下,将立即数视为我想要分支的指令数,而不是地址的字节数。

Being signed allows for forward or back branching.签名允许向前或向后分支。

nextPC <– PC + 4 + (sign_ext(Imm16) << 2)

The + 4 is part of a pipeline illusion, different cores have their own pipe designs so there must be a standard to have the ISA not be crazy to use (very typical design choice across many ISAs to have the PC at execution time point at the next instruction). + 4 是管道错觉的一部分,不同的内核有自己的管道设计,所以必须有一个标准来让 ISA 不会疯狂使用(在许多 ISA 中非常典型的设计选择让 PC 在执行时间点在下一条指令)。 So nextPC is the PC of the next instruction plus the signed offset number of instructions in the immediate (by shifting left two making the lower two bits zero).所以 nextPC 是下一条指令的 PC 加上立即数中指令的有符号偏移量(通过左移两位使低两位为零)。 If the condition is met or if unconditional (I don't know all the I format instructions, I don't need to in order to answer this question) then nextPC is used by the logic otherwise PC + 4 is used for the next instruction.如果满足条件或无条件(我不知道所有的 I 格式指令,我不需要为了回答这个问题)那么逻辑使用 nextPC 否则 PC + 4 用于下一条指令.

Edit编辑

Yes, you are correct;是的,你是对的; the sign extension does already make it a 32 bit value.符号扩展已经使它成为一个 32 位值。 The additional shift is to maximize the range that the branch can take, giving us four times as far forward or backward to branch with this instruction format.额外的移位是最大化分支可以采用的范围,使我们可以使用这种指令格式向前或向后四倍地进行分支。 The upper sign extended bits are all either 0s or 1s and there are more than 2 bits of extension so shifting the temporary 32 bit value two more bits does no harm to that value.高符号扩展位都是 0 或 1,并且扩展位超过 2 位,因此将临时 32 位值再移动两位不会对该值造成损害。

With a 16 bit signed immediate if I wanted to not do that shift of two then 0x00007FFC is the maximum distance I could branch forward and to be a valid instruction the lower two bits would have to be zeros.使用 16 位带符号立即数,如果我不想进行两次移位,那么 0x00007FFC 是我可以向前分支的最大距离,并且要成为有效指令,低两位必须为零。 With the shift I can now branch 0x1FFFC forward and I can use all of the immediate bits.通过移位,我现在可以向前分支 0x1FFFC,并且可以使用所有立即数位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM