Behind the scenes of the return value from a C ++ function

What is behind the scenes of returning a value from a function in C ++?

In my understanding, whenever func. called the return address and the stack frame (with local variables, revered order of arguments and registers func.) is placed on the call stack.

But what happens when meeting execution returns to a statemenet? eg

int a( int b ){
   int c = b * 2;
   return c;
}

After colliding with the return statement, the C value stored in the EAX register is destroyed, the local variables are deleted, and the stack stack is removed from the call stack, after which the value in the EAX register is moved to the "returned address" memory?

Or did I misunderstand this concept?

All help is much appreciated. Thanks.

+6
source share
3

BTW, . ARM EAX.

. (). .


.

, . .

/ : . , , , .


. .

; .

. , . .


" ". ...

+4

++ .

, " ". , , , , " " , , .

, , , gcc :

a(int):                                  # @a(int)
        push    rbp                       
        mov     rbp, rsp                  
        mov     dword ptr [rbp - 4], edi  
        mov     edi, dword ptr [rbp - 4]
        shl     edi, 1
        mov     dword ptr [rbp - 8], edi
        mov     eax, dword ptr [rbp - 8]
        pop     rbp
        ret

:

extern void foo(int x);

int main()
{
    foo(a(2));
}

:

main:                                   # @main
        push    rbp
        mov     rbp, rsp
        mov     edi, 2
        call    a(int)
        mov     edi, eax
        call    foo(int)
        xor     eax, eax
        pop     rbp
        ret

, foo 4. a . , .

foo , , a foo.

, , a, . foo , "" a(2).

, :

main:                                   # @main
        push    rax
        mov     edi, 4           # note: 'as if' a(2)
        call    foo(int)
        xor     eax, eax
        pop     rcx
        ret

a ( gcc) :

a(int):                                  # @a(int)
# 'as if' we created a variable and did some arithmetic, 
# stored the result and then returned the result
        lea     eax, [rdi + rdi]   
        ret
+2

, @Thomas Matthews ...

, , , , .

, , gcc:

int func(int a, int b)
{
    return a + b;
}

int main(int argc, char ** argv)
{
    int a, b;
    a = b = 100;

    int c = func(a, b);
}

, , (: printf, ).

-ggdb, gdb gdb <application>, , , disassemble. :

Breakpoint 1, func (a=100, b=100) at program.cpp:3
3           return a + b;
(gdb) disas
Dump of assembler code for function func(int, int):
   0x00000000004004d6 <+0>:     push   %rbp
   0x00000000004004d7 <+1>:     mov    %rsp,%rbp
   0x00000000004004da <+4>:     mov    %edi,-0x4(%rbp)
   0x00000000004004dd <+7>:     mov    %esi,-0x8(%rbp)
=> 0x00000000004004e0 <+10>:    mov    -0x4(%rbp),%edx
   0x00000000004004e3 <+13>:    mov    -0x8(%rbp),%eax
   0x00000000004004e6 <+16>:    add    %edx,%eax
   0x00000000004004e8 <+18>:    pop    %rbp
   0x00000000004004e9 <+19>:    retq
End of assembler dump.

, , , - rbp (pop %rbp), (retq). , - .

+1

All Articles