为什么GCC输出机code有NOP指令 [英] Why does gcc output machine code have nop instructions
问题描述
每次我做一个objdump的我-d总是看到ASM code。与NOP指令(什么也不做说明)批次
Everytime I do an objdump -d I always see the asm code with batches of nop instructions (instructions that do nothing)
例如借此同一个程序:
#include <stdio.h>
#include <math.h>
int main()
{
printf("Hello World!\n");
printf("cos: %f\n", cos(1));
return 1;
}
有关exampe的objdump的有2个空指令在入口点的结束
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
而这只是众多例子中的一个,但你的想法。为什么在C code编译这种方式?先谢谢了。
And that is just one of many examples but you get the idea. Why is the C code compiled this way? Thanks in Advance.
推荐答案
很多时候,那些只是用来做填充,这样后续的东西开始就一个字或边界再次,因为获得任意code未对齐字边界是CPU昂贵得多。
Very often those are just used to do padding so that subsequent stuff starts on a word or boundary again, as access to arbitrary code that is not aligned on word boundaries is much more expensive for the cpu.
这篇关于为什么GCC输出机code有NOP指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!