Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile to assembly with gcc

Tags:

c

linux

x86

gcc

How do I compile to assembly instead of an executable with gcc. I know there is an -S flag, but where do I use it in the makefile. For example, if I use flags -O3 -o exe_name, where should I put the -S flag?

like image 528
MetallicPriest Avatar asked Nov 05 '11 17:11

MetallicPriest


People also ask

Can you compile assembly with gcc?

Programmers can write their own assembly code by hand and compile it with gcc into a binary executable program. For example, to implement a function in assembly, add code to a . s file and use gcc to compile it.

Does gcc compile to assembly or machine code?

GCC compiles to assembler. Some other compilers don't. For example, LLVM-GCC compiles to LLVM-assembly or LLVM-bytecode, which is then compiled to machine code. Almost all compilers have some sort of internal representation, LLVM-GCC use LLVM, and, IIRC, GCC uses something called GIMPLE.

Does gcc output assembly?

Luckily, gcc does not output binary machine code directly. Instead, it internally writes assembler code, which then is translated by as into binary machine code (actually, gcc creates more intermediate structures). This internal assembler code can be outputted to a file, with some annotation to make it easier to read.


1 Answers

I suggest also using -fverbose-asm because then the generated assembler has some generated comments which "explains" the code. For example:

gcc -S -fverbose-asm -O2 foo.c 

would generate in foo.s (with some comments) the assembler code produced by compiling foo.c

And to understand what the GCC optimizations are doing one could even try -fdump-tree-all (but this produces hundreds of files!).

like image 65
Basile Starynkevitch Avatar answered Sep 27 '22 16:09

Basile Starynkevitch