2

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

3
  • You should remove prog.h from the list of files you have to compile. Commented Nov 15, 2009 at 23:19
  • but I need to include the header, how can I do that? Commented Nov 15, 2009 at 23:36
  • The header should be included by the C file. The header comprises important C language statements, but they don't constitute a complete program. Their purpose is to help you avoid reproducing a lot of boilerplate stuff over and over again. Instead you put it in a header file and included it in your C file. Commented Nov 16, 2009 at 5:02

4 Answers 4

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

And consider also using the flag -fverbose-asm
5

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.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.