简体   繁体   English

为什么我们不能将寄存器与立即数(Assembly-MIPS)进行比较(bne-beq)?

[英]Why we cant compare (bne-beq) a register with an immediate (Assembly-MIPS)?

For example on add we have addi for adding a register and an immediate,why on this case we cant have bnei or beqi... 例如在add上我们有addi用于添加寄存器和立即数,为什么在这种情况下我们不能有bnei或beqi ...

Im supposed to answer on that,but im not sure...any help? 我应该回答这个问题,但是我不确定...有什么帮助吗?

The reason is the instruction encoding: 原因是指令编码:

Both ADDI and BNE/BEQ are I-Type instructions . ADDIBNE/BEQ都是I型指令 But whereas the immediate field in the ADDI instruction is used for storing the immediate operand for the addition, it's used for storing the branch offset in the case of BEQ/BNE . 但是,尽管ADDI指令中的立即数字段用于存储加法的立即数操作数,但在BEQ/BNE的情况下,它用于存储分支偏移量。

There may be MIPS assemblers which allow you to use immediate operands in conditional branch instructions, but they will expand those pseudo-instructions into multiple actual instructions. 可能会有MIPS汇编器,使您可以在条件分支指令中使用立即操作数,但是它们会将那些伪指令扩展为多个实际指令。

In architectures with flags, the branch instructions usually follow a compare instruction which can compare registers and immediates and set the appropriate flags (often called CMP, but often other instructions can set flags, too). 在具有标志的体系结构中,分支指令通常遵循比较指令,该比较指令可以比较寄存器和立即数并设置适当的标志(通常称为CMP,但通常其他指令也可以设置标志)。 The branch then only checks the flags. 然后,分支仅检查标志。

(This was posted before the question was clarified to MIPS, which doesn't use flags) (这是在问题未告知MIPS之前发布的,MIPS不使用标志)

You don't want to use the 16 bit immediate value as an address to jump to because that limits where you can jump. 您不想使用16位立即数作为要跳转到的地址,因为这限制了您可以跳转的位置。 MIPS has 32 bits for an address. MIPS具有32位地址。 If you only use 16 bits, you are using a very small fraction of the possible addresses. 如果仅使用16位,则使用的地址可能很小。

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

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