This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: inline assembler


On Wed, 5 Nov 2003 y2bismil@engmail.uwaterloo.ca wrote:

<snip>

> Is there a way in inline assembler to do 'real' functions?  As an example
> suppose I have the following:
> #define times3(arg1)__asm__ (...);  /*this results in arg1*3*/
>
> Would there be a way for me to say
> int x = times3(arg1);

Yes.

> My guess is no, but I thought I'd check anyways.

You *can*, but it is a gcc extension, not standard, I guess?

At least gcc allows this:

#define times3(arg1) \
({ int x; \
 __asm__ ( \
   "leal (%0,%0,2),%0" \
   : "=r" (x) \
   : "0" (arg1)); x; })

and then you can just say:

printf("%d\n", times3(4));

I.e. compound statements return a value, if it is enclosed in parenthesis,
and the return value has the type and value of the last statement, in this
case the return value is the value of x, and its type is int.

Kimmo


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]