c compiler inline Intel notation assembly

Programming, for all ages and all languages.
Post Reply
Gavin
Posts: 23
Joined: Tue Mar 11, 2008 9:53 pm

c compiler inline Intel notation assembly

Post by Gavin »

What are my options ?
Has to comply with ANSI C and if possible be free and isn't likely to die out in the future.
Thanks alot.
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Post by Zenith »

The phrase "c compiler inline Intel notation assembly" is pretty vague :shock:, but I'm assuming you mean a C compiler with intel syntax for inline assembly.

gcc supports "-masm=intel", but i'm not sure if that also applies to the actual gnu assembler or if you have to supply your own intel-compatible one. IIRC the microsoft c compiler(s) also use intel as their standard assembly syntax.

And of course, it's always possible to just learn the more 'portable' AT&T syntax :wink:
"Sufficiently advanced stupidity is indistinguishable from malice."
Gavin
Posts: 23
Joined: Tue Mar 11, 2008 9:53 pm

Post by Gavin »

C compiler with intel syntax for inline assembly is correct.
I've used nasm alot so i would like to stick to the intel notation if possible.
The microsoft c compiler is not as portable for amd cpu's is it?

At&t is for unix and linux right?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Hi,

The GNU compiler collection (GCC) is the de facto compiler set for any unix system. It uses GAS, the GNU assembler, which uses AT&T syntax natively but can be configured via assembler directives to use Intel syntax.

Several people here do this, and it going something like this:

Code: Select all

#define ASM(x) do {asm volatile(".syntax intel"); asm volatile(x); asm volatile(".syntax att");} while (0)
Hope this helps,

James
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

The GNU Assembler is capable of Intel syntax.

Silly example - Works on OpenBSD(With gcc -O0..):

Code: Select all

#include <stdio.h>

int main(void) {
	int local_var;
	
	asm(".intel_syntax noprefix");
	
	asm("mov dword ptr [ebp-28], 666");
	
	asm(".att_syntax");
	
	printf("%d", local_var);

	return 0;
}
This may help you: http://www.mirbsd.org/htman/i386/man7/g ... -howto.htm

It would probably be worth while just to learn AT&T syntax, it's a skill worth having. ;)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Gavin
Posts: 23
Joined: Tue Mar 11, 2008 9:53 pm

Post by Gavin »

Thanks for the detailed replies .
I work on windows and use nasm alot i've only started to learn c.

If i use that define in my c source files thats where i use it right?Will that cause any problems i might not foresee?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

It shouldn't cause any problems - I got the preamble and epilogue slightly wrong though (the .syntax intel bit should be .intel_syntax noprefix, as per Brynet's post).
Gavin
Posts: 23
Joined: Tue Mar 11, 2008 9:53 pm

Post by Gavin »

I'll go ahead and use the gcc compiler it would be silly to use something else by the looks of it.
I haven't the fogiest what preamble and epilogue is, but the words sound lovely :wink:
But thanks . :D
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post by lukem95 »

Gavin wrote:I'll go ahead and use the gcc compiler it would be silly to use something else by the looks of it.
I haven't the fogiest what preamble and epilogue is, but the words sound lovely :wink:
But thanks . :D
"but the works sound lovely" hahahah :D

if they had ratings on this forum, you my friend would get 6 out of 5!

[/OT]
~ Lukem95 [ Cake ]
Release: 0.08b
Image
Gavin
Posts: 23
Joined: Tue Mar 11, 2008 9:53 pm

Post by Gavin »

Haha Luke :D

I installed cygwin and gcc and of course nasm i'm ready to get cracking on my mbr :)
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

Brynet-Inc wrote:

Code: Select all

#include <stdio.h>

int main(void) {
	int local_var;
	
	asm(".intel_syntax noprefix");
	
	asm("mov dword ptr [ebp-28], 666");
	
	asm(".att_syntax");
	
	printf("%d", local_var);

	return 0;
}
For a very funny result forget the ".att_syntax". I did it once :).


JAL
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: c compiler inline Intel notation assembly

Post by Solar »

Gavin wrote:Has to comply with ANSI C...
You most likely mean that the compiler should comply with ANSI C unless you do inline assembly. Nevertheless I'd like to point out that inline assembly never "complies with ANSI C", as ANSI C does not support that technique - it's a compiler-specific extension.
Every good solution is obvious once you've found it.
zerosum
Member
Member
Posts: 63
Joined: Wed Apr 09, 2008 6:57 pm

Post by zerosum »

This is quite neat; I did not know you could do this.

AT&T syntax is ugly as hell (imho) and I don't understand how to do many simple things in it. I think I'll be using this to do inline asm in the sensible syntax, in future ;-)

Thanks people :-)
Post Reply