Converting inline masm (C) to gcc inline asm?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Jonathan

Converting inline masm (C) to gcc inline asm?

Post 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.
Tim

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

Post 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].
Jonathan

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

Post 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?
Tim

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

Post 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]
Post Reply