Thread: assembly and C

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534

    assembly and C

    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
    Code:
    test $ cat hello1.c
    #include <stdio.h>
    
    void main(void)
    {
         printf("hello, world\n");
    }
    And the assembly output from gcc
    Code:
    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.
    Last edited by kermit; 09-12-2004 at 10:36 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    In this case, it's simply the return result of printf()
    Which for keen manual readers is the number of characters printed
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Salem
    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()?

    ~/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Salem
    > 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?

Popular pages Recent additions subscribe to a feed