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
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