473,563 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Jun 27 '08 #1
4 1642
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1217
by: Paul Taylor | last post by:
I want to show a figure based on an numeric entry that more than 20 (or 21and higher). If Number value is 21 or higher then the amount returned is Number value * 120 (£) * 10 (%) to give a discount on 21 and higher. How to I add that function to this line of code. SAmt0 = Math.round(eval(form.Number.value * 12000 * 0.1));...
4
8384
by: Ken | last post by:
Hello I am trying to change the color of a font in a text box. I have been reading about and trying various examples found it this group, but still can't get it right. Here is where I am now: <script language="JavaScript">
21
3560
by: Joakim Hove | last post by:
Hello, I have implemented a small library with a function a datatype to manage temporary storage, and handle out correctly casted storage. The function to get a double pointer is for instance: double * work_get_double(work_type *work, size_t size) {} Now, if the work area is not sufficiently large, the function fails,
12
9485
by: Newbie | last post by:
how can i call an oracle function to get data without using a select statement or stored procedures? given a project_no, i need to call the function: ops$sqltime.pa_new_job_no_fn which will return the next job_no thanks in advance.
6
9253
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will = "kelly" with no error // but string s2 = "kelly".Substring(0,200) // results in // ArgumentOutOfRangeException
1
1819
by: shiniskumar | last post by:
i go tto perform some validation while ck=liking a button without refreshing the value.. i used ajax.My code is as follows...on button click i call the function validateGridAdd(); Inside tat function i need to call the function tat uses ajax to go to the server and come back.. the resultant value is to be obtained in the main function...
13
13166
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
2
1765
by: DanYan | last post by:
So I was doing some stuff in Javascript, and I want to get access to a function's scope chain. As a simplified example of what I actually am trying to do, suppose I have this: function add(b) { return function(a) { return a + b; }; }
5
1759
by: hirsh.dan | last post by:
Hello to all, i have the following functions: string File::readLine(){ char ch; string str; ch = read(); while(ch != LF && ch != CR && ch != -1){ str.append(1,ch); ch = read(); }
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.