while(1), for(;;) and twiddling of thumbs
Posted: Mon Aug 12, 2013 11:09 pm
I've always used while(1) in C for an infinite loop, and overlooked for(;;) because it makes less sense and degrades readability somewhat. I decided to investigate whether one has any advantage over the other once the code is compiled:
for(;;){}
while(1){}
So it's all fine and dandy - they both result in an empty, infinite loop. Cool.
What I was intrigued about was the useless xchg instructions, and the nop. Why would the compiler put these in here and leave the computer twiddling its thumbs? The only things I can think of is to pad the section for some reason... I can see the pattern of 906690669066 in there.. is that something significant? I'd better pull the ASCII chart out.
Any thoughts?
for(;;){}
Code: Select all
080483d0 <main>:
80483d0: 55 push %ebp
80483d1: 89 e5 mov %esp,%ebp
80483d3: eb fe jmp 80483d3 <main+0x3>
80483d5: 66 90 xchg %ax,%ax
80483d7: 66 90 xchg %ax,%ax
80483d9: 66 90 xchg %ax,%ax
80483db: 66 90 xchg %ax,%ax
80483dd: 66 90 xchg %ax,%ax
80483df: 90 nop
while(1){}
Code: Select all
080483d0 <main>:
80483d0: 55 push %ebp
80483d1: 89 e5 mov %esp,%ebp
80483d3: eb fe jmp 80483d3 <main+0x3>
80483d5: 66 90 xchg %ax,%ax
80483d7: 66 90 xchg %ax,%ax
80483d9: 66 90 xchg %ax,%ax
80483db: 66 90 xchg %ax,%ax
80483dd: 66 90 xchg %ax,%ax
80483df: 90 nop
What I was intrigued about was the useless xchg instructions, and the nop. Why would the compiler put these in here and leave the computer twiddling its thumbs? The only things I can think of is to pad the section for some reason... I can see the pattern of 906690669066 in there.. is that something significant? I'd better pull the ASCII chart out.
Any thoughts?