Copying registers
We already know that we can set the value of a register to a constant, using
the li pseudo-instruction.
WHat if we have a value in one register, and we want to copy it to another register?
For that, we can use the mv pseudo-instruction.
Syntax
Section titled “Syntax”If we want to copy the value in register a0 to register a1, we would write:
mv a1, a0For example, our full code may look something like:
.textmain: li a0, 20 mv a1, a0
stop: li a7, 10 ecallThe result in the registers will be:
a0 = 20a1 = 20Implementation
Section titled “Implementation”The mv pseudo-instruction is actually implemented using the addi
instruction, which adds an immediate value to a register.
It’s essentially the reverse of the li pseudo-instruction we saw earlier.
For example, the instruction:
mv a1, a0is translated to:
addi a1, a0, 0In other words, add 0 to the value in a0, and store the result in a1.
This effectively copies the value from a0 to a1.