Function return value doubt

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pradeep

    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!
  • Antoninus Twink

    #2
    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.

    Comment

    • Eric Sosman

      #3
      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

      Comment

      • Chris Dollin

        #4
        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

        Comment

        • Bartc

          #5
          Re: Function return value doubt


          "pradeep" <nospam@nospam. comwrote 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


          Comment

          Working...