0

As execution of a function is completed, and instructions and local variables are removed from the stack, how is the return value stored in memory for the process' main program to use?

Algonomaly
  • 99
  • 1
  • 8

2 Answers2

1

How parameters are passed in, and values returned from, an executed function is known as the Calling Convention.

Ignoring runtime environments (Java and .NET, I'm looking at you) and scripted languages (any of them) and concetrating purely on native code on x86, there's a lot of them. You may have come across them if you've ever heard the term cdecl or stdcall amongst others.

Typically return values will be returned in registers. The cdecl convention, for example, returns data either in EAX (for integers and pointers) or ST0 (for floating-point values).

But the calling convention defines more than just the return format. It also defines how arguments are passed on (stack, or register and left to right or right to left) and who is responsible for cleaning the stack up (i.e., the caller or the callee). cdecl for example is an example of a convetion where the caller must clean the stack up, whilst stdcall the callee keeps the stack tidy.

Other conventions include fastcall, pascal and syscall, amongst others. Wikipedia has a good breakdown on them all, as does Microsoft's MSDN notes. You may also want to look at the SO question "stdcall and cdecl" which goes into cdecl and stdcall in detail.

Community
  • 1
  • 1
Chris J
  • 30,688
  • 6
  • 69
  • 111
0

I think a right answer - "It depends".. in general it's called 'calling conventions'. I think very good overview you can find here

Pay attention that this link is only x86 related, so for other architectures they can be completely different.

evilruff
  • 3,947
  • 1
  • 15
  • 27