Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function calling methods. 1

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
NL
I have a purely theoretical question about the various methods of passing arguments to a function.
There are two i am interested in:

+ call by value-returned (aka value-result).
+ call by result.

i think value-returned does the following:

+ make a local copy of the value the passed pointer points to.
+ work with this value locally.
+ upon function termination, store the value back at its original address.

Both calling methods are described in the art of assembly language but the difference between the two is very vaguely explained.

anybody know the difference?
 
I'm not familiar with The Art of Assembly Language, so the author's terminology might be different. However, the two basic divisions of parameter passing to a function with which I am familiar are:

by value, i.e., placing the actual value in a register, on the stack, or possibly in a global variable

by reference, i.e., placing a pointer to the value in a register, on the stack, etc.

How a value is returned from a function is not necessarily dependent upon how a value was passed to the function.
 
To expand on rankamatuers post, Passing by value allows a function to modify the variable without those changes being seen by the calling function; any change to a passed variable are discarded and lost upon exiting the function. passing by reference allows the calling function to see any changes made by the called function; any alterations to the variable are "permanent." This terminology is the industry standard; what the author of "The Art of Assembly" is trying to say I have no clue, as I have not read his book.

The simplest solution is the best!
 
while rankamateur is absolutely right that how a function returns a value needn't depend on how values were passed to it, there is a relation between the two.

If you pass values by reference, you may not need to return anything whatsoever; the function can modify the variables pointed to by the references that were passed. Of course you may have other things you need to return, and these can be returned as values or as references. If you pass by value, absolutely everything will have to be explicitly returned, by value or reference: you'll have to return either the value itself or an address of somewhere where you've put the value.

If you're really interested in this sort of thing, it's worth looking at the chapters in most assembly books on the subject of interfacing with high-level languages. While you're not obliged to conform in assembly (why write assembly that looks as though it were written by a compiler?), high-level languages deal with passing and returning parameters in quite neat, globally useful ways.

 
If you pass by value, absolutely everything will have to be explicitly returned, by value or reference: you'll have to return either the value itself or an address of somewhere where you've put the value.

I'm confused. Are you saying that, if you don't return what you've passed by value, the calling routine will somehow lose the value(s) it passed?
 
No, sorry, I didn't express myself well, and perhaps I'm just confusing you by stating the obvious in a complicated way.

The calling routine will always retain all its local variables and so on.

If the caller passes something by value, then the called function has no way of communicating with the caller except by returning values or addresses of its results.

If the caller passes by reference, the called function can actually change the contents of the original variables, and therefore it doesn't actually need to return a value or a reference to a value. The information is already there for the caller.

Hope that's clearer?
 
Call by value-result is one of legal Fortran methods (to encourage of memory access locality). But it's a very interesting thing: what's call by result?..
 
call by result is similar to value-result, but slightly different. I can't quite understand from the text in Art of Assembly what the exact difference is. And i'm even not sure that i understand value-result either, all i know is what i posted earlier.

If anybody has the Art of Assembly book, the material that i am referring to is in chapter 11.

thanks.
 
In chapter 11 the author describes several methods of parameter passing:

1. Pass by Value - what you'd expect, passing the actual value to the called routine.

2. Pass by Reference: passing a pointer to the variable or structure. The author states that this technique can be less efficient than pass by value because you have to dereference the pointer on each access of the value.

3. Pass by Value-Returned or Value-Result: passing a pointer to the variable, then making a local copy, using the local copy, and before exiting the called routine, passing the local (and presumably changed) copy back through the pointer.

4. Pass by Result: unlike 3 above, you're not passing data to the called procedure, just providing a pointer the called procedure can use to return its result.

He also mentiones 2 other parameter passing methods, Pass by Name and Pass by Lazy-Evaluation, that appear to have no application in assembly language programming.
 
Thank you, rankamateur. It's a special case (of by value-by result?) for out parameters...
 
I think differentiating between (3) and (4) in rankamateur's quoted list rather over-complicates the issue. From the caller's point of view there is no difference whatsoever. From the called function's point of view it's a matter of efficient internal writing. You've only got limited segment registers, so it's hard to handle a lot of variables by reference. If you've got more variables than segment registers, the easiest thing is to put several of the variables somewhere local so you can address them all by ss. And it avoids the segment prefix bytes that are necessary if you go esoteric and use fs, gs etc. (This is all thinking about an IBM-PC but the situation would be similar on any processor)

 
rankamateur

you've summed it up much clearer than the book has. i see i was right about pass by value-result in my first post. but for me it was all about the difference between the two. thank you.

lionelhill

or you can declare variables global, all adjacent to each other, and pass one (far or near) pointer to a function. provided ofcourse that the variables are all the same size and thus have a fixed offset, you can now access multiple variables, and still pass by reference. some C programmers might argue that global variables are definitely not the way to go, but in assembly it speeds up execution considerably (the code becomes somewhat less modular though).
 
oh, absolutely. Nothing wrong with global variables. If you need to follow good C-practice, use a C compiler. Assembler should be reserved for places where good C practice won't cut the mustard (admittedly quite a small range of places).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top