为什么SPARC或MIPS的%r0始终为0? [英] Why the %r0 of SPARC or MIPS, is always 0?

查看:498
本文介绍了为什么SPARC或MIPS的%r0始终为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当您在SPARC CPU(和MIPS)中读取%r0时,总是返回0,但是我想知道为什么?

I know that when you read %r0 in SPARC CPU (and MIPS), always return 0, but I like to know Why ?

这背后是什么设计决定,为什么?

What design decision is behind this and why ?

推荐答案

这只是CPU的设计方式.一方面,确保r0始终设置为零是一种避免使用非常常见的值进行潜在的代价昂贵的内存访问的方法.

It's just the way the CPU was designed. Ensuring that r0 is always set to zero is, for one thing, a way to avoid potentially costly memory accesses for a very common value.

一方面(读取),预留一个寄存器来包含零值很方便,以便您可以使用它.否则,您将不得不自己将零加载到寄存器中.

On one hand (reading), it's handy to have a register set aside to contain the value of zero so that you can use it. Otherwise, you would have to load zero into a register yourself.

许多RISC处理器倾向于支持寄存器中的数据处理,仅访问存储器以进行加载和存储操作.这不是RISC的一成不变的规则,更多的是趋势.留出一个寄存器使您始终有零个可用空间可能会很有用-这是一个折衷,因为您少了一个寄存器即可用于通用值,但MIPS设计师显然认为这是值得的.

Many RISC processors tend to favour data manipulation in registers, accessing memory only for load and store operations. That's not a hard and fast rule of RISC, more of a tendency. Setting aside a register so that you always have zero available can be useful - it's a trade-off since you get one less register to use for general purpose values but the MIPS designers obviously thought it was worth it.

另一方面(写),由于r0绑定到值零,因此您可以在其中放置所需的内容,并且不会有任何区别-它将保持为零.这意味着如果您想放弃该值,可以将其用作目标.

On the other hand (writing), because r0 is tied to the value zero, you can put what you want in there and it will make no difference - it will remain at zero. That means you can use it as a target if you want to throw the value away.

同样,这与RISC背后的哲学有关.它倾向于支持极少数的指令格式,例如MIPS RIJ格式(寄存器,立即数和跳转).例如,您可以使用一组始终存储结果的方法,而不是根据是否要存储结果的情况而采用多种指令格式,如果不关心它,只需将其存储到r0中即可.

Again, this has to do with the philosophy behind RISC. It tends to favour a very small number of instruction formats such as the MIPS R, I and J formats (register, immediate and jump). As an example, rather than having multiple instruction formats depending on whether you want to store the result or not, you can have one set which stores the result always, then just store it into r0 if you don't care about it.

因此,如果要检查是否将两个寄存器相加是否会导致溢出,但又不想将结果存储在任何地方,则可以使用:

Hence, if you wanted to check if adding two registers would result in an overflow but didn't want to store the result anywhere, you could use:

add $0, $7, $8  ; r0 <- r7 + r8, but r0 remains at 0.

MIPS文档MIPS32 Architecture for Programmers Volume I: Introduction to the MIPS32 Architecture确认以上内容:

The MIPS documentation, MIPS32 Architecture for Programmers Volume I: Introduction to the MIPS32 Architecture, confirms the above:

R0的值硬连接为零,并且可以用作要丢弃其结果的任何指令的目标寄存器.当需要零值时,R0也可以用作来源.

R0 is hard-wired to a value of zero, and can be used as the target register for any instruction whose result is to be discarded. R0 can also be used as a source when a zero value is needed.

这篇关于为什么SPARC或MIPS的%r0始终为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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