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.
Jonathan
Post
by Jonathan » Wed Apr 23, 2003 1:14 pm
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
Post
by Tim » Wed Apr 23, 2003 1:24 pm
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
Post
by Jonathan » Wed Apr 23, 2003 1:54 pm
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
Post
by Tim » Wed Apr 23, 2003 2:18 pm
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: