The assembler code of my C function

This is my C function:

int reg(struct my_callback_struct *p, int data)
{
   return p->data = data;
}

This is the same in the assembly:

0x000000000040057d <+0>:  push   %rbp
0x000000000040057e <+1>:  mov    %rsp,%rbp
0x0000000000400581 <+4>:  mov    %rdi,-0x8(%rbp)
0x0000000000400585 <+8>:  mov    %esi,-0xc(%rbp)
0x0000000000400588 <+11>: mov    -0x8(%rbp),%rax
0x000000000040058c <+15>: mov    -0xc(%rbp),%edx
0x000000000040058f <+18>: mov    %edx,(%rax)
0x0000000000400591 <+20>: mov    -0x8(%rbp),%rax
0x0000000000400595 <+24>: mov    (%rax),%eax
0x0000000000400597 <+26>: pop    %rbp
0x0000000000400598 <+27>: retq

I think I understand what is happening. $rdicontains a pointer (address) and $esinumber 12.

This is how I called the function:

p->callback_func(p,12);

I dont understand what:

0x0000000000400591 <+20>: mov -0x8(%rbp),%rax

Since <+11>we already filled in the $raxaddress of the pointer. Why download it twice?

+4
source share
1 answer

Indeed, the code is true in that the instructions perform the functions called by the C code. But even the most trivial optimizations have not been performed.

This is easily fixed by including some level of compiler optimization. Probably the first level will clear the excess load no matter which compiler is used.

, , .

+2

All Articles