0

When I compile a simple C code into assembler (x86-64 under gcc, but it doesn't matter), the labels corresponding to the C functions each have the same construction :

void foo() {

}

Is compiled in :

foo:
        push    rbp
        mov     rbp, rsp
        nop
        pop     rbp
        ret

Why? This function does nothing. I thought I understood that these assembler instructions are "stack pointers". But I still don't grasp their uses in the program. Could you explain that to me?

ino
  • 39
  • 5
  • 2
    compile with `-O2` and the assembly is `ret`. – bolov May 01 '18 at 20:26
  • @bolov: Thank you, and sorry for the possible duplication. As far as the compilation parameter is concerned, it does work. However, why doesn't the compiler do this by default? Are there any advantages/disadvantages? – ino May 01 '18 at 20:29
  • 1
    Possibly optimisation never calls or codes a function that does nothing. – Weather Vane May 01 '18 at 20:30
  • its a generic thing that not all compilers necessarily do. prologue/epilogue or google stack frame, etc...how often are there functions with parameters vs functions without parameters? or with or without local variables. cheap and easy to just put that code in there and assume its there, later on if you optimize you add more work to your compiler to maybe take it out. – old_timer May 01 '18 at 20:32
  • @old_timer: Would this mean that these assembly code operations (given above) are not optimized, and that they can slow down the program? – ino May 01 '18 at 20:32
  • 1
    Of course unnecesssary code can slow down. – Weather Vane May 01 '18 at 20:34
  • @WeatherVane: Okay, I understand. Thank you for your answer ;) – ino May 01 '18 at 20:35
  • the advantage is, that you did ran the compiler with optimizer switched off, which makes the compilation process much faster. When you are developing C or C++ application, you often need to run new code quickly to debug it, and verify what it does, but you don't care about it's slower performance, plus the unoptimized code is easier to follow for debugger to allow "per line" debugging. In the optimized code the lines are often reordered or removed completely, so stepping over "single line" in optimized code often makes little sense. – Ped7g May 01 '18 at 20:37
  • @Ped7g: Thanks, that's exactly what I expected :) – ino May 01 '18 at 20:45
  • unoptimized is the least risky and you will see some software standards where humans might be at risk, flight computers, cars, etc. they may dictate no optimization. it is easier or closer to possible to validate a compiler when you dont optimize. but for the rest of us it has to do with training and habits, some folks IDEs just do it, some folks do it by habit, others never learned to and dont. unoptimized has lots and lots of extra code in it variables are often kept in memory going back and forth rather than staying in a register. – old_timer May 01 '18 at 21:06
  • does the function actually have anything to store or access on the stack is arguably an optimization. so sure even this function with nothing going on, unoptimized could end up with some amount of generic framework code that most certainly burns execution time... – old_timer May 01 '18 at 21:07
  • A quick search on *function prologue* and *function epilogue* may be enlightening. – David C. Rankin May 02 '18 at 00:13
  • @old_timer: Would this mean that a sustained use of assembler variables (I'm thinking _NASM variables_, example: `MyVar db 'hello'`) would slow down the program? How can we do to get maximum and optimum optimization, while having a fast program in fact? – ino May 03 '18 at 20:50
  • declaring a variable doesnt necessarily have a direct performance cost (there can be side effects), if you dont use the variable and simply declare it then there is no code to execute to deal with it so you dont burn that time. – old_timer May 03 '18 at 22:44
  • @old_timer: thanks to you :D – ino May 05 '18 at 07:39

0 Answers0