473,387 Members | 1,512 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,387 software developers and data experts.

Any explanations

I would like to understand the following C++/C program in assembly

int main(void){
return 0;
}

================g++ -O -S compile.cpp

.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 2
.p2align 4,,15
..globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl $16, %eax
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
call __alloca
call ___main
leave
xorl %eax, %eax
ret

What is actually going on? Why subtract 8 from esp (is that the stack
pointer?)
why this magical -16? what happens when alloca and main are called?
What
is leave and xorl? Is there a explanation of something like this or a
tutorial on
the web that can help me understand this?

Thanks a lot,
--j

Aug 27 '06 #1
2 4343
John a écrit :
I would like to understand the following C++/C program in assembly

int main(void){
return 0;
}

================g++ -O -S compile.cpp

.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 2
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl $16, %eax
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
call __alloca
call ___main
leave
xorl %eax, %eax
ret

What is actually going on? Why subtract 8 from esp (is that the stack
pointer?)
why this magical -16? what happens when alloca and main are called?
What
is leave and xorl? Is there a explanation of something like this or a
tutorial on
the web that can help me understand this?

Thanks a lot,
--j
In the wikipedia there is an example EXACTLY like yours.
GOTO
http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax

Aug 27 '06 #2


John wrote:
I would like to understand the following C++/C program in assembly

int main(void){
return 0;
}

================g++ -O -S compile.cpp

.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 2
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl $16, %eax
probably the argument to the 'alloca' call. Alloca allocates stack
space. Not sure why it is being called.
>
movl %esp, %ebp
>
subl $8, %esp
this is generally how local variables are allocated. The pushl ebp, move
esp, ebp, and subl 8, esp could be replaced by an enter instruction, but
that is a lot slower so most compilers do this explicitly. You haven't
declared local variables so this might have something to do with C++
exception handling.
>
andl $-16, %esp
align the stack. Stack alignment is done by GCC because floating point
accesses are significantly faster when aligned.
>
call __alloca
now allocate space on the stack. Not sure what is happening here. Could
be allocating something required for C++ exception use. or could be doing
alignment for the next call, or could be
allocating space for return data from the next call
>
call ___main
this is probably a C++ stub that somehow executes the constructors for
file-scope objects.
>
>
leave
restore ESP and EBP to what they were when the function entered
(same as movl %ebp, %esp, popl %ebp)
>
xorl %eax, %eax
this is the code for the 'return 0' statement. By convention integers
are returned in EAX. This instruction sets EAX to 0.
>
ret

What is actually going on? Why subtract 8 from esp (is that the stack
pointer?)
why this magical -16? what happens when alloca and main are called?
What
is leave and xorl? Is there a explanation of something like this or a
tutorial on
the web that can help me understand this?

Thanks a lot,
--j
..

Aug 27 '06 #3

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

Similar topics

14
by: Zac Hester | last post by:
I figured since a lot of us around here design sites for "customers" a lot, I'd ask a general question that might help a lot of us in the future. When dealing with clients asking for...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
3
by: Searcher7 | last post by:
I was wondering if anyone is aware of any websites or books that cover the stated goals and display the completed code for various programs, and also includes in detail the explanations for what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.