What is the purpose of the nop in this code?
Posted: Thu Nov 13, 2025 3:12 am
Hi everyone, firs post here.
I am exploring the RISC-V ISA and in particular assembly while also catching up on C. I have a setup where I write simple C programs and compile them to assembly.
I compiled the following program:
and got the following assembly code (snippet):
What is the purpose of this "nop" here? I understand that it is supposed to do nothing and consume 1 cycle, if I am not mistaken. What I am trying to learn more about it why GCC decided to put it there and what purpose it servers.
I compiled it via:
What do I need to read more about to understand this?
I am exploring the RISC-V ISA and in particular assembly while also catching up on C. I have a setup where I write simple C programs and compile them to assembly.
I compiled the following program:
Code: Select all
void bob() {
static int z = 0;
z += 1;
}
int valio() {
auto int a = 6;
bob();
bob();
return a;
}
Code: Select all
...
bob:
addi sp,sp,-16
sw ra,12(sp)
sw s0,8(sp)
addi s0,sp,16
lui a5,%hi(z.0)
lw a5,%lo(z.0)(a5)
addi a4,a5,1
lui a5,%hi(z.0)
sw a4,%lo(z.0)(a5)
nop
lw ra,12(sp)
lw s0,8(sp)
addi sp,sp,16
jr ra
...
I compiled it via:
Code: Select all
riscv64-unknown-elf-gcc -O0 -nostdlib -march=rv32i -mabi=ilp32 -Wl,-Tm.ld c-asm.c -S