By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,503 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,503 IT Pros & Developers. It's quick & easy.

Function return value doubt

P: n/a
Hello friends ~

We know that a C function will return a 32-bit value from a function
in %eax and a 64-bit value in %edx:%eax. But what about larger return
types, for example large structs? The only other spare register is
%ecx because %ebx needs to be preserved, but that only brings the
%possible total up to 96 bits. What happens after that?

Thanks for any information!
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 12 Apr 2008 at 13:24, pradeep wrote:
We know that a C function will return a 32-bit value from a function
in %eax and a 64-bit value in %edx:%eax.
Of course, there are also separate floating point registers, in which
floating point return values will be placed.
But what about larger return
types, for example large structs? The only other spare register is
%ecx because %ebx needs to be preserved, but that only brings the
%possible total up to 96 bits. What happens after that?
Typically, for sizes beyond 64 bits (on a 32-bit machine) the calling
function will allocate enough memory to store the return value and pass
a "hidden pointer" to this memory as an extra argument. When the called
function returns, it places the return value into this memory.

Obviously, some work is needed to take care of the memory used, and push
and pop the hidden pointer properly, but this all happens behind the
scenes and you probably don't need to worry about it unless you're using
inline assembly and doing something complicated.

Jun 27 '08 #2

P: n/a
pradeep wrote:
Hello friends ~

We know that a C function will return a 32-bit value from a function
in %eax and a 64-bit value in %edx:%eax.
Interesting. That explains, I guess, why there are no
C implementations for SPARC, VAX, PowerPC, ARM, zSeries, ...
But what about larger return
types, for example large structs? The only other spare register is
%ecx because %ebx needs to be preserved, but that only brings the
%possible total up to 96 bits. What happens after that?

Thanks for any information!
The mechanisms by which arguments are passed to functions
and values returned from them are the implementation's business,
and different implementations use different techniques. If
you want to know how one particular implementation handles
things, read that implementation's documentation and/or study
the code it generates. Be aware that your conclusions will be
specific to that particular implementation and will not be
valid for others.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3

P: n/a
pradeep wrote:
We know that a C function will return a 32-bit value from a function
in %eax and a 64-bit value in %edx:%eax.
In some x86 implementations, yes. But the ARM has no %eax or
%edx and yet manages to return results. But generalising ...
But what about larger return
types, for example large structs? The only other spare register is
%ecx because %ebx needs to be preserved, but that only brings the
%possible total up to 96 bits. What happens after that?
One tactic is:

The return type of the function is known. So the compiler can
allocate an object of that type in the callING function and
pass its address to the callED function as an additional hidden
argument; the callED function writes its result into that
object using the pointer argument. Then when control returns
to the callING function the compiler can arrange for code to
copy the value of the allocated object to its final destination.
A sufficiently clever compiler given sufficiently simple code
may be able to eliminate the copy by passing the address of the
final destination.

"Every programming problem can be solved by adding an additional
layer of indirection".

--
"Give as few orders as possible. Once you have given orders /Dune/
on a subject, you must always give orders on that subject."

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 27 '08 #4

P: n/a

"pradeep" <no****@nospam.comwrote in message news:ft**********@aioe.org...
Hello friends ~

We know that a C function will return a 32-bit value from a function
in %eax and a 64-bit value in %edx:%eax. But what about larger return
types, for example large structs? The only other spare register is
%ecx because %ebx needs to be preserved, but that only brings the
%possible total up to 96 bits. What happens after that?

Thanks for any information!
If your C compiler has an option to output assembly code, try it on some
code that manipulates larger structs.

Then you will get some idea of how it can be done.

--
Bart
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.