tags:

views:

139

answers:

2

I know all the normal ways of doing this, what I'm looking for is an article I read a long long time ago that showed how to write some crazy helper functions using inline assembly and __naked functions to return a pair of items.

I've tried googling for it endlessly, but to no avail, I'm hoping someone else knows the article I'm talking about and has a link to it.

+8  A: 

No assembly necessary

struct PairOfItems {int item1;double item2;};
struct PairOfItems crazyhelperfunction(void){
    struct PairOfItems x = {42, -0.42};
    return x;
}

I don't know about the article.

pmg
Nice implementation of the supplied specs. :-)
Amardeep
+1 for `crazyhelperfunction`.
JoshD
I thought this was one of the normal ways of doing it!
Juan Mendes
LOL You caught me Juan. I *cheated*
pmg
I'm quite aware of how to return a struct, or an array, or just a pointer to some memory. I'm looking for that article.
ReaperUnreal
Yup, using a struct is about as good as it'll get in C. If you're using C++ you could use the "pair" type. At least pmg didn't recommend using void*. :D
GigaWatt
A: 

The __naked that I know of is

#define __naked __attribute__((naked))

Which is a GCC attribute that is documented here.

It is not supported on all targets. Doing a google code search will turn up some uses of it, though.

The reasons that it is done as a macro is so that it can be defined as empty for targets that don't support it or if the headers are being used with some other compiler.

I do remember (I think) seeing some examples of this for the division and modulation (divmod) functions for avr_gcc headers. This returned a struct that had both return values in it, but stored the whole thing in registers rather than on the stack.

I don't know if __naked had anything to do with the ability to return both parts of the result (which were in a struct) in registers (rather than on the stack), but it did allow the inline function to consist entirely of a call to one helper function.

nategoose