Assembly in GCC

Does anyone know how to program assembly in GCC? Can you point me to a resource?

For some reason I just don't think it should be this hard.

In Visual Studio, it's butt-easy. You just prefix plain-old x86 assembly code with __asm, or put it in __asm {} blocks. Plain old easy to understand assembly, and you can even reference variable names:

mov eax, input

add eax, 1

etc..

So I'm programming linux drivers now, and I'm trying to figure out how to do assembly in GCC. Not supprisingly, it seems to be unnecessarily difficult. I've tried to teach myself by reading the linux kernel code (the linux answer to all documentation, gah) but I'm not getting anywhere.

They have this weird formatting for assembly. You can't just input x86 ASM, you have to do this weird printf type of thing:

__ASM__("mov eax, [%rd65]" "+r" "%r5")

blah something like that. I can't decipher how to do it, or how to reference code variables in the assembly.

I took linux device driver courses, and well of all things the instructor didn't even know how to do it.

And of course the man pages are devoid of any reference to this (the man pages are devoid of mostly enything you're looking for anyway).

The GCC manual also is devoid of any useful information.

And I've searched the interweb and come up with nothing.

Geez, it can't be this difficult to find some documentation on it. Lots of people are apparently doing assembly in GCC, they must have learned it from somewhere. WHERE????

Thanks,

JMT.
 
I don't know where you're seeing "__ASM__" but I don't see it in my Linux tree, and it's not (documented) GCC syntax.

There is a HOWTO for inline assembly with GCC, though it's a few years old so there may be some new bits that it doesn't document. For the assembly syntax itself (which is AT&T syntax rather than Intel, presumably for historical reasons), look here.
 
Thanks!

Yeah, it's all in there. Right from the page:

__asm__ ("movl %eax, %ebx\n\t"

"movl $56, %esi\n\t"

"movl %ecx, $label(%edx,%ebx,$4)\n\t"

"movb %ah, (%ebx)");

asm ("movl %1, %%eax;

movl %%eax, %0;"

:"=r"(b) /* output */

:"r"(a) /* input */

:"%eax" /* clobbered register */

);

OK, so it's lower-case __asm__, not uppercase, my bad.
 
GCC uses that syntax to be able to tell what operations you're performing (mainly, what has been clobbered) which enables the optimizer to work with the assembly code rather than assume the worst. Visual C++/Borland C++ etc. have an easier inline assembly syntax, but if you look at the output the surrounding code usually looks much worse as the compiler will have to assume that all registers are destroyed and potentially all memory locations have been touched and so on.
 
Back
Top