English 中文(简体)
Generate assembler code from C file in linux
原标题:
  • 时间:2009-11-15 22:10:43
  •  标签:
  • c
  • unix
  • gcc

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

最佳回答

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.

问题回答

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.

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.

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.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签