Page 1 of 1

Converting inline masm (C) to gcc inline asm?

Posted: Wed Apr 23, 2003 1:14 pm
by Jonathan
Hi!

I have a problem I need to convert the following inline masm code into gcc asm.

Code: Select all

int getFlags()
{
   int temp;

   __asm
   {
      mov eax, 0x01
      cpuid
      mov temp, edx
   }

   return temp;
}
I tried with this:

Code: Select all

int getFlags()
{
   int temp;

   asm volatile("mov 0x01,%eax" "\n\t"
             "cpuid" "\n\t"
             "mov %edx,temp");
   return temp;
}
But then I get an error "undefined refrence to 'temp'" when I am about to build it.

Re:Converting inline masm (C) to gcc inline asm?

Posted: Wed Apr 23, 2003 1:24 pm
by Tim
Do this:

Code: Select all

int getFlags()
{
   int temp;

   asm volatile("mov 0x01,%%eax" "\n\t"
             "cpuid" "\n\t"
             "mov %%edx,%0" : "=g" (temp));
   return temp;
}
For more information on what to put in "=g", run [tt]info gcc "c extensions" constraints[/tt].

Re:Converting inline masm (C) to gcc inline asm?

Posted: Wed Apr 23, 2003 1:54 pm
by Jonathan
Thx!

I have now tried it and I also tried with the letter r but I ran into some other problems!

The program I am trying to develop checks for sse2 mmx etc and it does this by this way.

Code: Select all

#include "stdio.h"

int getFlags()
{
  int temp;

  asm volatile("mov 0x01,%%eax" "\n\t"
            "cpuid" "\n\t"
            "mov %%edx,%0" : "=g" (temp));
  return temp;
}

int main()
{
   int flags = getFlags();

   int acpi =   (flags & 0x00400000) >> 22;
   int mmx =   (flags & 0x00800000) >> 23;
   int sse =   (flags & 0x03000000) >> 25;
   int sse2 =   (flags & 0x04000000) >> 26;

   printf("acpi: %d\nmmx: %d\nsse: %d\nsse2: %d\n", acpi, mmx, sse, sse2);

   return 0;
}
But when I try to run it I recieve the one deadly Segmentation fault!

Hmm what could possibly be wrong?

Re:Converting inline masm (C) to gcc inline asm?

Posted: Wed Apr 23, 2003 2:18 pm
by Tim
We missed another mistake:

Code: Select all

asm volatile("mov 0x01,%%eax" "\n\t"
            "cpuid" "\n\t"
            "mov %%edx,%0" : "=g" (temp));
Should be:

Code: Select all

asm volatile("mov $0x01,%%eax" "\n\t"
            "cpuid" "\n\t"
            "mov %%edx,%0" : "=g" (temp));
What you had before looks like this in Intel syntax:

Code: Select all

mov eax, [0x01]