LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   what's wrong with my inline assembly program (https://www.linuxquestions.org/questions/programming-9/whats-wrong-with-my-inline-assembly-program-868406/)

roMoon 03-14-2011 12:41 AM

what's wrong with my inline assembly program
 
hi all, I am trying to use inline assembly code in my program, but the mov instruction does not work.
my code:

Quote:

#include "stdio.h"
int main( int argc, char* argv[])
{
int cpu_iid;

__asm__ __volatile__(
"mov %0, 1\n"
:"=m"(cpu_iid)
:
);
}
and the error:
Quote:

$gcc test.c -o a.out
test.c: Assembler messages:
test.c:6: Error: too many memory references for `mov'
Thank you for your help.

Sylvester Ink 03-14-2011 02:26 AM

What assembly language are you using? X86?

[EDIT]Here, read this:
http://www.ibiblio.org/gferg/ldp/GCC...-HOWTO.html#s2
Seems to me you're missing a \t

roMoon 03-15-2011 04:32 AM

Quote:

Originally Posted by Sylvester Ink (Post 4289945)
What assembly language are you using? X86?

[EDIT]Here, read this:
http://www.ibiblio.org/gferg/ldp/GCC...-HOWTO.html#s2
Seems to me you're missing a \t

thank you Sylvester,
I have fix it with this:
Code:

#define CPU_ID(cpu_iid)                  \
      __asm__ __volatile__ (            \
      "mov $0, %0\n"                    \
      :"=r"(cpu_iid)                    \
      :                                  \
      );

but when I try to use memory another fault occurs
Code:

#define CPU_ID_R(cpu_iid)                \
      __asm__ __volatile__ (            \
      "mov $0, %%eax\n\t"                \
      "mov %%eax, %0\n\t"                \
      :"=m"(cpu_iid)                    \
      :                                  \
      :"%eax"                            \
      );

error: unknown register name '%eax' in 'asm'


Sylvester Ink 03-15-2011 11:30 PM

I haven't used inline assembly in some time, but if I recall correctly, #defining stuff like that isn't such a hot idea, as preprocessor directives bypass the error and syntax checking of the compiler. All it's really doing is copy-pasting the macro into the locations where it's being called. Probably safer to write it into its own method. As for the error itself, I'd take a closer look at your syntax. Try using the Intel style. The code is cleaner and it's easier to find errors. See the following link for an example:
http://www.cplusplus.com/forum/beginner/3280/

Beyond that, I couldn't say.
Hopefully there's someone else better versed in assembly here. Otherwise, I'd ask on the cplusplus.com forums, or one of the many assembly forums out there . . .

paulsm4 03-15-2011 11:40 PM

In your other thread, you suggested you were compiling for an Alpha.

Q: (also asked above):
Quote:

What assembly language are you using? X86?


All times are GMT -5. The time now is 06:24 AM.