简体   繁体   中英

RISC-V NOP instruction

I am working on RISC-V 32I instructions recently. I got a question about NOP instruction, which the specification says it is equal to ADDI x0, x0, 0 .

However, x0 is not a general register which can be modified by the programmer. Thus, why x0 serves as a destination register here for the NOP instruction?

Can anyone please shed some lights on this point?

NOP is a pseudoinstruction that expands to ADDI x0, x0, 0 . The x0 (or zero ) is a read-only register dedicated to the value zero, ie, it is hardwired to zero for every single bit. Whatever is written to this register is simply discarded since its value can't be modified.

From The RISC-V Instruction Set Manual Volume I: Unprivileged ISA :

The NOP instruction does not change any architecturally visible state, except for advancing the pc and incrementing any applicable performance counters. NOP is encoded as ADDI x0, x0, 0 .

Keeping in mind that RISC-V has no arithmetic flags (ie, carry, overflow, zero, sign flags), any arithmetic operation whose destination register is x0 will do as a no operation instruction regardless of the source registers since the net result will consist of advancing the program counter to the next instruction without changing any other relevant processor's state.

oystercatcher quotes a correct statement, x0 holds the constant 0 and cannot be written to another value.

The instruction ADDI x0, x0, 0 performs x0 <- x0 + 0 , where 0 is an immediate encoded in the instruction. Such instruction has no effect on the state of RISCV and therefore is a NOP.

Other alternative for a NOP on RISCV : ADDI x0, x1, 0 . Even ADD x0, x1, x2 will have no effect on RISCV (there is no Carry flag on RISCV) and therefore behaves like a NOP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM