MIPS在过程中使用$ s0 ...等reginsters并最终将其还原是否优雅 [英] MIPS Is it elegant to use $s0... etc reginsters inside the procedure and restore it on end

查看:266
本文介绍了MIPS在过程中使用$ s0 ...等reginsters并最终将其还原是否优雅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MIPS教程中读到,在整个过程调用中只保留reginster $ s0- $ s7. 但是我认为(夫人,我错了吗?)创建具有副作用的过程并不好-我认为过程仅应更改$ v0,$ v1寄存器和堆栈(如果需要的话)(对吗?). 因此,在我的程序内部,我只能使用$ t0- $ t9的reginster. 但是,当我在程序中调用某些程序时,可以更改$ t0- $ t9的reginster.因此,我必须存储临时reginster并在过程中调用过程后将其还原.但是当我的程序如下所示时:

I read in MIPS tutorial, that only reginsters $s0-$s7 are preserved across procedure calls. But I think (maby I am wrong?) that it is not elegant to create procedures that have side effects - I think procedures should change only $v0, $v1 reginsters and stack if it is needed (am I right?). So in my opinion inside my procedure I can use only $t0-$t9 reginsters. But when I call some prodedures in my procedure it can change $t0-$t9 reginsters. So I have to store temporary reginsters and restore it after procedure call in my procedure. But when my procedure looks like following:

call procedure1
compute something on temporary reginsters

call procedure2
compute something on temporary reginsters

call procedure3
compute something on temporary reginsters

...

我的过程需要大量的内存访问. 所以我的想法是使用:

my procedure needs a lot of memory accesses. So my idea is to use:

Store at the begining of my procedure reginsters $s0-$s7.
Use reginsters $s0-$s7 in my procedure.
Restore old values of reginsters $s0-$s7.

它优雅吗,有什么不好的后果吗?

Is it elegant and has it any bad consequences?

推荐答案

我在MIPS教程中读到,在整个过程调用中只保留$ gin- $ s0- $ s7.

I read in MIPS tutorial, that only reginsters $s0-$s7 are preserved across procedure calls.

$s0-$s7在调用过程中没有任何神奇的事情发生(例如,jal some_routine只是将返回地址粘贴在$ra中,并跳转到some_routine;什么也没有发生).

Nothing magical happens to $s0-$s7 across a call (e.g. jal some_routine just sticks the return address in $ra and jumps to some_routine; nothing else happens).

在调用过程中保留这些寄存器只是一个约定:它是标准应用程序二进制接口"(ABI)的一部分,它是一组约定,涉及寄存器使用,堆栈使用,数据格式等.不同符合同一ABI的部分代码(应用程序,库等)将彼此互操作.

The preservation of these registers across a call is just a convention: it's part of the standard "Application Binary Interface" (ABI), which is a set of conventions covering register usage, stack usage, data formats etc. -- different bits of code (applications, libraries, etc.) which conform to the same ABI will interoperate with each other.

如果您希望代码可从其他地方调用,则您的代码必须符合调用者期望的ABI.例如如果要编写一些需要从C代码中调用的汇编例程,则需要符合C编译器生成的代码所使用的ABI.

If you want your code to be callable from elsewhere, your code needs to conform to the ABI expected by the caller. e.g. if you're writing some assembly routine that needs to be called from C code, you'll need to conform to the ABI used by the code that the C compiler generates.

对于通常的MIPS ABI,这意味着调用您的例程的代码将假定在调用返回之前,它在$s0-$s7中输入的任何值仍将存在,但在$s0-$s7 em> not 假定$t0-$t9的内容仍然相同. (类似地,例如,如果您的代码调出库例程,则可以做出相同的假设:在调用返回后,$s0-$s7中的所有内容均被保留,但$t0-可能包含任何内容.)

For the usual MIPS ABI, this means that the code which calls your routine will assume that whatever values it has put in $s0 - $s7 before the call will still be there after the call returns, but it will not assume that the contents of $t0 - $t9 are still the same. (Similarly, if your code calls out to library routines (for example), it can make the same assumptions: after the call returns, anything that was in $s0 - $s7 has been preserved, but $t0 - $t9 may contain anything.)

这意味着您的代码必须先保存$s0-$s7中的任何一个,然后再进行更改,并在返回之前将其还原.保存和还原它们之间的作用无关紧要-重要的是调用者看不到任何更改.

That means your code must save any of $s0 - $s7 before changing them, and restore them before returning. What it does between saving and restoring them doesn't matter -- all that matters is that the caller does not see any change.

所以,是的,您的想法是正确的(在这里确实是明智的选择.)

So, yes, your idea is correct (and indeed the sensible thing to do here).

(请注意,您的子过程(procedure1prodcedure2等)不一定必须符合此标准ABI,如果它们只是可以从您的主过程中调用,而不必调用外部例程,因为在这种情况下,它们只需要与您的主过程进行互操作,而无需与任何其他代码进行互操作,但是遵循ABI是一个好主意无论如何,除非有很好的理由不这样做;它使读取代码更容易,以后在需要时更易于使内部过程之一更公开,更容易向其他代码添加调用,等等.)

(Note that your sub-procedures (procedure1, prodcedure2 etc.) do not necessarily have to conform to this standard ABI, if they are only ever called from your main procedure, and don't call out to external routines -- because, in that case, they only need to interoperate with your main procedure, not with any other code. But it is a good idea to follow the ABI anyway, unless there is a very good reason not to; it makes it easier to read the code, easier to make one of the internal procedures more public later on if necessary, easier to add calls to other code, etc.)

这篇关于MIPS在过程中使用$ s0 ...等reginsters并最终将其还原是否优雅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