C and asm macro 
Author Message
 C and asm macro

Is there any way to let a C routine call an assembly macro? Purpose is this:

int LastBit(unsigned __int64 I)

{    asm
    {    mov EAX, dword ptr [I]
         bsf EAX, EAX
         jnz done
         mov EAX, dword ptr [I+4]
         bsf EAX, EAX
         add EAX, 32
         done:
    }

Quote:
}

Now my C compiler allows inlined assembly, so the above works.

But I would like to get rid of the function call overhead. Problem: my C
compiler refuses this:

inline LastBit(unsigned __int64 I);

with the message: "functions containing asm are not expanded inline".




Thu, 31 Oct 2002 03:00:00 GMT  
 C and asm macro

Quote:

>Is there any way to let a C routine call an assembly macro? Purpose is this:

>int LastBit(unsigned __int64 I)

>{    asm
>    {    mov EAX, dword ptr [I]
>         bsf EAX, EAX
>         jnz done
>         mov EAX, dword ptr [I+4]
>         bsf EAX, EAX
>         add EAX, 32
>         done:
>    }
>}

>Now my C compiler allows inlined assembly, so the above works.

>But I would like to get rid of the function call overhead. Problem: my C
>compiler refuses this:

>inline LastBit(unsigned __int64 I);

>with the message: "functions containing asm are not expanded inline".

Perhaps you could use GCC instead, which does allow inlining of
functions containing asm.

Alternatively, why not write a C macro which produces inline asm?
Just specify the asm inputs and outputs as arguments to the macro,
and everything should work fine.  Here's simple example (for GCC):

/********************************/
#include <stdio.h>

#define RDTSC(u64)              \
    __asm__ __volatile__ (      \
        "rdtsc"                 \
        : "=A" (u64)            \
        : /* no inputs */       \
    );

int main(void)
{
    unsigned long long t;

    RDTSC(t);
    printf("%llu\n",t);
    return 0;

Quote:
}

/********************************/

Actually, looking again at your example code it would seem that
the syntax your compiler uses for inline asm makes it impossible
to use macros like this, except in very simple cases.  If this is
true, then you will either have to put up with the function call
overhead, or get a compiler with better support for inline asm.

S.

(Don't try running the above program unless RDTSC is actually
available, since it doesn't check before using it...)



Thu, 31 Oct 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. to CS: or not to CS: in F-PC assembler

2. A macro involving two sub-macros - where the 2nd macro needs results from the first

3. asm macro

4. FS: IBM Macro ASM V 2.0

5. Square Root in ASM (I'd also like a print literal Macro)

6. asm in c macro. how

7. ASM Macros ?

8. FS: IBM Macro ASM V 2.0

9. challenge for MACRO experts & ASM gurus

10. Macro Preprocessor (was: C -> ASM compression problem)

11. ASM Macro

12. macro -vs- macro/codeblock

 

 
Powered by phpBB® Forum Software