Generate Assembly code and Binary code also

This is a discussion on Generate Assembly code and Binary code also within the C Programming forums, part of the General Programming Boards category; Dear all, Now I need to modify the existing source code. I added -S options to gcc command, so it ...

  1. #1
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46

    Generate Assembly code and Binary code also

    Dear all,
    Now I need to modify the existing source code.

    I added -S options to gcc command, so it generates the Assembly code, but the .o (object) files also are combined to Assembly code, so the linker fails while try to link object files.

    So, how to generate both Assembly code (to view), and binary code (to run and linking).

    Thank you very much

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Run gcc twice?

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Use an assembler to produce the binary file from the asm?

  4. #4
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear tabstop,

    Because the linker fails while linking object files, so gcc can not create the final result in Assembly code (but it is which I need)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    I don't see how that stops you from running gcc twice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're ... trying to use the linker to create assembler? That's not going to happen. Linkers turn object code into executable, that's all they do. You can't have a final executable in assembler.

  7. #7
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear,

    But in the first time, I run with -S options, it fails and cannot generate the final output

  8. #8
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear tabstop,

    But I can, for example
    gcc -S -o test test.c
    And gcc will generate test as assembly code file

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the first run (with just gcc -S) fails, then you have problems in your code. You need to fix those first.

    If you don't have compiler errors, then run it once with -S and once without -S (on your original code, not the -S output).

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Hannibal2010
    But in the first time, I run with -S options, it fails and cannot generate the final output
    So use the options separately: first use -S to get your assembly output, then run gcc again to compile and link.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear tabstop and laserlight,

    I think it is my root question:

    In a big project, with many Makefile and many gcc commands, which gcc command should be added -S option (it means, which is lastest gcc command to generate final output)

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Makefiles are like anything else, they go top to bottom.

    You should note, however, that once you've done virtually anything else it's too late to do -S -- once you've created an object file, there's no re-creating the assembler for it.

  13. #13
    Registered User Hannibal2010's Avatar
    Join Date
    Jun 2011
    Location
    Hanoi, Vietnam
    Posts
    46
    Dear,

    So how can I generate Assembly code of final output in the big project? I am really confuse now

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Hannibal2010
    So how can I generate Assembly code of final output in the big project?
    Why do you need it, as opposed to assembly output of indvidual translation units?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't. But that's okay, you don't want to, so it's all good.

    Now I admit I've never tried something like
    Code:
    gcc -S file1.c file2.c file3.c file4.c file5.c
    to see what happens. I expect that either you'll get a bunch of assembler files, or you'll get one big file that is a bunch of assembler files smushed together. If you do get a bunch of assembler files, I suppose you could cat them together in one big assembler file. What you would do with it, I have no idea.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert assembly>machine code, machine code>assembly
    By wenxinleong in forum C Programming
    Replies: 12
    Last Post: 06-23-2011, 10:42 PM
  2. Converting C code to DLX assembly code
    By xyz3 in forum C Programming
    Replies: 2
    Last Post: 05-17-2010, 02:01 PM
  3. GCC option to get assembly code AND C code
    By AntoineC in forum C Programming
    Replies: 2
    Last Post: 04-29-2010, 09:04 AM
  4. assembly code for release code?
    By George2 in forum Windows Programming
    Replies: 4
    Last Post: 07-09-2008, 11:17 AM
  5. To generate bar code from a number
    By darkducke in forum C Programming
    Replies: 18
    Last Post: 01-16-2008, 05:33 AM

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