Skip to content
systemcoding.dev
Assembly

Copy a register with mv, which is addi rd, rs, 0

mv a0, a1 is a pseudo-instruction for addi a0, a1, 0 on RISC-V, so there is no separate move opcode in the base ISA.

1 min read
Summarize in AI

RISC-V has no move instruction. The base integer ISA spends its encoding space on operations that compute something, and a copy is an addition of zero. mv rd, rs is a pseudo-instruction the assembler rewrites to addi rd, rs, 0.

asm
    li    a1, 42
    mv    a0, a1          # assembler emits: addi a0, a1, 0
Text
a0 = 42
a1 = 42

You will see the expanded form in a disassembly, so recognise it. With the C extension, mv can also be a 2-byte c.mv, and the assembler picks it when -march includes c, as rv64gc does. The same idea gives the other register-to-register idioms: nop is addi x0, x0, 0, neg is sub rd, x0, rs, and not is xori rd, rs, -1. The full list of pseudo-instructions and their expansions is in the RISC-V assembly programmer's manual, and the encoding of addi is in the RISC-V unprivileged ISA specification.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.