PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Function return value doubt
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Function return value doubt

Réponse
 
LinkBack Outils de la discussion
Vieux 12/04/2008, 14h24   #1
pradeep
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Function return value doubt

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!
  Réponse avec citation
Vieux 12/04/2008, 14h34   #2
Antoninus Twink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function return value doubt

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.

  Réponse avec citation
Vieux 12/04/2008, 15h50   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function return value doubt

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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 14/04/2008, 12h16   #4
Chris Dollin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function return value doubt

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

  Réponse avec citation
Vieux 14/04/2008, 18h07   #5
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function return value doubt


"pradeep" <nospam@nospam.com> wrote in message news:ftqd66$5rb$1@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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14220 seconds with 13 queries