question regarding stack size allocation

R

ronjon

I am trying to corelate C code and its associated
assembly code produced by gcc. In particular,I am
trying to figure out how the stack pointer increments
during variable initialization inside a function call.

For this I have a very simple C program that only
initializes an array in a function call.

#include <stdio.h>

void func(int a , int b){

char arr[2];

}

int main(){

func(1,2);

}

The assembly code produced by gcc is the following:

..file "hello.c"
..text
..globl func
..type func,@function
func:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
leave
ret
..Lfe1:
..size func,.Lfe1-func
..globl main
..type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $8, %esp
pushl $2
pushl $1
call func
addl $16, %esp
leave
ret
..Lfe2:
..size main,.Lfe2-main
..ident "GCC: (GNU) 3.2.2 20030222 (Red Hat
Linux 3.2.2-5)"


For now I am only concerned about the line in the
function that allocates stack size:

subl $4, %esp

The displacement of the stack pointer changes
according to the size x of the char array, arr[x],
specified in the function.
The following are array sizes that I had specified and
the corresponding displacements of the stack sizes.

array size disp of %esp

2 4
3 24
4 4
5 24
6 24
8 8
9 24
16 24
17 40
32 40
33 56

I am unable to figure out the pattern in which the
stack size is incremented above.
I know this has something to do with word addressing
in intel processors but the values(of stack pointer
displacement) dont make sense to me.

Any gurus out there who can help me out? Thanks in
advance.

thanks

RS
 
R

Raymond Martineau

I am trying to corelate C code and its associated
assembly code produced by gcc. In particular,I am
trying to figure out how the stack pointer increments
during variable initialization inside a function call.

You are better off asking thefollowing newsgroups:
- (x86 assembly language programming)
- (The gcc free C compiler)
I am unable to figure out the pattern in which the
stack size is incremented above.
I know this has something to do with word addressing
in intel processors but the values(of stack pointer
displacement) dont make sense to me.

It certainly doesn't look like expected behaviour from the compiler.
However, something like this is more suitable on the newsgroups posted
above (ot if it happens to be a bug with the compiler, the maintainers of
gcc.)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top