memory allocation of local variables

S

sandy

Hi all,
I tried compiling the above two programs :
on x86, 32 bit machines.

And when I used objdump on that I saw the following code.
Can anyone help me know,
Why in the objdump of our first program the esp is decremented by 18H
bytes
and in the second program the esp is decremented by 28H bytes.

How actually is teh memory allocated by gcc for local variables.

Kindly help.

int main()
{
char x;
return 0;

}


[root@localhost ~]# gcc test.c
[root@localhost ~]# objdump -S a.out | less
08048348 <main>:
8048348: 55 push %ebp
8048349: 89 e5 mov %esp,%ebp
804834b: 83 ec 18 sub $0x18,%esp
804834e: 83 e4 f0 and $0xfffffff0,%esp
8048351: b8 00 00 00 00 mov $0x0,%eax
8048356: 83 c0 0f add $0xf,%eax
8048359: 83 c0 0f add $0xf,%eax
804835c: c1 e8 04 shr $0x4,%eax
804835f: c1 e0 04 shl $0x4,%eax
8048362: 29 c4 sub %eax,%esp
8048364: b8 00 00 00 00 mov $0x0,%eax
8048369: c9 leave
804836a: c3 ret
804836b: 90 nop

int main()
{
double x,y,z;
char p,q,r;
return 0;

}

08048348 <main>:
8048348: 55 push %ebp
8048349: 89 e5 mov %esp,%ebp
804834b: 83 ec 28 sub $0x28,%esp
804834e: 83 e4 f0 and $0xfffffff0,%esp
8048351: b8 00 00 00 00 mov $0x0,%eax
8048356: 83 c0 0f add $0xf,%eax
8048359: 83 c0 0f add $0xf,%eax
804835c: c1 e8 04 shr $0x4,%eax
804835f: c1 e0 04 shl $0x4,%eax
8048362: 29 c4 sub %eax,%esp
8048364: b8 00 00 00 00 mov $0x0,%eax
8048369: c9 leave
804836a: c3 ret
804836b: 90 nop
 
R

Richard Tobin

sandy said:
Why in the objdump of our first program the esp is decremented by 18H
bytes
and in the second program the esp is decremented by 28H bytes.

How actually is teh memory allocated by gcc for local variables.

For details of gcc, you should try a gcc-related group.

But you might find it enlightening to print out the addresses of the
variables (using the "%p" format in printf()). I didn't.

-- Richard
 
S

Stephen Sprunk

sandy said:
I tried compiling the above two programs :
on x86, 32 bit machines.

And when I used objdump on that I saw the following code.
Can anyone help me know,
Why in the objdump of our first program the esp is decremented by
18H bytes and in the second program the esp is decremented by
28H bytes.

How actually is teh memory allocated by gcc for local variables.

How any particular implementation accomplishes that is off-topic here;
all comp.lang.c cares about is that the Standard guarantees that it
happens somehow -- and that's all you need to know to write portable
code. If you start digging into what happens under the hood, you're
going to end up writing non-portable code.

<OT>In this particular case, GCC is setting up the stack frame to hold a
certain number of bytes of local variables, including padding. If you
don't know what a stack frame is, or don't know why modifying ESP
affects it, you're probably better off asking in comp.lang.asm.x86.</OT>

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top