Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I have written simple Hello world program in C. Compiled using gcc. (gcc hello.c -s hello.s) to generate assembly code. When i see the assembly code Helloworld program, i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    3

    Various section of code post compiling just to provide Assembly code


    Hi,

    I have written simple Hello world program in C. Compiled using gcc.
    (gcc hello.c -s hello.s) to generate assembly code.
    When i see the assembly code Helloworld program, i happen to see only assembly equivalent to C prog.
    I expected to see startup code and start up code calling main function details but in really i couldnt see that.
    Can any one give me some details what i need to do while compiling to see the expected details?
    Thanks in advance in providing the required information.


    Thanks,
    -Janardhan N L

  2. #2
    Just Joined!
    Join Date
    May 2011
    Location
    Brazil
    Posts
    21
    Hey,

    All assembly code you are seeing is equivalent to your hello.c program.

    Probably you used a printf calling and you included some defines. When "transforming" your C code to assembly the toolchain(compiler, linker, assembler) must insert all necessary things to transform C code to a real code.

    All the other code you are seeing is added because of directives and because of the C library

  3. #3
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location
    Hell..no literally, this state is hell..
    Posts
    192
    Quote Originally Posted by n_janardhana View Post
    Hi,

    I have written simple Hello world program in C. Compiled using gcc.
    (gcc hello.c -s hello.s) to generate assembly code.
    When i see the assembly code Helloworld program, i happen to see only assembly equivalent to C prog.
    I expected to see startup code and start up code calling main function details but in really i couldnt see that.
    Can any one give me some details what i need to do while compiling to see the expected details?
    Thanks in advance in providing the required information.


    Thanks,
    -Janardhan N L
    I know its been awhile and it probably means nothing to you now...

    Though as there is no other startup code or anything. You wrote a 'program' as you see it, but it's is simply a main function. There is no supporting code.

    When you run the application the OS will create a process, allocate memory for the image, load the image, and all its other house-keeping, then set the execution pointer for that process to the label of your main function (which is all the function really is). Then the OS will continue all of its other duties, context switching in and out and such until there are no more instructions in the executable image, in which case it will do all its other housekeeping of unallocating the memory used for the image, making sure no child processes, open pipes, sockets, returning any allocated memory, etc., and that's that.

    Then, as levi pointed out, your printf function was simply a call to an address (most likely mapped by the OS somewhere else) which ended up putting the execution pointer in the C runtime library's print function, where it proceeded to do all the systemy stuff to output.

    If you want to see assembly in action, don't use the C runtime library..as weird as it sounds, 'invisible' code will give you better results in your case, so if you take a program like this, and disassemble it, things might be easier for you to look at.

    Note: Code hasn't been tested.
    Code:
    int main(int argc, char *argv[])
    {
         X = 5;
         Y = 3;
         return X + Y;
    }
    Then you hope the compiler doesn't seize the opportunity to optimize that into:
    Code:
    __main:
         movl    $8, %eax
    If it does, then simply disable compiler optimizations...but I can't recall the flag(s) for that off the top of my head.

    Hope that helps some if you still cared about this and didn't figure it out, lol.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •