MIPS伪指令,替代品 [英] MIPS Pseudo istructions, replacements
问题描述
研究了什么之后,我发现这只是获得相同结果的方式的替代.如果我错了,请纠正我.
After making research what is it, I found out that it is simply replacment of way of getting same result. Correct me please if I'm wrong.
示例:
move $s0, $t1
可以替换为:
add $s0, $zero, $t1
问题:
如何替换lw
,la
,sw
,bne
?
推荐答案
是的,move
指令可以并且由add
指令代替.
Yes, the move
instruction can and is replaced with an add
instruction.
伪指令的某些背景:MIPS是RISC(精简指令集计算机)体系结构,这意味着您可以使用的指令数量相对较少.这种简单性导致更快的性能,但是要权衡的是,更复杂的操作需要多个指令.伪指令是伪"指令,代表一个或多个其他更复杂的操作.
Some background on pseudo instructions: MIPS is a RISC (Reduced Instruction Set Computer) architecture, meaning there is a relatively small number of instructions that you are able to use. This simplicity results in faster performance, but the trade-off is that more complicated operations require multiple instructions. Pseudo instructions are "fake" instructions that represent one or more other more complex operations.
关于您的问题:
lw
,sw
和bne
不是伪指令.它们全部由一条MIPS汇编指令执行.
lw
, sw
, and bne
are not pseudo instructions. They are all executed by one MIPS assembly instruction.
la
或加载地址是伪指令. la
可以分解为lui
指令和ori
指令.在32位MIPS架构上,每个指令以及每个寄存器的大小均为32位.因此,要存储32位地址,您必须首先获取最高有效(高阶)的16位,然后再获取最低有效(低阶)的16位.
la
, or Load Address, is a pseudo instruction. la
can be broken down into a lui
instruction and an ori
instruction. On a 32-bit MIPS architecture, each instruction as well as the size of each register is 32 bits. So in order to store a 32 bit address, you must first grab the most significant (high order) 16 bits first, and then take the least significant (low order) 16 bits afterward.
lui
(即立即加载上位立即数)获取立即数字段并将其左移16次,并将其存储在临时汇编程序寄存器中. ori
指令按位或在临时寄存器上执行立即数,并将完整地址存储在la
指令中指定的初始寄存器中.
The lui
, or Load Upper Immediate, takes the immediate field and shifts it left 16 times and stores it in a temporary assembler register. The ori
instruction does a bitwise or on the temporary register and an immediate value and stores the full address in the initial register specified in the la
instruction.
编辑:例如,要获取字符串的地址,您可以在函数中使用以下代码段:
To get the address of a string, for example, you might use this code segment in your function:
la $a0, msg # pseudo-instruction to load the address of the label str
您还将在其他地方定义msg
:
You would also have msg
defined elsewhere:
.data
msg: .asciiz "This is a string"
在SPIM中运行此示例后,la
指令被转换为:
After running this example in SPIM, the la
instruction gets translated into:
lui $1, 4097 [msg]
ori $4, $1, 0 [msg]
$1
是临时汇编器寄存器,$4
是寄存器a0
,这是传递给初始la
指令的参数.
$1
is the temporary assembler register and $4
is register a0
which was the argument passed into the initial la
instruction.
参考文献: MIPS指令集,从只是做了很多MIPS.尝试在 QTSPIM 这样的模拟器中运行每条指令,然后看看会得到什么.
References: MIPS Instruction Set and from just doing a lot of MIPS. Try running each instruction in a simulator like QTSPIM and see what you get.