Inline assembly in the GNU compiler

Programming, for all ages and all languages.
Post Reply
lpcstr
Posts: 11
Joined: Sat Jun 25, 2011 7:11 pm

Inline assembly in the GNU compiler

Post by lpcstr »

I've been using GCC pretty much exclusively for the last several months, and I really like it compared to MSVC, but there is one aspect of GCC that I cant stand and luckily have not had to use too much, until now, and that is inline ASM.

First off, I can't stand AT&T syntax, which is the default. I refuse to even look at the stuff, I'll run it through a conversion program if I have to. That by itself isn't too much of a problem since GCC does have a -masm=intel option, but the other problem I have is how you inline the code. It's extremely clumsy and confusing for me. In MSVC, it's so easy and clean there is no contest.

Code: Select all

int FuncAdd(int a, int b)
{
    _asm
    {
        mov eax, a
        add eax, b
    }
}
It doesn't get any simpler or cleaner than that, at least not from my point of view. I realise GCC has to support more architectures than I could even name off the top of my head, and this is probably why they chose to do it the way they did, but I need something that works for me, and I don't target hardware other than x86/AMD64. I've read that Apples' GCC has a option -fasm-blocks which allows you to use the same style asm syntax as MSVC. Is there some sort of identical extension I could use with GCC?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Inline assembly in the GNU compiler

Post by NickJohnson »

In general, inline assembly is a bad idea. It's a compiler-specific extension (as you have seen) and is therefore even less portable than assembly. I would highly recommend using an external assembler (which could also be more easily set to use Intel syntax) and then linking the assembler-generated object files with the compiler-generated ones. If you're using just C, this should be quite straightforward, and even if you're using C++, the name mangling is not that hard to deal with. You should also make sure to read up on whichever C calling convention (most likely cdecl) you're using.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Inline assembly in the GNU compiler

Post by Brynet-Inc »

I'm not sure how this obvious solution escapes most opponents of inline assembler, oh right, they're also opposed of learning.. that was stupid, I feel very silly now.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
lpcstr
Posts: 11
Joined: Sat Jun 25, 2011 7:11 pm

Re: Inline assembly in the GNU compiler

Post by lpcstr »

NickJohnson wrote:In general, inline assembly is a bad idea. It's a compiler-specific extension (as you have seen) and is therefore even less portable than assembly. I would highly recommend using an external assembler (which could also be more easily set to use Intel syntax) and then linking the assembler-generated object files with the compiler-generated ones. If you're using just C, this should be quite straightforward, and even if you're using C++, the name mangling is not that hard to deal with. You should also make sure to read up on whichever C calling convention (most likely cdecl) you're using.
You're right. I haven't had much experience with the GCC assembler, but it seems pretty easy to work with compared to the inline assembly.

Code: Select all

.intel_syntax noprefix

.globl FuncAdd

FuncAdd:
	push	ebp
	mov		ebp,	esp
	mov		eax,	DWORD [ebp+4]
	add		eax,	DWORD [ebp+8]
	pop		ebp
	ret
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Inline assembly in the GNU compiler

Post by gerryg400 »

Code: Select all

int FuncAdd(int a, int b)
{
    _asm
    {
        mov eax, a
        add eax, b
    }
}
If you do this to the Microsoft compiler does it know that you've modified the eax register ? The 'complexity' with inline gcc assembler is generally about telling the compiler what you're doing with its registers in a way that allows it to continue with its optimising. This type of feature requires that you learn the syntax. IMHO, it's a small price to pay.

In general I agree with NickJohnson tho, and keep inline assembler to simple things like writing to ports and modifying special purpose registers. I try never to tell the compiler what to do with the eax register.
If a trainstation is where trains stop, what is a workstation ?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Inline assembly in the GNU compiler

Post by NickJohnson »

berkus wrote:Inline assembly is not hard at all, all you need is just READ a single page in gcc manual. I know it's too hard for most coders, to READ.
I never said that it was hard, just that it was nonstandard and unnecessary (in the vast majority of cases.) I would make a similar argument against nested functions, even though they're very straightforward to use. A little -pedantic can't hurt.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Inline assembly in the GNU compiler

Post by JamesM »

gerryg400 wrote:

Code: Select all

int FuncAdd(int a, int b)
{
    _asm
    {
        mov eax, a
        add eax, b
    }
}
If you do this to the Microsoft compiler does it know that you've modified the eax register ? The 'complexity' with inline gcc assembler is generally about telling the compiler what you're doing with its registers in a way that allows it to continue with its optimising. This type of feature requires that you learn the syntax. IMHO, it's a small price to pay.

In general I agree with NickJohnson tho, and keep inline assembler to simple things like writing to ports and modifying special purpose registers. I try never to tell the compiler what to do with the eax register.
Yes, it does.

GCC style inline assembler is pointlessly complicated - ARM's compiler does it in the same (far superior) way as MSVC, by allowing any C expression as a register or memory location.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Inline assembly in the GNU compiler

Post by Owen »

JamesM wrote:
gerryg400 wrote:

Code: Select all

int FuncAdd(int a, int b)
{
    _asm
    {
        mov eax, a
        add eax, b
    }
}
If you do this to the Microsoft compiler does it know that you've modified the eax register ? The 'complexity' with inline gcc assembler is generally about telling the compiler what you're doing with its registers in a way that allows it to continue with its optimising. This type of feature requires that you learn the syntax. IMHO, it's a small price to pay.

In general I agree with NickJohnson tho, and keep inline assembler to simple things like writing to ports and modifying special purpose registers. I try never to tell the compiler what to do with the eax register.
Yes, it does.

GCC style inline assembler is pointlessly complicated - ARM's compiler does it in the same (far superior) way as MSVC, by allowing any C expression as a register or memory location.
If my memory is correct, MSVC only allows variables to be used as memory operands. In ARMCC, __asm { mov r0, #0 } doesn't guarantee that r0 is actually r0; ARMCC's inline assembler is a high level assembler, and r0-r12 are actually implicitly defined variables. I'd say ARMCC's embedded assembler is a closer analogue of GCC inline assembly in some regards - but embedded assembler is never inlined.

This all said: There is not real need for the complexity of GCC inline assembler - if the compiler parsed the assembler source and understood the applicable constraints
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Inline assembly in the GNU compiler

Post by Love4Boobies »

NickJohnson wrote:
berkus wrote:Inline assembly is not hard at all, all you need is just READ a single page in gcc manual. I know it's too hard for most coders, to READ.
I never said that it was hard, just that it was nonstandard and unnecessary (in the vast majority of cases.) I would make a similar argument against nested functions, even though they're very straightforward to use. A little -pedantic can't hurt.
The advantage to using inline assembly is that you don't need to know what ABI the compiler uses. Although using a different compiler is probably out of the question (as almost none of them use the asm keyword similarly), compilers may be configurable. With external assembly, you need different object files for every compiler setting.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Inline assembly in the GNU compiler

Post by Owen »

Another advantage: Let me see you write something like this without inline assembler:

Code: Select all

template<typename T> 
inline T atomicSwap(volatile T& lhs, const T& rhs)
{
    __asm__ ("xchg %[lhs], %[rhs]" : [lhs] "+m" (lhs), [rhs] "+r" (rhs));
    return rhs;
}
Post Reply