Inline Asmebly with C (Trying to use nasm syntax)

Inline Asmebly with C (Trying to use nasm syntax)

Post by Israel on Wed Jun 10, 2009 12:57 am
([msg=25172]see Inline Asmebly with C (Trying to use nasm syntax)[/msg])

Ok, some of you probably know what this code does, that's has nothing to do with this. I've been translating some AT&T syntax over to nasm with no problem. But suddenly with this code I've had a problem. Here is the C code with AT&T syntax:
Code: Select all
#include <stdio.h>
unsigned long get_sp(void){
   __asm__("movl %esp, %eax");
}
int main(){
   printf("Stack Pointer (ESP): 0x%x\n", get_sp());
}


That works fine. But when I try to put in as nasm syntax like this:
Code: Select all
#include <stdio.h>
unsigned long get_sp(void){
   __asm__("mov eax, esp");
}
int main(){
   printf("Stack Pointer (ESP): 0x%x\n", get_sp());
}


I get this error from the compiler:
Code: Select all
$ gcc -o get_nasm get_nasm.c
get_nasm.c: Assembler messages:
get_nasm.c:3: Error: too many memory references for `mov'


Does C not allow inline-assembly in nasm syntax? Or am I missing something in the code. I'm quite new to Assembly so I won't be surprised.
User avatar
Israel
Experienced User
Experienced User
 
Posts: 74
Joined: Thu Sep 18, 2008 9:53 pm
Blog: View Blog (0)


Re: Inline Asmebly with C (Trying to use nasm syntax)

Post by mischief on Wed Jun 10, 2009 2:04 am
([msg=25175]see Re: Inline Asmebly with C (Trying to use nasm syntax)[/msg])

gcc uses gas ('as' from binutils) as it's assembler, and gas takes AT&T syntax. read the manpage for more info :p
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: Inline Asmebly with C (Trying to use nasm syntax)

Post by tgoe on Wed Jun 10, 2009 10:56 am
([msg=25193]see Re: Inline Asmebly with C (Trying to use nasm syntax)[/msg])

Code: Select all
#include <stdio.h>
unsigned long get_sp(void){
   __asm__(".intel_syntax noprefix\n mov eax, esp");
}
int main(){
   printf("Stack Pointer (ESP): 0x%x\n", get_sp());
}


Code: Select all
$ gcc asm.c -masm=intel


Not sure if that is the right way to do things :? but it compiles and runs.
User avatar
tgoe
Contributor
Contributor
 
Posts: 664
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Inline Asmebly with C (Trying to use nasm syntax)

Post by Israel on Thu Jun 11, 2009 12:09 am
([msg=25235]see Re: Inline Asmebly with C (Trying to use nasm syntax)[/msg])

Works for me. But I had to use:
Code: Select all
gcc -o get_nasm get_nasm.c -masm=intel

Or I wouldn't get an executable :)

Thanks, I was about to give up and start going with AT&T.
User avatar
Israel
Experienced User
Experienced User
 
Posts: 74
Joined: Thu Sep 18, 2008 9:53 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests