Any explanations

J

John

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
 
I

Ian Collins

John said:
I would like to understand the following C++/C program in assembly
You compiled it with g++, so it's a C++ program.
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?)

Possibly to reserve space for any exception objects.
why this magical -16? what happens when alloca and main are called?
What

Probably to make sure the stack pointer is correctly aligned, -16 is
0xfffffff0.
is leave and xorl? Is there a explanation of something like this or a
tutorial on

xorl %eax, %eax is a quick register to register techinque to zero eax,
which is used for the return value.
the web that can help me understand this?
Have a look for gcc documentation on the web, where yoou will also find
x86 programming information.
 
J

Jens Theisen

Hello John,

see the other reply, but I'd like to point out that you're not looking
at main's disassembly but _main, which for some reason calls some
___main function. I'm don't know much about this, but it occurs to be
that you should have the real main disassembly somehwere else in you
listing (it looks different when I try it on gcc, but I don't know what
version you use).
================g++ -O -S compile.cpp

You might want to compile without -O when you look at disassemblies,
except a certain bug is manifesting only then.
is leave and xorl? Is there a explanation of something like this or a

leave pops ebp and returns.
tutorial on
the web that can help me understand this?

I don't know of good assembler web resources, but after you found that,
have search for "processor supplement" on goolge to find documents that
describe calling conventions / stack layout you need in order to
understand the disassembly.

Jens
 
J

jacob navia

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
 
D

david

John said:
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.

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

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top