PDA

View Full Version : assembly and C



kermit
09-12-2004, 10:30 AM
Its was hard to determine where this post belonged, so I put it here...

I have been playing with two versions of a 'hello, world' program, in order to see the differences produced by declaring main as returning an int, and declaring main as not returning anything. When I declare main to return an int, and return 0, everything goes as expected, and when I check the exit status code it is indeed 0. But when I do a void main version of the program, I get an exit status of 13. So I have done a fair bit of searching, and the best I can come up with is that the 13 I get on my machine is something left over in the register eax. (When I ran the program in gdb, and checked eax, sure enough it was 13 stored there) Anyway, I am not strong with assembly, and so I am not sure of how eax ends up holding the value 13, and I was wondering if someone could shed some light on the matter for me. Here is the code I am using:

C version


test $ cat hello1.c
#include <stdio.h>

void main(void)
{
printf("hello, world\n");
}


And the assembly output from gcc


test $ cat hello1.s
.file "hello1.c"
.section .rodata
.LC0:
.string "hello, world\n"
.text
.align 2
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
leave
ret


I am not interested in using void main, but simply am trying to discover some of the inner workings of things.

Salem
09-12-2004, 10:59 AM
In this case, it's simply the return result of printf()
Which for keen manual readers is the number of characters printed

kermit
09-12-2004, 07:32 PM
In this case, it's simply the return result of printf()
Which for keen manual readers is the number of characters printed

Ah ok - thanks Salem - and yes I will go read the man page now.. ;)

edit::

If anyone who reads this has trouble finding info about the return value of printf by using 'man printf', try using 'man 3 printf'

Another thing... I was wondering if anyone can tell me if the startup routine would interpret that number 13 as if I had explicitly returned 13 from main()?

~/

Salem
09-13-2004, 02:33 AM
> I was wondering if anyone can tell me if the startup routine would interpret that number 13 as if I had explicitly returned 13 from main()?
Yes it would

kermit
09-13-2004, 04:18 AM
> I was wondering if anyone can tell me if the startup routine would interpret that number 13 as if I had explicitly returned 13 from main()?
Yes it would

So if you were checking against WEXITSTATUS in a parent program, having an unknown value returned from the child process could have some funny results? Or am I missing something?