MIPS - and instruction of zero - Register

160 Views Asked by At

I've got the instruction to understand a MIPS - code. One line of it is the following one:

and $t6, $zero, $zero

Which purpose does it have ? There is the and - instruction of two zero - constants. So the result should also be zero ? But why is the and - command in usuage here ?

Thanks a lot !


I've tried the MARS - Simulator for MIPS - Processors and watched the registers. But the register never reaches a different value than zero after the and - command.

2

There are 2 best solutions below

6
gusbro On

The and instruction (and rd, rs, rt) performs a bitwise logical AND between rs and rt and stores the result in rd.

So in your example, and $t6, $zero, $zero would perform a logical and between 0 and 0 yielding also 0 and that value is stored in $t6 (overwriting its previous value).

Example:

 li $t6, 0x1234 # store 0x1234 in $t6
 # ... maybe some other unrelated code
 and $t6, $zero, $zero  # stores 0 in $t6
0
puppydrum64 On

The real reason is because move and li don't actually exist. These are known as pseudo-instructions (essentially standardized macros.)

So when you write move $t0, $zero the CPU is actually running and $t0,$0,$0.

Similarly, li $t0, 0x20 is actually ori $t0, $zero, 0x20 or sometimes addi $t0, $zero, 0x20 depending on the implementation.

Now you may be wondering, why does MIPS work like this? MIPS is known as a Reduced Instruction Set Computer, which means it has fewer possible instructions than a computer like x86. In addition, unlike x86, every instruction takes up 4 bytes in memory, regardless of what the instruction is doing. This makes the CPU's job much easier. Unlike x86, MIPS doesn't need to calculate how far to advance $pc after each instruction since the answer is always 4. As a result, MIPS is more efficient at executing the small list of instructions it does have, despite sometimes needing more steps to achieve what other computers can do in one line of code.

Here's an example:

main:
j foo      ;0x00000000
nop        ;0x00000004
nop        ;0x00000008
nop        ;0x0000000C

foo:
nop        ;0x00000010

If you think about it, what is j foo really? Couldn't you also represent that as addiu $pc, $pc, 0x10? After all, that's essentially what's happening.