473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

some doubts in a .s file of a c program

hi
i was trying to see how the compiler hides the static golbals from the
linker and allows golbal varibale to be visable to the linker.i managed
to figure out how it did that ( the .lcomm and .comm sections) but the
assembly code for the c program raised a few more doubts . i am
enclosing the .c and the .s files .. if someonce could expain what the
starred statements in the .s file mean.. i would be greatful
the .c file is as follows
#include<stdio.h>
int i ;
static int j ;
int main()
{
printf ("\t%d\t%d\n" ,i , j );
}

the .s file it generated on a P4 RHEL machine using the cc command is
as follows

..file "some.c"
.section .rodata
..LC0:
.string "\t%d\t%d\n"
.text
..globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp * why more space in stack when no local vars
are used
andl $-16, %esp * this has something to do with making the
stack aligned to a 16 bit filed.. can u expalin it in
detail
movl $0, %eax * the next six statements make no sense to me
at all... why are they here
addl $15, %eax *
addl $15, %eax *
shrl $4, %eax *
sall $4, %eax *
subl %eax, %esp *
subl $4, %esp *
pushl j
pushl i
pushl $.LC0
call printf
addl $16, %esp
leave
ret
.size main, .-main
.comm i,4,4
.local j
.comm j,4,4
..section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)"


any help will be appriciated
kind regards
rahul

Oct 3 '06 #1
7 1966

ra*******************@gmail.com wrote:
hi
i was trying to see how the compiler hides the static golbals from the
linker and allows golbal varibale to be visable to the linker.i managed
to figure out how it did that ( the .lcomm and .comm sections) but the
assembly code for the c program raised a few more doubts . i am
enclosing the .c and the .s files .. if someonce could expain what the
starred statements in the .s file mean.. i would be greatful
the .c file is as follows
#include<stdio.h>
int i ;
static int j ;
int main()
{
printf ("\t%d\t%d\n" ,i , j );
}
****Well, this is off topic here. Posting it to gcc.* groups is
better.****
>

the .s file it generated on a P4 RHEL machine using the cc command is
as follows

.file "some.c"
.section .rodata
.LC0:
.string "\t%d\t%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp * why more space in stack when no local vars
are used
andl $-16, %esp * this has something to do with making the
stack aligned to a 16 bit filed.. can u expalin it in
detail
movl $0, %eax * the next six statements make no sense to me
at all... why are they here
addl $15, %eax *
addl $15, %eax *
shrl $4, %eax *
sall $4, %eax *
subl %eax, %esp *
subl $4, %esp *
Maybe gcc is clever than us all. If you really want to know how gcc
works, why don't you ask a gcc hacker?
pushl j
pushl i
pushl $.LC0
call printf
addl $16, %esp
leave
ret
.size main, .-main
.comm i,4,4
.local j
.comm j,4,4
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)"


any help will be appriciated
kind regards
rahul
Oct 3 '06 #2
The function call to printf has 3 arguments.
Gcc attempts to maintain the stack aligned by rounding
up the stack value before pusshing the arguments.

This feature of gcc is an incredible costly feature. As you
can see a great percentage of gcc's emitetd code is just
stack manipulation stuff.
Oct 3 '06 #3

<ra*******************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
hi
i was trying to see how the compiler hides the static golbals from the
linker and allows golbal varibale to be visable to the linker.i managed
to figure out how it did that ( the .lcomm and .comm sections) but the
assembly code for the c program raised a few more doubts . i am
enclosing the .c and the .s files .. if someonce could expain what the
starred statements in the .s file mean.. i would be greatful

the .c file is as follows

#include<stdio.h>
int i ;
static int j ;
int main()
{
printf ("\t%d\t%d\n" ,i , j );
}

the .s file it generated on a P4 RHEL machine using the cc command is
as follows
Unfortunately, most here either don't know any assembly or feign ignorance
to keep the topics to C only.

