I wrote a few simple programs to test gdb and its disassemble command and noticed that for one of programs the output seemed a bit verbose. Here is the two programs with assembly output attached.
C++
#include <iostream>
#include "math.hpp"
int main(int, char**)
{
std::cout << math::sign(1) << std::endl;
return 0;
}
ASM
Dump of assembler code for function main(int, char**):
0x0000000000401500 <+0>: push %rbp
0x0000000000401501 <+1>: push %r15
0x0000000000401503 <+3>: push %r14
0x0000000000401505 <+5>: push %r13
0x0000000000401507 <+7>: push %r12
0x0000000000401509 <+9>: push %rdi
0x000000000040150a <+10>: push %rsi
0x000000000040150b <+11>: push %rbx
0x000000000040150c <+12>: sub $0x148,%rsp
0x0000000000401513 <+19>: lea 0x80(%rsp),%rbp
0x000000000040151b <+27>: movaps %xmm6,0x20(%rbp)
0x000000000040151f <+31>: movaps %xmm7,0x30(%rbp)
0x0000000000401523 <+35>: movaps %xmm8,0x40(%rbp)
0x0000000000401528 <+40>: movaps %xmm9,0x50(%rbp)
0x000000000040152d <+45>: movaps %xmm10,0x60(%rbp)
0x0000000000401532 <+50>: movaps %xmm11,0x70(%rbp)
0x0000000000401537 <+55>: movaps %xmm12,0x80(%rbp)
0x000000000040153f <+63>: movaps %xmm13,0x90(%rbp)
0x0000000000401547 <+71>: movaps %xmm14,0xa0(%rbp)
0x000000000040154f <+79>: movaps %xmm15,0xb0(%rbp)
0x0000000000401557 <+87>: mov %ecx,0x110(%rbp)
0x000000000040155d <+93>: mov %rdx,0x118(%rbp)
0x0000000000401564 <+100>: lea 0x1b15(%rip),%rax # 0x403080 <__gxx_personality_sj0>
0x000000000040156b <+107>: mov %rax,-0x30(%rbp)
0x000000000040156f <+111>: lea 0x9faa(%rip),%rax # 0x40b520 <___DTOR_LIST__+16>
0x0000000000401576 <+118>: mov %rax,-0x28(%rbp)
0x000000000040157a <+122>: lea -0x20(%rbp),%rax
0x000000000040157e <+126>: lea 0x20(%rbp),%rdx
0x0000000000401582 <+130>: mov %rdx,(%rax)
0x0000000000401585 <+133>: lea 0x5a(%rip),%rdx # 0x4015e6 <__fu0__ZSt4cout+34>
0x000000000040158c <+140>: mov %rdx,0x8(%rax)
0x0000000000401590 <+144>: mov %rsp,0x10(%rax)
0x0000000000401594 <+148>: lea -0x60(%rbp),%rax
0x0000000000401598 <+152>: mov %rax,%rcx
0x000000000040159b <+155>: callq 0x404880 <_Unwind_SjLj_Register>
0x00000000004015a0 <+160>: callq 0x4042f0 <__main>
0x00000000004015a5 <+165>: movl $0x1,0x1c(%rbp)
0x00000000004015ac <+172>: lea 0x1c(%rbp),%rax
0x00000000004015b0 <+176>: mov %rax,%rcx
0x00000000004015b3 <+179>: callq 0x409ad0 <math::sign<int>(int const&)>
0x00000000004015b8 <+184>: movl $0x1,-0x58(%rbp)
0x00000000004015bf <+191>: mov %eax,%edx
0x00000000004015c1 <+193>: lea 0x110cc(%rip),%rcx # 0x412694 <__imp__ZSt4cout>
0x00000000004015c8 <+4>: callq 0x403090 <_ZNSolsEi>
0x00000000004015cd <+9>: lea 0x1ab4(%rip),%rdx # 0x403088 <_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_>
0x00000000004015d4 <+16>: mov %rax,%rcx
0x00000000004015d7 <+19>: callq 0x403098 <_ZNSolsEPFRSoS_E>
0x00000000004015dc <+24>: mov $0x0,%eax
0x00000000004015e1 <+29>: mov %eax,0xc(%rbp)
0x00000000004015e4 <+32>: jmp 0x4015fd <__fu0__ZSt4cout+57>
0x00000000004015e6 <+34>: lea -0x20(%rbp),%rbp
0x00000000004015ea <+38>: mov -0x50(%rbp),%rax
0x00000000004015ee <+42>: movl $0xffffffff,-0x58(%rbp)
0x00000000004015f5 <+49>: mov %rax,%rcx
0x00000000004015f8 <+52>: callq 0x404c40 <_Unwind_SjLj_Resume>
0x00000000004015fd <+57>: lea -0x60(%rbp),%rax
0x0000000000401601 <+61>: mov %rax,%rcx
0x0000000000401604 <+64>: callq 0x4048e0 <_Unwind_SjLj_Unregister>
0x0000000000401609 <+69>: mov 0xc(%rbp),%eax
0x000000000040160c <+72>: movaps 0x20(%rbp),%xmm6
0x0000000000401610 <+76>: movaps 0x30(%rbp),%xmm7
0x0000000000401614 <+80>: movaps 0x40(%rbp),%xmm8
0x0000000000401619 <+85>: movaps 0x50(%rbp),%xmm9
0x000000000040161e <+90>: movaps 0x60(%rbp),%xmm10
0x0000000000401623 <+95>: movaps 0x70(%rbp),%xmm11
0x0000000000401628 <+100>: movaps 0x80(%rbp),%xmm12
0x0000000000401630 <+108>: movaps 0x90(%rbp),%xmm13
0x0000000000401638 <+116>: movaps 0xa0(%rbp),%xmm14
0x0000000000401640 <+124>: movaps 0xb0(%rbp),%xmm15
0x0000000000401648 <+132>: add $0x148,%rsp
0x000000000040164f <+139>: pop %rbx
0x0000000000401650 <+140>: pop %rsi
0x0000000000401651 <+141>: pop %rdi
0x0000000000401652 <+142>: pop %r12
0x0000000000401654 <+144>: pop %r13
0x0000000000401656 <+146>: pop %r14
0x0000000000401658 <+148>: pop %r15
0x000000000040165a <+150>: pop %rbp
0x000000000040165b <+151>: retq
End of assembler dump.
C++
#include <iostream>
#include "math.hpp"
int main(int, char**)
{
int x = math::sign(1);
std::cout << x << std::endl;
return 0;
}
ASM
Dump of assembler code for function main(int, char**):
0x0000000000401500 <+0>: push %rbp
0x0000000000401501 <+1>: mov %rsp,%rbp
0x0000000000401504 <+4>: sub $0x30,%rsp
0x0000000000401508 <+8>: mov %ecx,0x10(%rbp)
0x000000000040150b <+11>: mov %rdx,0x18(%rbp)
0x000000000040150f <+15>: callq 0x4041e0 <__main>
0x0000000000401514 <+20>: movl $0x1,-0x8(%rbp)
0x000000000040151b <+27>: lea -0x8(%rbp),%rax
0x000000000040151f <+31>: mov %rax,%rcx
0x0000000000401522 <+34>: callq 0x4099c0 <math::sign<int>(int const&)>
0x0000000000401527 <+39>: mov %eax,-0x4(%rbp)
0x000000000040152a <+42>: mov -0x4(%rbp),%eax
0x000000000040152d <+45>: mov %eax,%edx
0x000000000040152f <+47>: lea 0x1115e(%rip),%rcx # 0x412694 <__imp__ZSt4cout>
0x0000000000401536 <+4>: callq 0x402f78 <_ZNSolsEi>
0x000000000040153b <+9>: lea 0x1a2e(%rip),%rdx # 0x402f70 <_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_>
0x0000000000401542 <+16>: mov %rax,%rcx
0x0000000000401545 <+19>: callq 0x402f80 <_ZNSolsEPFRSoS_E>
0x000000000040154a <+24>: mov $0x0,%eax
0x000000000040154f <+29>: add $0x30,%rsp
0x0000000000401553 <+33>: pop %rbp
0x0000000000401554 <+34>: retq
End of assembler dump.
So basically I am confused as to why using a temporary variable in this case produces less assembly code?
If any more information is needed please don't hesitate to ask
Thanks