LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-18-2007, 02:24 PM   #1
p4nk4j
LQ Newbie
 
Registered: May 2007
Distribution: Ubuntu 7.04 Feisty Fawn
Posts: 3

Rep: Reputation: 0
Question Beginner question for C/Assembly


I wrote a simple C program to test the memory allocation for local variables.

Code:
#include <stdio.h>

int main(int argc, char **argv) {
        int x=10;

        return 0;
}
But when i try to disassemble the program using gdb, it shows that 16 bytes are being reserved for local variables on the stack, although i declared a single integer variable which is supposed to take only 4 bytes.
Can anyone tell, why it is reserving 16 bytes on the stack for just a single variable ?

Code:
Breakpoint 1, main () at test7.c:4
4               int x=10;
(gdb) disassemble main
Dump of assembler code for function main:
0x08048344 <main+0>:    lea    0x4(%esp),%ecx
0x08048348 <main+4>:    and    $0xfffffff0,%esp
0x0804834b <main+7>:    pushl  0xfffffffc(%ecx)
0x0804834e <main+10>:   push   %ebp
0x0804834f <main+11>:   mov    %esp,%ebp
0x08048351 <main+13>:   push   %ecx
0x08048352 <main+14>:   sub    $0x10,%esp
0x08048355 <main+17>:   movl   $0xa,0xfffffff8(%ebp)
0x0804835c <main+24>:   mov    $0x0,%eax
0x08048361 <main+29>:   add    $0x10,%esp
0x08048364 <main+32>:   pop    %ecx
0x08048365 <main+33>:   pop    %ebp
0x08048366 <main+34>:   lea    0xfffffffc(%ecx),%esp
0x08048369 <main+37>:   ret    
End of assembler dump.
 
Old 05-18-2007, 03:35 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 33
Some points:

the compiler is free to do what it wants based on the best interests of the architecture, I woulfd GUESS the compiler is optimzing access to the int by aligning it on a 128 bit boundary in memory. 128 bits may also be best for a minimum size for a stack frame as well. I dunno.

if you want the size of a program stack you can call getrusage() to get stack size while it is running. the struct rusage member ru_isrss gives that information.

From the command line, size <compiled_file> will also give you information about your compiled image file.
 
Old 05-18-2007, 03:48 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,408

Rep: Reputation: 108Reputation: 108
It tries to optimize the number of stack operations (local variables plus registers that need to be put on stack when calling a function).
 
Old 05-18-2007, 04:03 PM   #4
alpha_gamma
LQ Newbie
 
Registered: Mar 2007
Posts: 11

Rep: Reputation: 0
What part of the disassembly indicates 16 bytes are on the stack?

Subroutine's memory are put on the stack and you have two primitive values that are arguments for the main function. These command line arguments will definitely need allocation. **argv, the argument vector variable is a double pointer as well.

Here is what I see that is going on in the disassembly: fff0, fffc, fff8, are address variables on the CPU. The CPU register, ECX is specifically used for counter variables, so it is related to argc, the argument count variable. The actions 'push %ecx' and 'pop %ecx' are pushing the variable onto the stack and popping it off of the stack. ESP is the stack pointer and points to the address of the stack at the point in the program. 0x08048344 <main+0>:, is an adress marker as well, through the disassembly you can see that the left hand address number in hexadecimal (base 16) and on the right it has the function's name as well as +0 (base 10). The left-hand number's two numbers at the right '44' will increment the same amount as the right hand number 'main +'. If you find a hex to decimal converter on the internet you can convert those hex numbers and see the increments are the same.

That is what I know about Assembly language, I hope it helps...
 
Old 05-18-2007, 07:33 PM   #5
p4nk4j
LQ Newbie
 
Registered: May 2007
Distribution: Ubuntu 7.04 Feisty Fawn
Posts: 3

Original Poster
Rep: Reputation: 0
Thanx for the replies.

Quote:
Originally Posted by alpha_gamma
What part of the disassembly indicates 16 bytes are on the stack?
Code:
0x08048352 <main+14>:   sub    $0x10,%esp
 
Old 05-18-2007, 08:43 PM   #6
p4nk4j
LQ Newbie
 
Registered: May 2007
Distribution: Ubuntu 7.04 Feisty Fawn
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jim mcnamara
the compiler is free to do what it wants based on the best interests of the architecture, I woulfd GUESS the compiler is optimzing access to the int by aligning it on a 128 bit boundary in memory. 128 bits may also be best for a minimum size for a stack frame as well.
Yeah, may be. When i tried declaring 4 variables, the same 16 bytes of memory was allocated, but when i tried declaring 5 variables, 32 bytes of memory was allocated on the stack.
But now, when i declare a 100 byte char array, 116 bytes of memory is being allocated on the stack. Any idea, why ?

Code:
(gdb) list
1       #include <stdio.h>
2
3       int main(int argc, char **argv) {
4               char buf[100];
5
6               return 0;
7       }
8
(gdb) disassemble main
Dump of assembler code for function main:
0x080483a4 <main+0>:    lea    0x4(%esp),%ecx
0x080483a8 <main+4>:    and    $0xfffffff0,%esp
0x080483ab <main+7>:    pushl  0xfffffffc(%ecx)
0x080483ae <main+10>:   push   %ebp
0x080483af <main+11>:   mov    %esp,%ebp
0x080483b1 <main+13>:   push   %ecx
0x080483b2 <main+14>:   sub    $0x74,%esp
0x080483b5 <main+17>:   mov    0x4(%ecx),%eax
0x080483b8 <main+20>:   mov    %eax,0xffffff88(%ebp)
0x080483bb <main+23>:   mov    %gs:0x14,%eax
0x080483c1 <main+29>:   mov    %eax,0xfffffff8(%ebp)
0x080483c4 <main+32>:   xor    %eax,%eax
0x080483c6 <main+34>:   mov    $0x0,%eax
0x080483cb <main+39>:   mov    0xfffffff8(%ebp),%edx
0x080483ce <main+42>:   xor    %gs:0x14,%edx
0x080483d5 <main+49>:   je     0x80483dc <main+56>
0x080483d7 <main+51>:   call   0x80482e4 <__stack_chk_fail@plt>
0x080483dc <main+56>:   add    $0x74,%esp
0x080483df <main+59>:   pop    %ecx
0x080483e0 <main+60>:   pop    %ebp
0x080483e1 <main+61>:   lea    0xfffffffc(%ecx),%esp
0x080483e4 <main+64>:   ret    
End of assembler dump.

Last edited by p4nk4j; 05-18-2007 at 08:47 PM.
 
  


Reply

Tags
assembly, gdb, stack


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembly Question aceman817 Programming 1 02-28-2006 01:01 AM
MIPS assembly question Gnute Programming 1 08-24-2004 05:33 PM
Inline Assembly Question tjt Programming 3 08-08-2004 04:38 AM
C & Assembly question eantoranz Programming 3 04-23-2004 01:18 PM
Assembly Question! wwnn1 Programming 4 06-16-2002 01:18 AM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration