简体   繁体   English

MIPS使用不同的寄存器类型但相同的解决方案

[英]MIPS using different register types but same solution wise

I am wondering is there any difference when you use different register such as the code below: 我想知道你使用不同的寄存器有什么区别,如下面的代码:

Here the correct answer uses register $v0 all the way: 这里正确的答案一直使用寄存器$v0

get_status:
        lui $t0,0xabab
        ori $t0, $t0, 0x0020

        lw $v0,0($t0)
        andi $v0,$v0,0x4
        srl $v0,$v0,2

        jr $ra

I used $t1 instead of $v0 above, notice I have a extra sw : 我使用$t1而不是$v0以上,请注意我有一个额外的sw

get_status:
        lui $t0,0xabab
        ori $t0, $t0, 0x0020

        lw $t1,0($t0)
        andi $t1,$t1,0x4
        srl $t1,$t1,2
        sw $t1, 0($t0)

        jr $ra

I think both code works the same, what you guys think? 我认为两个代码的工作原理相同,你们认为是什么?

$v0 and $t1 are both call-clobbered general-purpose integer registers. $v0$t1都是调用破坏的通用整数寄存器。 They're not "different types". 他们不是“不同类型”。

But in the standard MIPS calling convention, $v0 (and sometimes $v1 ) is where callers expect to find integer return values. 但是在标准的MIPS调用约定中, $v0 (有时是$v1 )是调用者期望找到整数返回值的地方。 That's why get_status calculates a result in $v0 . 这就是get_status$v0计算结果的原因。

I think both code works the same, what you guys think? 我认为两个代码的工作原理相同,你们认为是什么?

Your code modifies the static storage that it loaded from!!! 您的代码修改了它加载的静态存储! It's not a get_status anymore. 它不再是get_status了。

It's either void update_status(void) or int update_status(void) with a non-standard calling convention (returning in $t1 ). 它是void update_status(void)int update_status(void) ,具有非标准调用约定(在$t1返回)。


This is inefficient: 这效率很低:

lui ...
ori $t0, $t0, 0x0020
lw  $v0,0($t0)

Use the immediate offset space of lw to hold the low 16 bits of the static address. 使用lw的立即偏移空间来保存静态地址的低16位。 (It's sign extended, vs. ori zero-extending its immediate, but in this case you don't need to adjust the lui because the offset is signed positive.) (它是符号扩展,而ori零扩展它的立即,但在这种情况下,你不需要调整lui因为偏移是正面的。)

lui ...
lw  $v0, 0x0020($t0)

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

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