Skip to content

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.

If we want to copy the value in register a0 to register a1, we would write:

mv a1, a0

For example, our full code may look something like:

.text
main:
li a0, 20
mv a1, a0
stop:
li a7, 10
ecall

Run on creatorsim

The result in the registers will be:

a0 = 20
a1 = 20

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, a0

is translated to:

addi a1, a0, 0

In other words, add 0 to the value in a0, and store the result in a1.

This effectively copies the value from a0 to a1.