tags:

views:

219

answers:

5

Hi

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c

I also used firstly cpp and then try as but I'm getting errors.

I'm trying to build an assembler program from 3 different programs

prog1.c prog2.c prog.h

Is it correct to do gcc -S prog1.c prog2.c prog.h? Seems that is not correct. I don't know if I have to generate the assembler from each of them and then link them

Thanks

+9  A: 

According the manual:

`-S'
     Stop after the stage of compilation proper; do not assemble.  The
     output is in the form of an assembler code file for each
     non-assembler input file specified.

     By default, the assembler file name for a source file is made by
     replacing the suffix `.c', `.i', etc., with `.s'.

     Input files that don't require compilation are ignored.

so try gcc -S file.c.

Dirk Eddelbuettel
+5  A: 

From man gcc:

   -S     Stop  after the stage of compilation proper; do not
          assemble.  The output is an assembler code file for
          each non-assembler input file specified.

          By default, GCC makes the assembler file name for a
          source file by replacing  the  suffix  `.c',  `.i',
          etc., with `.s'.  Use -o to select another name.

          GCC ignores any input files that don't require com-
          pilation.
Tim
And consider also using the flag -fverbose-asm
mmagin
+4  A: 

If you're using gcc (as it seems) it's gcc -S.

Don't forget to specify the include paths with -I if needed.

gcc -I ../my_includes -S my_file.c

and you'll get my_file.s with the Assembler instructions.

Remo.D
A: 

Thanks a lot for the answers, they are very helpful.

I'm trying to build an assembler program from 3 different programs

prog1.c prog2.c prog.h

Is it correct to do gcc -S prog1.c prog2.c prog.h?

Peter
If you have additional things to say about your question you should edit the question, not post an answer. Answers will get sorted by votes, not chronological, thus additions as answers are likely to get lost.
bluebrother
ok, thanks,I did as you said
Peter
+1  A: 

objdump -d also works very nicely, and will give you the assembly listing for the whole binary (exe or shared lib).

This can be a lot clearer than using the compiler generated asm since calls to functions within the same source file can show up not yet resolved to their final locations.

Build your code with -g and you can also add --line and/or --source to the objdump flags.

Peeter Joot