assembler code generated by gcc

Stefan Müller mail@s-mueller.ch
Sun Dec 28 21:05:00 GMT 2003


I'm trying to understand how my computer works ;-) 
Now I have a lot of questions...

if I compile the following function (with -O2):

func(char *sm){
        char buffer[256];
        int i;
        for(i=0; i<=255; i++) buffer[i] = sm[i];
}

it gives me this assembler code:

0x08048358 <func+0>:    push   %ebp
0x08048359 <func+1>:    mov    %esp,%ebp
0x0804835b <func+3>:    sub    $0x108,%esp
0x08048361 <func+9>:    mov    0x8(%ebp),%ecx
0x08048364 <func+12>:   xor    %edx,%edx
0x08048366 <func+14>:   mov    %esi,%esi
0x08048368 <func+16>:   mov    (%edx,%ecx,1),%al
0x0804836b <func+19>:   mov    %al,0xfffffef8(%edx,%ebp,1)
0x08048372 <func+26>:   inc    %edx
0x08048373 <func+27>:   cmp    $0xff,%edx
0x08048379 <func+33>:   jle    0x8048368 <func+16>
0x0804837b <func+35>:   leave
0x0804837c <func+36>:   ret
0x0804837d <func+37>:   lea    0x0(%esi),%esi

gcc allocates 264 (sub $0x108,%esp) bytes on the stack. But only the lower 256 
are used.  Without the gcc parameter "-O2" even 280 Bytes on the stack are 
reserved.
For what are those unused bytes?

And what does "mov %esi,%esi" do? Nothing?

When will the "lea 0x0(%esi),%esi" instuction be executed?

There's an other small program:

long getesp() {
__asm__("movl %esp,%eax");
}

void main() {
        printf("%08X\n",getesp());
}

Everytime I execute it, it gives me a slightly different value. Shouldn't the 
esp register be the same value everytime?

I'm sorry to bore you with my stupid questions, I know, I should buy my self a 
good assembly book. But the bookstores have closed these days ;-) 
By the way, who knows a good book (x86, AT&T,linux/unix)? I don't even find an 
explanation of the assembly commands (what does "lea" do?) in the internet.

Sorry for my miserable english. I'm still learning...

stefan mueller




More information about the Gcc-help mailing list