noppseudo

nop

pseudo
RISC-V Assembly Reference

lyszt.net covers nop and other RISC-V instructions in more detail.

Description

nop does nothing. The name shortens "no operation." When the processor reaches a nop, no register changes, no memory is touched, and execution continues with the next instruction. It takes up one slot in the program and one tick of time passes.

nop is a pseudo-instruction for the line below.

addi zero, zero, 0

RISC-V includes a register called zero that always holds 0. A read returns 0, and a write is discarded. That addi adds 0 and 0 and discards the result, so the instruction completes and nothing changes. RISC-V needs no dedicated nop because the zero register supplies one.

Real processors use nops as padding, so that a later instruction begins at a memory boundary the hardware handles faster, and as placeholders that tools overwrite later with real instructions. Compilers insert them as a matter of course, which is why disassembled code contains them even though no programmer typed them.

The RISC-V manual states that nop changes no architecturally visible state, meaning nothing a program can observe, apart from advancing the pc and incrementing any performance counters. It gives two uses: aligning code segments to address boundaries that suit the hardware, and leaving room for later changes to code. The manual explains that the encoding uses addi because that instruction costs the fewest resources on most systems, since it reads only one register.

In this simulator nop serves as a spacer or a marker during experiments. Stepping over one moves pc forward by 4 and changes no register or memory location.

Expands to

addi zero, zero, 0

The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.

Example

li t0, 1
nop              # advances one instruction, changes nothing
addi t0, t0, 1   # t0 = 2