assembly and C

This is a discussion on assembly and C within the Linux Programming forums, part of the Platform Specific Boards category; Its was hard to determine where this post belonged, so I put it here... I have been playing with two ...

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

    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 11: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
    32,834
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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
    32,834
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21