查找代码片段的偏移量
我对以下问题有点困惑,
考虑以下 MIPS 代码并回答后面的问题。
addi $t1, $s0, 400
loop: lw $s1, 0($s0)
add $s2, $s2, $s1
lw $s1, 4($s0)
add $s2, $s2, $s1
addi $s0, $s0, 8
bne $t1, $s0, loop
在条件分支中标签循环转换成什么值 指令?
现在我知道了分支目标地址的数学公式。但这里由于内存寻址没有完成,所以我通过计算目标地址和PC之间的行来找出偏移量。答案为 7(字偏移)。我的这种做法对吗?
I am a bit stuck up with the following question,
Consider the following MIPS code and answer the questions that follow.
addi $t1, $s0, 400
loop: lw $s1, 0($s0)
add $s2, $s2, $s1
lw $s1, 4($s0)
add $s2, $s2, $s1
addi $s0, $s0, 8
bne $t1, $s0, loop
What value is the label loop translated to in the conditional branch
instruction?
Now I know the mathematical formula for Branch Target Address. But here as memory addressing is not done so I found out the offset by counting the lines between the target address and PC. This gives the answer to be 7 (word offset). Am I right with this approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 MARS 模拟器进行快速实验 http://courses.missouristate.edu/KenVollmar/MARS/download .htm 给了我答案-6,-5表示行数差异,另一个-1,因为指令后PC增加了1。
A quick experiment with MARS simulator http://courses.missouristate.edu/KenVollmar/MARS/download.htm gave me the answer-6, -5 for number of lines difference and another -1 because PC is increased by 1 after the instruction.
AFAIK,恐怕不是。
正如 MIPS 指令参考所述:
据我了解,从分支指令到循环标签的距离是负数(因为标签在分支之前,因此地址较低)。距离以字数计算(因此左移 2 位)。由于所有 MIPS 指令都是 4 个字节,这之前将是 6 个指令,因此 -6 是应该出现在分支指令偏移量(低半字)中的值。二进制:1111 1111 1111 1010(二进制补码)。十六进制:FFFA。
通过模拟器检查,似乎我的推理是正确的,因为指令编码为 0x1530FFFA。
AFAIK, I'm afraid not.
As MIPS instruction reference says:
So as I understand, the distance from the branch instruction to the
loop
label is negative (because the label is before the branch, thus the address is lower). The distance is calculated in number of words (hence the 2 bits left shift). As all MIPS instructions are 4 bytes, this would be 6 instructions before, hence -6 is the value that should appear in the branch instruction offset (lower half-word). In binary: 1111 1111 1111 1010 (two's complement). In hexadecimal: FFFA.Checked with simulator and seems that my reasoning is correct since the instruction is coded as 0x1530FFFA.