So what does it add to the compiled assembly code?
Let me answer this in the most literal way possible (assuming that's what you asked) and then I'll explain a little more. It adds nothing to the output assembly. No whitespace/newline/comments, nothing. The best way to illustrate this is to show what happens when you have nothing significant to compile in the first place. For example consider these two trivial functions with nothing to optimize:
void Test1(void) { }
void Test2(void) { asm(""); }
Compile it to assembly:
g++ -c -S test.cpp -o test.s
And look at the output (abridged):
_Z5Test1v:
endbr64
pushq %rbp
movq %rsp, %rbp
nop
popq %rbp
ret
_Z5Test2v:
endbr64
pushq %rbp
movq %rsp, %rbp
nop
popq %rbp
ret
Note that they are the same. The compiler has literally inserted an empty string ("") into the assembly output.
Even still, this little asm("")
can have a profound effect on how the compiler generates code in non-trivial cases. Any user-supplied assembly is treated as volatile
, which means the compiler assumes it cannot "see" what is happening behind the ""
string or make assumptions about how it can be optimized. Operations cannot be re-ordered around it for example, or as Peter Cordes pointed out in the comments, if you put it in a loop, loop unrolling will be completely defeated.
In this sense what it "adds" to the compiled code is everything that the optimization passes could otherwise have done if the asm("")
was not present.
A treatment of exactly what various optimizers will do given a certain source input is totally beyond the scope of a SO question like this, but you can always play around with checking assembly output given various code and optimization flag combos.