Take the 2-minute tour ×

gcc and g++ are capable of compiling c++ code with various optimizations. gcc / g++ can also output assembly instructions. These optimizations are applied before outputting asm code, I believe.

Often gcc / g++ code is faster than anything I can produce in asm, probably because I am new to assembler, and so I don't know very many instructions. (Basically I am a bit rubbish.)

A good way to learn about optimizations is to inspect the assembly level output from the C++ compiler, or so I am told.

Is it possible to compile assembly code produced by gcc or g++ using gcc or g++, or something else entirely like gas or nasm?

There are two reasons I ask:

Firstly, it would be cool to be able to produce my own asm code, and compile it with gcc optimizations, and see if any differences appear. This is more of a learning tool really.

The second reason for asking would be to check if I can speed up gcc's assembly code. Probably I can't, I know gcc is pretty much the expert at optimizations, and so I'm not expecting to gain anything here, but it would make an interesting challenge - beat the compiler!

So yeah, is that sort of thing possible?

share|improve this question
GCC only generates assembly. When you compile a program, GCC produces the assembly and then calls as (gas) to do the assembly part. –  DaoWen Jul 27 '13 at 0:29
@DaoWen Ah, excellent, so it is possible? I'm guessing gcc main.c -s -o main.s then as main.s -o main.o - but what should I say to gas to tell it what format it needs, and should it be .s or .S ? –  Edward Bird Jul 27 '13 at 0:31
Optimizing assembly code would be really difficult for a computer to do... Other than perhaps changing one instruction to an equivalent of it that takes less time, optimization would be difficult. This is because of the same reason that there are no good decompilers of machine code - machine code is so precise that it's hard to tell what it's doing at a practical level. Because the computer could not tell what you are trying to do, it has not way of optimizing. –  Forgive Goto Jul 27 '13 at 0:31
@JesusPlusPlus11 What's going on with -O<1,2,3> and -os then? –  Edward Bird Jul 27 '13 at 0:32
@EdwardBird Yeah, so it's like this - code can be optimized by the computer if the computer understands what the code does. The computer can understand what C++ code does, but not what assembly code does. Why? Imagine trying to understand what an image represents if you were to only look at it pixel by pixel at a time, vs looking at the entire image as a whole. –  Forgive Goto Jul 27 '13 at 0:42
show 3 more comments

1 Answer

GCC does optimization on the IR (intermediate representation) of the source code, so all optimizations are applied before the assembly code is created. Of course, there are probably some things you could consider as "optimizations" in how they choose which assembly instructions to emit for a given set of operations—but again that's decided at the IR-level.

I think you might find this answer helpful too: Get Assembly code after every optimization GCC makes?

share|improve this answer
yeah that's useful thanks –  Edward Bird Jul 27 '13 at 0:57
add comment

Your Answer


