473,508 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question regarding stack size allocation

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

Nov 14 '05 #1
2 1857
On 29 Dec 2004 21:07:37 -0800, "ronjon" <ro****@gmail.com> wrote:
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:
- news:comp.lang.asm.x86 (x86 assembly language programming)
- news:gnu.gcc (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.)

Nov 14 '05 #2
ronjon wrote:

[snip]

Try the gnu.gcc.help newsgroup.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
2671
by: Michael Sig Birkmose | last post by:
Hi everyone! Does anyone know, if it is possible to meassure the maximum stack usage of a C program throughout it's entire execution? -- Michael Birkmose
25
2525
by: Brian Lindahl | last post by:
I'm using a temporary buffer to transfer some data and would rather not allocate it on the heap. The problem is that the size of the buffer is only known upon entry into the function that utilizes...
20
1893
by: mohd hisham | last post by:
Write a C program to construct a queue of integers and to perform the following operations on it: a. insert b. delete c. display The program should print appropriate messages for stack overflow...
6
1538
by: Fernando Barsoba | last post by:
Hi all, I have a simple question regarding dynamic memory allocation.. is there a way to create a variable for which we don't know its size using the 'stack' instead of the 'heap'? For instance,...
20
1979
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
24
2838
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
1
1729
by: subramanian100in | last post by:
This is not a homework assignment question. Kindly excuse me for posting this question here. Suppose a BST contains 100,000 ndoes. Suppose I have to traverse the BST by Preorder, say, without...
14
3812
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
87
5464
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.