Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gcc output machine code have nop instructions

Tags:

c

objdump

nop

Everytime I do an objdump -d I always see the asm code with batches of nop instructions (instructions that do nothing)

For example take this same program:

#include <stdio.h>
#include <math.h>

int main()
{
    printf("Hello World!\n");
    printf("cos:  %f\n", cos(1));
    return 1;
}

The objdump for exampe has 2 nops at the end of the entry point

0000000000400450 <_start>:
400450: 31 ed                   xor    %ebp,%ebp
400452: 49 89 d1                mov    %rdx,%r9
400455: 5e                      pop    %rsi
400456: 48 89 e2                mov    %rsp,%rdx
400459: 48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
40045d: 50                      push   %rax
40045e: 54                      push   %rsp
40045f: 49 c7 c0 00 06 40 00    mov    $0x400600,%r8
400466: 48 c7 c1 70 05 40 00    mov    $0x400570,%rcx
40046d: 48 c7 c7 34 05 40 00    mov    $0x400534,%rdi
400474: e8 bf ff ff ff          callq  400438 <__libc_start_main@plt>
400479: f4                      hlt    
40047a: 90                      nop
40047b: 90                      nop 

And that is just one of many examples but you get the idea. Why is the C code compiled this way? Thanks in Advance.

like image 440
PuercoPop Avatar asked May 05 '11 17:05

PuercoPop


People also ask

Why does compiler add NOP?

Usually nop s inside functions are to align branch targets, including function entry points like in the question Brian linked. (Also see -falign-loops in the gcc docs, which is on by default at optimization levels other than -Os ).

Why does the NOP instruction exist?

A NOP is most commonly used for timing purposes, to force memory alignment, to prevent hazards, to occupy a branch delay slot, to render void an existing instruction such as a jump, as a target of an execute instruction, or as a place-holder to be replaced by active instructions later on in program development (or to ...

What does NOP mean in assembly?

What Does No Operation (NOP) Mean? A no operation or “no-op” instruction in an assembly language is an instruction that does not implement any operation. IT pros or others might refer to this as a blank instruction or placeholder.


1 Answers

The nops are added to force the next function align to the 4-byte boundary. (notice that the address following the last nop will be 40047c which is divisible by 4)

like image 55
kennytm Avatar answered Oct 12 '22 00:10

kennytm