4

I have a question about assembly code generated by GCC (-S option). Since, I am new to assembly language and know very little about it, the question will be very primitive. Still, I hope somebody will answer:

Suppose, I have this C code:

main(){

    int x = 15; 

    int y = 6;

    int z = x - y;


    return 0;
}

If we look at the assembly code (especially the part corresponding to int z = x - y ), we see:

main:

...
subl    $16, %esp
movl    $15, -4(%ebp)
movl    $6, -8(%ebp)
movl    -8(%ebp), %eax
movl    -4(%ebp), %edx
movl    %edx, %ecx
subl    %eax, %ecx
movl    %ecx, %eax
movl    %eax, -12(%ebp)
...

Why doesn't GCC generate something like this, which is less copying things around.

main:

...
movl    $15, -4(%ebp)
movl    $6, -8(%ebp)
movl    -8(%ebp), %edx          
movl    -4(%ebp), %eax          
subl    %edx, %eax              
movl    %eax, -12(%ebp)
...

P.S.

Linux zion-5 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686 GNU/Linux gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)

Mysticial
  • 464,885
  • 45
  • 335
  • 332
Jared Nash
  • 75
  • 1
  • 4

1 Answers1

7

First, as Mysticial commented, you should turn on some optimizations. Try passing -O2 (or -O3, or just -O1) to gcc. If you want to understand more the generated assembly code, also pass -fverbose-asm. If you want to understand why the code is generated (or not generated), learn GCC internals (perhaps pass also -fdump-tree-all and -fdump-rtl-all which produces a big lot of internal dump files).

Some slides on MELT (MELT is a domain specific language to extend GCC) might help and give other references.

You might be surprised by the amount of optimizations GCC can give you, when asked to. By default GCC does not optimize. There are some optimizations which you should explicitly ask for (not even done at -O3).

Recent versions of GCC probably optimize more than older ones. Current GCC version in 2021 is GCC 11.

PS. I don't work anymore on MELT (abandoned it in 2017). in 2021, see also Bismon, RefPerSys, Frama-C.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I also recommend besides the -fdump-tree-all the -fdump-rtl-all which annotates the assembler output with the the internal gcc representation. – flolo Sep 08 '12 at 10:27
  • 2
    reminds me how I had some function for calculating the norm of a vector. when seeing how gcc would optimize it, I had the following outcome (with constant input): -O1: Function inlined, -O2: also the loop was unrolled, -O3: norm of the constant vector was statically in the code, no calculation at all. (mightve mixed up -O1 and -O2 though) – Jonas Schäfer Sep 08 '12 at 10:34
  • the MELT site looks like a spam site currently, was the URL changed? – qwr Sep 12 '18 at 04:22
  • Corrected. I added a link to some archive – Basile Starynkevitch Sep 12 '18 at 04:38
  • 1
    @JonasSchäfer: If you want to see how gcc would calculate something, *don't give it a constant input*. Just write a function that takes args and returns a value. See [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). You don't need a `main` if you want to look at the asm instead of running it. – Peter Cordes Sep 12 '18 at 16:13