* how to return a pointer to pointer
@ 2007-10-06 11:28 Lope De Vega
2007-10-06 16:45 ` Robert Plantz
0 siblings, 1 reply; 4+ messages in thread
From: Lope De Vega @ 2007-10-06 11:28 UTC (permalink / raw)
To: linux-assembly
Hi list!
I'm wondering how do you guys use to do when you need
to return a pointer to a pointer from within a
function?
if I use local storage it get's swept out during
function's return.
I've though on pushing $0 onto the stack after or
before such a function's arguments, so I could either
store on it during this function's execution and then
moving it to eax at it's end so it will be visible
Any hints or comments?
Thanks.
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: how to return a pointer to pointer
2007-10-06 11:28 how to return a pointer to pointer Lope De Vega
@ 2007-10-06 16:45 ` Robert Plantz
2007-10-08 16:36 ` Lope De Vega
0 siblings, 1 reply; 4+ messages in thread
From: Robert Plantz @ 2007-10-06 16:45 UTC (permalink / raw)
To: Lope De Vega; +Cc: linux-assembly
From "System V Application Binary Interface"
Integral and pointer return values appear in %eax. A function that
returns a struct or union value places the address of the result
in %eax. Otherwise this is a scratch register.
Pointers to anything, even other pointers, are returned in eax.
Of course, the object it points to must not be in the stack frame, which
as you point out, is deleted when the function returns. (I'm using
"object" in the general sense here, not as in "object-oriented
programming.")
I try to design my code such that the function that creates and object
is the only one that can delete it. That is, I try not to write a
function that allocates memory, then returns a pointer to it. I allocate
the memory in the calling function, then pass a pointer to this memory
area to the function that will do something to the memory.
Perhaps if you can provide more details about what you need to do we can
give more advice.
Bob
On Sat, 2007-10-06 at 04:28 -0700, Lope De Vega wrote:
> Hi list!
>
> I'm wondering how do you guys use to do when you need
> to return a pointer to a pointer from within a
> function?
>
> if I use local storage it get's swept out during
> function's return.
>
> I've though on pushing $0 onto the stack after or
> before such a function's arguments, so I could either
> store on it during this function's execution and then
> moving it to eax at it's end so it will be visible
>
> Any hints or comments?
>
> Thanks.
>
>
>
>
>
> ____________________________________________________________________________________
> Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
> http://answers.yahoo.com/dir/?link=list&sid=396545469
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: how to return a pointer to pointer
2007-10-06 16:45 ` Robert Plantz
@ 2007-10-08 16:36 ` Lope De Vega
2007-10-08 23:38 ` Robert Plantz
0 siblings, 1 reply; 4+ messages in thread
From: Lope De Vega @ 2007-10-08 16:36 UTC (permalink / raw)
To: Robert Plantz; +Cc: linux-assembly
--- Robert Plantz <plantz@sonoma.edu> wrote:
> >From "System V Application Binary Interface"
>
> Integral and pointer return values appear in
> %eax. A function that
> returns a struct or union value places the
> address of the result
> in %eax. Otherwise this is a scratch register.
>
> Pointers to anything, even other pointers, are
> returned in eax.
>
> Of course, the object it points to must not be in
> the stack frame, which
> as you point out, is deleted when the function
> returns. (I'm using
> "object" in the general sense here, not as in
> "object-oriented
> programming.")
>
> I try to design my code such that the function that
> creates and object
> is the only one that can delete it. That is, I try
> not to write a
> function that allocates memory, then returns a
> pointer to it. I allocate
> the memory in the calling function, then pass a
> pointer to this memory
> area to the function that will do something to the
> memory.
>
> Perhaps if you can provide more details about what
> you need to do we can
> give more advice.
>
> Bob
>
Hello, thanks.
Well, what I'm doing is a keyword-tree, where the
function I mentioned intern keys on it, and the reason
to return a pointer to a pointer is that I can then
use it for either intern and lookup-on-intern at the
same time (so keys doesn't get overwritten): if it
returns null, then key hasn't been interned, if it
doesn't return null but the nested pointer is null,
key exists, but no data has been attached (or was
removed from it), if both pointers come non-null this
key exists and is currently bound to something.
How I finally did was:
pushl $0
pushl key
pushl keyword-tree
call function
addl $8, %esp
Then I use the %esp slot where I pushed 0 from within
the function I call to return the value, which as you
said mentioned, needs to be copied to eax as well, so
after the bit above I did this:
cmpl $0, %eax /* key doesn't exist */
jz somewhere
cmpl $0, (%eax) /* non-zero means this key is already
bound */
jz somewhere_else
/* if we get here, key exists but but hasn't got any
data attached to it */
somewhere_else:
/* and if we get to here, key is bound already, but
still can do whatever we think appropiate, if any at
all */
addl $4, %esp /* so as to discard returned value,
which I didn't do in the last addl because %eax points
to it */
So that's what I've been up to, perhaps I'm in the
wrong approach, but it's the better I could think of.
Any comments are welcome anyway.
Thanks.
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: how to return a pointer to pointer
2007-10-08 16:36 ` Lope De Vega
@ 2007-10-08 23:38 ` Robert Plantz
0 siblings, 0 replies; 4+ messages in thread
From: Robert Plantz @ 2007-10-08 23:38 UTC (permalink / raw)
To: Lope De Vega; +Cc: linux-assembly
On Mon, 2007-10-08 at 09:36 -0700, Lope De Vega wrote:
> Hello, thanks.
>
> Well, what I'm doing is a keyword-tree, where the
> function I mentioned intern keys on it, and the reason
> to return a pointer to a pointer is that I can then
> use it for either intern and lookup-on-intern at the
> same time (so keys doesn't get overwritten): if it
> returns null, then key hasn't been interned, if it
> doesn't return null but the nested pointer is null,
> key exists, but no data has been attached (or was
> removed from it), if both pointers come non-null this
> key exists and is currently bound to something.
>
> How I finally did was:
>
> pushl $0
> pushl key
> pushl keyword-tree
> call function
> addl $8, %esp
>
> Then I use the %esp slot where I pushed 0 from within
> the function I call to return the value, which as you
> said mentioned, needs to be copied to eax as well, so
> after the bit above I did this:
>
> cmpl $0, %eax /* key doesn't exist */
> jz somewhere
>
> cmpl $0, (%eax) /* non-zero means this key is already
> bound */
> jz somewhere_else
>
> /* if we get here, key exists but but hasn't got any
> data attached to it */
>
> somewhere_else:
> /* and if we get to here, key is bound already, but
> still can do whatever we think appropiate, if any at
> all */
>
> addl $4, %esp /* so as to discard returned value,
> which I didn't do in the last addl because %eax points
> to it */
>
> So that's what I've been up to, perhaps I'm in the
> wrong approach, but it's the better I could think of.
> Any comments are welcome anyway.
>
> Thanks.
>
It's still not completely clear to me. I'm not sure what you
mean by "intern."
If I understand your code correctly, I would write something
like
I think the real question is if you wish to write assembly
language functions that obey C rules so that they can
be called by a C function. The function you wrote does
not follow C rules.
pushl $flag
pushl key
pushl keyword-tree
call function
addl $12, %esp
cmpl $0, %eax /* key doesn't exist */
jz someplace
movl flag, %eax /* get flag value */
cmpl $0, %eax /* any data attached? */
jz somewhere_else /* no */
/* if we get here, key is bound and has data attached. */
somewhere_else:
/* if we get here, key is bound but has no data attached. */
Further general notes:
(I am writing this for a 32-bit x86 platform. Other architectures
follow different rules, even the 64-bit x86.)
In C there are two ways to get inputs into a function:
1. Pass by value; a copy of the data is pushed onto the stack.
2. Pass by address; the address where the data is located is
pushed onto the stack. This is typically used when the data
object is large. For example, an array is usually large. Passing
an array by address is so common, the C syntax does not
even require you to use the "address-of" operator (&).
There are two ways to get ouputs from a function:
1. The return value, which is in the eax register.
2. Pass by address; the address where the data is to be stored is
pushed onto the stack.
One approach you could take is to write your function in C. Then
compile it with gcc using the -S option. (Notice that this is upper-
case S.) This will produce the assembly language file, foo.s from
the C file, foo.c. (Be careful that you do not use the same name
as the assembly language file you have already written. This
process would write over that file. I suggest doing this exercise in
a "temp" directory.) Then you can see how the compiler "writes"
assembly language. From there, you can write your own (better)
assembly language function.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-10-08 23:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-06 11:28 how to return a pointer to pointer Lope De Vega
2007-10-06 16:45 ` Robert Plantz
2007-10-08 16:36 ` Lope De Vega
2007-10-08 23:38 ` Robert Plantz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).