mv
pseudolyszt.net covers mv and other RISC-V instructions in more detail.
Description
mv copies the value of one register into another. The name is short for move. After the line below, both registers hold the same value and the source, t0, is unchanged.
mv a0, t0
mv is a pseudo-instruction. The assembler rewrites this readable name into a real instruction, shown below.
addi rd, rs, 0
Adding zero to a value and storing it elsewhere makes a copy. RISC-V has no dedicated move instruction and reuses addition instead.
Different registers have different jobs, which is the usual reason to use mv. By convention, calculations happen in the temporary registers (t0, t1 and so on). When a function is called, its arguments go in specific registers (a0, a1 and so on), and results come back in a0. A program therefore places a mv before a call, which puts a computed value in the register the called code reads, and a mv after a call, which saves a returned result in a safe place.
mv a0, t0 call square mv s0, a0
Placing values in the right registers at the right moment is a large part of assembly, and mv does it.
The RISC-V manual states that addi with the constant 0 implements the mv assembler pseudoinstruction.
Because mv copies the value, changing the destination later leaves the source unchanged. This makes mv useful for keeping a copy of a value before a loop changes the original. The simulator's trace shows the addi that runs in place of mv.
Expands to
addi rd, rs, 0
The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.
Example
li t0, 42 mv t1, t0 # t1 = 42
