为什么gcc输出机器代码有nop指令
每次我做一个objdump -d我总是看到asm代码与批量的nop指令(指令什么都不做)
例如,采取相同的程序:
#include #include int main() { printf("Hello World!\n"); printf("cos: %f\n", cos(1)); return 1; }
示例的objdump在入口点的末尾有2个nops
0000000000400450 : 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 400479: f4 hlt 40047a: 90 nop 40047b: 90 nop
这只是众多例子中的一个,但你明白了。 为什么C代码以这种方式编译? 提前致谢。
通常这些只是用来做填充,以便后续的东西再次在一个字或边界上开始,因为访问未在字边界上对齐的任意代码对于cpu而言要昂贵得多。
添加nop
以强制下一个函数与4字节边界对齐。 (注意最后一个nop
后面的地址是40047c,可被4整除)