First, the assembly is using some GAS directives I'm not familiar with which
I suspect are unique to Linux.
.file "some.c"
.section .rodata
.LC0:
.string "\t%d\t%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
These two instructions save and replace the current stack pointer. They are
equivalent to the 'enter' instruction. These two when combined with the
'subl $8, %esp' are the C function's prolog. The prolog and epilog (below)
create and destroy the stackframe, respectively.
subl $8, %esp * why more space in stack when no local vars
are used
This is used to allocate stack space for variables, but it doesn't appear
that anything accesses the allocated space... (It may be in the C startup,
or printf() code, or main's return value, etc...) main() does return an int
(probably 4 bytes and not 8) but it isn't set or cleared in the posted code
(it should be done in the C startup). This may go away with optimization.
It seems the extra space is removed further below.
andl $-16, %esp * this has something to do with making the
stack aligned to a 16 bit filed.. can u expalin it in
detail
$-16 is the same as 0xfffffff0. An 'and' the stack pointer (esp) clears the
lower eight bits, thereby "aligning" it to a multiple of 16.
movl $0, %eax * the next six statements make no sense to me
addl $15, %eax *
addl $15, %eax *
shrl $4, %eax *
sall $4, %eax *
subl %eax, %esp *
subl $4, %esp *
In C syntax:

eax=0;
eax+=15;
eax+=15;
eax>>4;
eax<<4; /* eax is 16 */
esp-=eax; /* esp=esp-16 */
esp-=4; /* esp=esp-20 */

The end result is some more (20 bytes) of stack allocation. The first six
_seem_ to be an aligned stack allocation (of two items) and the last line
appears to be unaligned allocation (although it's value is 4) of one item.
Since there isn't a nice correspondence with the C code and it appears that
variables i and j have space allocated at the bottom, I'm not sure what
specifically causes the instructions to be generated. But, I've seen it
before and it usually goes away or is reduced with optimization.
pushl j
pushl i
pushl $.LC0
call printf
Although this GAS syntax is slightly different from the way I've seen it,
this pushes the arguments to printf() and calls it.
addl $16, %esp
leave
ret
'addl $16,%esp' removes 16 bytes from the stack,i.e., stack cleanup prior to
the function exiting. 'leave' restores the saved stack pointer. It is
equivalent to 'movl %ebp, %esp; popl %ebp'. This is also the C function's
epilog. As I stated earlier, it seems that there is extra space allocated
and destroyed for some reason, probably lack of optimization.
.size main, .-main
.comm i,4,4
.local j
.comm j,4,4
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)"
About a month ago, there was a similar thread on comp.lang.c++ and
comp.lang.asm.x86:

original
http://groups.google.com/group/comp....bb9616fd?hl=en
explanation
http://groups.google.com/group/comp....8b61eeac?hl=en
explanation
http://groups.google.com/group/comp....930da546?hl=en

Jacob Navia also listed this link:
http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax
Someone around here claimed to have in depth knowledge of GCC "about a
decade ago." They might be able to help further. (Was that Chris Torek?)
Rod Pemberton
Oct 3 '06 #4

jacob navia wrote:

This feature of gcc is an incredible costly feature.
Note this is in function main(), which is usually called just once.

Aligning the stack pointer can be a BIG win on some architectures, like
2x faster access to parameters and local variables.

But of course anything having to do with the real world is somewhat
off-topic here :)

Oct 3 '06 #5
hi all
many thanks to rod for giving such a detailed explanation... i
appriciate his help....
just contuing further though.. any idea where i can find some
literature on this stack aligning business... some doc where it has
been discussed in detail...
kind regards
rahul

Oct 3 '06 #6

<ra*******************@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
hi all
many thanks to rod for giving such a detailed explanation... i
appriciate his help....
just contuing further though.. any idea where i can find some
literature on this stack aligning business... some doc where it has
been discussed in detail...
kind regards
rahul
I would start with the Intel microprocessor manuals. For example, the
Volume 1 of the Pentium4 manuals has a section (6.2.2) on stack alignment:

http://www.intel.com/design/pentium4/manuals/253665.htm

Then, I would check the gcc documentation,
specifically -mpreferred-stack-boundary.

http://gcc.gnu.org/onlinedocs/gcc-4....002d64-Options

