So I have the following C code I wrote:
#include <stdio.h>
int main() {
int i = 1;
while(i) {
printf("in loop\n");
i++;
if(i == 10) {
break;
}
}
return 0;
}
Compiled with gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 it disassembles to this:
0x000000000040051c <+0>: push %rbp
0x000000000040051d <+1>: mov %rsp,%rbp
0x0000000000400520 <+4>: sub $0x10,%rsp
0x0000000000400524 <+8>: movl $0x1,-0x4(%rbp)
0x000000000040052b <+15>: jmp 0x400541 <main+37>
0x000000000040052d <+17>: mov $0x400604,%edi
0x0000000000400532 <+22>: callq 0x4003f0 <puts@plt>
0x0000000000400537 <+27>: addl $0x1,-0x4(%rbp)
0x000000000040053b <+31>: cmpl $0xa,-0x4(%rbp)
0x000000000040053f <+35>: je 0x400549 <main+45>
0x0000000000400541 <+37>: cmpl $0x0,-0x4(%rbp)
0x0000000000400545 <+41>: jne 0x40052d <main+17>
0x0000000000400547 <+43>: jmp 0x40054a <main+46>
0x0000000000400549 <+45>: nop
0x000000000040054a <+46>: mov $0x0,%eax
0x000000000040054f <+51>: leaveq
0x0000000000400550 <+52>: retq
Why is there a nop
on +45? And why does not je
on +35 just jump right to +46?
-falign-labels
either (there is anop
inserted as per description offalign-labels
but the adjusted address is not used). I rather think this is caused by the compiler reserving some bytes for longer representation of opcodes and not cleaned up. – Jongware Oct 13 '13 at 14:07-O3
or-Os
to the code generated by-O0
. – microtherion Oct 13 '13 at 16:44