简体   繁体   中英

Do you need to clear a register before using la in MIPS?

We are taught to always clear a register before using it, or to treat it as if it may already contain data that could affect the operation we are trying to perform on that register. If I use the la instruction, does it overwrite whatever was in that register so it wouldn't matter if it already has data in it?

Would an or operation or and operation be able to do the same thing as la ? Is there a better way to load an address than la ? It is what I most often see for that type of operation but I feel like maybe once before I used or instead.

I know some instructions are pseudo-instructions and not as fast or desirable. What is the preferred method?

la is a pseudo-instruction, so it's up to each assembler if and how they want to implement it. But I would say that it's typically implemented as lui + ori , or lui + addiu . Both of those combinations will affect all bits of the destination register without any dependency on the old value. (They start with lui )

We are taught to always clear a register before using it

That sounds like strange advice. Clear a register if you have to, otherwise don't. MIPS doesn't have partial registers like the x86.

Setting a register to 0 right before setting some other value just clutters your source code and makes your program slower. Two write-only accesses in a row to a register are pointless.

Is there a better way to load an address than la?

No, not really. That's the most convenient way. And unless the assembler is doing something stupid it will almost always also be as efficient as any handcoded method. (Except for taking advantage of the displacement bits in an lw instruction, but MARS doesn't provide a way to do that other than hard-coding numeric addresses. See comments.)

Instead of using la , it's more common to use addi . You have always do one of those two because if you don´t do them, the variables will have trash inside them and not specific values. or or and should only be used in a condition statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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