[lug] C++ inline asm on g++

Ian S. Nelson nelson_ at attglobal.net
Thu Jan 10 16:10:35 MST 2002


Can you just use a C++ function header and put the code inside there?  I
wrote a C compiler a while back and to avoid the different machine and OS
conventions of runtime zeros and start up we would use another C compiler
to wrap system calls and we did the same thing with main, that way you
don't have to worry about that junk anymore than you want to.

I've done most of my assembly with gcc/g++ as macros but I'll try to hack
something together on the fly here..  Foregive me if it doesn't compile.
(generally I write only perfect bug free code though, so it will probably
work..;)

So here is my favorite macro on an IA32 box...  I assume you understand the
GCC inline assembly syntax.   the "a" requests %eax and since it is after
the second ":" it's an input value.   The code then outbytes from the lower
part of %eax to port 0x80 to show up on my handy post code reader card.

#define post_code(post)   __asm__ __volatile__ ("outb %%al, $0x80" : : "a"
(post) );

I could just as easily write it like this:

void post_code_func(unsigned char post_value)
{
        __asm__ __volatile__ ("outb %%al, $0x80" : : "a" (post_value) );
}

or  if I wanted to do it C++ I could

void inline fooClass::post_code(unsigned char post_value)
{
        __asm__ __volatile__ ("outb %%al, $0x80" : : "a" (post_value) );
}

and it will let the compiler correctly mangle the names.

Ian


"D. Stimits" wrote:

> I'm going to be at the BLUG meeting this evening, maybe someone can give
> me some hints while there. I'm trying to find out what is needed for g++
> using recent g++ releases will need to add inline asm with C++ linkage?
> If wrapped extern "C", I know it won't be an issue, but if for example
> one wants to create asm based functions that are overloaded or within a
> separate namespace, what hoops must be jumped through? Is there asm name
> mangling that must be dealt with, or can g++ take care of it? My asm
> abilities are rather limited, maybe someone has a URL with samples of
> basic C++ linkage asm. Now if the asm requires some sort of name
> mangling scheme that g++ itself does not take care of, I assume one
> version is required for g++ 2.95 or below, another for redhat's 2.96,
> and yet one more for 3.x versions of g++? The name mangling scheme is
> apparently one of the reasons one can't mix binary object files across
> these compiler versions (among many reasons), I certainly hope it is
> possible to handle this without detailed knowledge of the actual
> mangling.
>
> D. Stimits, stimits at idcomm.com
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug




More information about the LUG mailing list