Skip to contentSkip to conversation

GCC Optimization?

DISCUSSION [email protected] 3 participants 3 messages Nov 23, 2004

lore
Share & tools
lore
#0 lore raw

Hello,

where is one question I didnt found a good answer. When I'm disassembling the main function with gdb or objdump I see always code like this:

0x08048354 <main+0>:    push   %ebp
0x08048355 <main+1>:    mov    %esp,%ebp
0x08048357 <main+3>:    sub    $0x8,%esp
0x0804835a <main+6>:    and    $0xfffffff0,%esp
0x0804835d <main+9>:    mov    $0x0,%eax
0x08048362 <main+14>:   sub    %eax,%esp

I understand what the first 3 lines are good for. Pushing the Basepointer, moving the Basepointer to the Stackpointer, reserving 8 Bytes on the stack for variables.

But why do they use and .... I think and does only even %esp so that the last 4 Bits are set to Zero.
But if %esp was 0xcccccccc before it would now be 0xccccccc0 what happens to the 4 Bits from 0xccccccc0 to 0xcccccccc? And what is this good for?

Second question:
What does mov $0x0,%eax? Why dont they use xor %eax and what is it good for?

Third question:
What is sub %eax,%esp good for? We subtract 0 from esp... mmmhh... for what?

Best regards

Signature · 2 lines
Geschenkt: 3 Monate GMX ProMail + 3 Top-Spielfilme auf DVD
++ Jetzt kostenlos testen http://www.gmx.net/de/go/mail ++
#1 lore raw
Quoted 5 lines

But why do they use and .... I think and does only even %esp so that the last 4 Bits are set to Zero.
But if %esp was 0xcccccccc before it would now be 0xccccccc0 what happens to the 4 Bits from 0xccccccc0 to 0xcccccccc? And what is this good for?

The x86 push and pop instructions are much, much less efficient if the stack pointer is not dword-aligned. So if one function (say) pushes a segment register onto the stack, it can affect the performance of every function that is called from there.

Quoted 3 lines

Second question:
What does mov $0x0,%eax? Why dont they use xor %eax and what is it good for?

It zeros the accumulator, of course. As for why they don't use xor, why should they? What would be the advantage?

Quoted 3 lines

Third question:
What is sub %eax,%esp good for? We subtract 0 from esp... mmmhh... for what?

Did you try building the same code with gcc optimizations turned on?

b

#2 lore raw
In reply to [email protected]
Quoted 3 lines

Second question:
What does mov $0x0,%eax? Why dont they use xor %eax and what is it good for?

It's because GCC was designed for more processors than only x86 based and in some cases it won't work. With optimization enabled you will find there xorl %eax, %eax instead of mov $0x0,%ea

Regards