Where in memory are return values ​​stored in memory?

Where in memory are return values ​​stored in memory?

Consider the following code:

int add(int a, int b) { int result = a+b; return result; } void main() { int sum = add(2, 3); } 

When add(2, 3) is called add(2, 3) 2 functional parameters are inserted into the stack, the stack frame pointer is pushed onto the stack, and the return address is pushed onto the stack. Then, the thread goes to add(...) , and local variables inside this function are also stored on the stack.

When add(...) completes and executes the return command ... where is the return value stored? How does [result] end in [sum] ?

+3
low level
source share
6 answers

It clearly depends on your hardware architecture and your compiler. On x86 x86 using gcc your code compiles to:

  .file "call.c" .text .globl add .type add, @function add: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 movq %rsp, %rbp .cfi_offset 6, -16 .cfi_def_cfa_register 6 movl %edi, -20(%rbp) movl %esi, -24(%rbp) movl -24(%rbp), %eax movl -20(%rbp), %edx leal (%rdx,%rax), %eax movl %eax, -4(%rbp) movl -4(%rbp), %eax ; return value placed in EAX leave ret .cfi_endproc .LFE0: .size add, .-add .globl main .type main, @function main: .LFB1: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 movq %rsp, %rbp .cfi_offset 6, -16 .cfi_def_cfa_register 6 subq $16, %rsp movl $3, %esi movl $2, %edi call add movl %eax, -4(%rbp) ; the result of add is stored in sum leave ret .cfi_endproc .LFE1: .size main, .-main .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3" .section .note.GNU-stack,"",@progbits 

Here, the compiler uses the EAX register to pass the result of add caller.

You can read x86 calling conventions on Wikipedia .

+5
source share

There is no general answer to this question, since it depends on the target architecture. Typically, there is a Binary API specification for any target architecture that defines this, and the compiler creates codes that work in accordance with that specification. Most architectures use a register to pass the return value back, simply because this is the fastest way to do this. This is only possible if the value fits into the register, of course. If not, they can use a pair of registers (for example, the lower 32 bit in one register, the upper 32 bit in another), or they will pass it back through the stack. Some architectures never use registers and always return via the stack. Since the caller must create a stack frame before calling the function (there are exceptions to this rule, but let's stay here with the default case), the stack frame still exists when the function returns to the caller, and the caller knows how to access it should know this, since he should also clear the stack frame on return. On most architectures, the caller clears the stack frame rather than the callee, since the caller knows how many arguments it passed through the stack (for example, for a C function that takes a variable number of arguments), while the caller does not (time does not compile, the caller can only know what is at run time), so it makes sense to let the caller clear it. And before doing this, the caller can read any value of the stack frame that he wants to receive.

+5
source share

On x86, the return value is placed in the EAX register (although this may depend on your actual agreement).

You can parse the code compiled from your source to see what happens for sure.

+3
source share

Function parameters, local variables and return values ​​can be pushed / pasted onto the stack or stored in internal CPU registers, they are highly system dependent.

0
source share

Usually in the battery. For a return value that does not fit into the battery, the battery will hold the pointer to the stack on the stack. This is the general scheme used on several platforms with which I worked at this level, but depends on the hardware, and I think that on the compiler / assembler.

0
source share

EAX is used to store the return value, if size allows (here it is); This is the action of the caller (the main thing in your case) to assign the contents of EAX to the amount

0
source share

All Articles