Then, I would use Yahoo (search for "stack alignment" "how to") to find
stuff like this, note the 'sub esp,xx' comment:

"With newer versions of GCC, programs whose inner loops include many
function calls, or which are deeply recursive, could benefit from using
the -mpreferred-stack-boundary=2 compiler option. This causes the compiler
to relax its stack-alignment requirements that need a lot of sub esp,xx
instructions. The default stack alignment is 16 bytes, unless overridden
by -mpreferred-stack-boundary. The argument to this option is the power of 2
used for alignment, so 2 means 4-byte alignment; if your code uses double
and long double variables, an argument of 3 might be a better choice. "

http://www.delorie.com/djgpp/v2faq/faq14_2.html

Then, I would use Google's Groups advanced search to search for say: gcc
"stack alignment." This will pull up hundreds of posts similar to the
detailed response I provided, especially some much older ones on
comp.lang.asm.x86.

http://groups.google.com/advanced_search?hl=en
Rod Pemberton
Oct 3 '06 #7
<ra*******************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
the .c file is as follows

#include<stdio.h>
int i ;
static int j ;
int main()
{
printf ("\t%d\t%d\n" ,i , j );
}

the .s file it generated on a P4 RHEL machine using the cc command is
as follows

.file "some.c"
.section .rodata
.LC0:
.string "\t%d\t%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp * why more space in stack when no local vars
are used
andl $-16, %esp * this has something to do with making the
stack aligned to a 16 bit filed.. can u expalin it in
detail
movl $0, %eax * the next six statements make no sense to me
at all... why are they here
addl $15, %eax *
addl $15, %eax *
shrl $4, %eax *
sall $4, %eax *
subl %eax, %esp *
subl $4, %esp *
None of this stuff makes much sense; my guess is that you compiled
without optimization. GCC is infamous for putting out incredibly stupid
code when you do that. OTOH, it's very difficult to match GCC's
optimized code up to the C source to figure out why it does what it
does, so there's a purpose in leaving that mode in.

Still, here's what I get with my GCC on Linux without optimization:

main:
pushl %ebp
movl %esp,%ebp
movl j,%eax
pushl %eax
movl i,%eax
pushl %eax
pushl $.LC0
call printf
addl $12,%esp
..L1:
leave
ret

and with -O3:

main:
pushl %ebp
movl %esp,%ebp
pushl j
pushl i
pushl $.LC0
call printf
leave
ret

The unoptimized version isn't really that much worse, certainly not as
bad as the version you posted. Turn on optimizations and recompile; if
the odd stack stuff is still there, go report it to the GCC folks as a
bug.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Oct 3 '06 #8

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

Similar topics

1
by: Piotre Ugrumov | last post by:
I have some problems and some doubts. I have implemented a class hierachy. The base class Velivolo, from Velivolo derive Militare and Civile, from militare derive Aereo and Elicottero, from Civile...
17
by: ranjeet.gupta | last post by:
Dear All Below are the few doubts which I got while studying about C 1. Is there any method in C by which we can process the entire string in one unit, 2. Does there exist any way to...
6
by: Chua Wen Ching | last post by:
Hi there, I have some questions to ask... just say i have this xml file: Scenario :- Script.xml ======== <software> <settings>
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
17
by: fctk | last post by:
some other doubts: 1) K&R, p50, 2.10: "if expr1 and expr2 are expressions, then expr1 op= expr2 is equivalent to expr1 = (expr1) op (expr2) except that expr1 is computed only once."
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
0
by: Gotch | last post by:
Hi, I'm writing a C++ application that does XML parsing (throug expat). Since I want it to have from start Unicode support (so that I won't have to think about it later), I'm facing various doubts...
28
by: sophia.agnes | last post by:
1)can any one give an example for a C program that can be written using goto only ? i.e a program in which we cannot avoid the keyword goto 2)what exactly is the purpose of tmpfile() ? will it...
1
by: ramasubramanian.rahul | last post by:
hi people.. dont know if this the right forum for this doubt ... so sorry if i am mis-posting... i was looking at the way glib 2.10.3 does export optimization using a list of "to be exposed"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.