Using CPUID with GCC

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
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Using CPUID with GCC

Post by AlfaOmega08 »

I'm trying to read the information of the CPU by the CPUID instruction. With Dev C++ I've wrote this code:

int rEBX, rECX, rEDX;

asm {
MOV EAX, 0
CPUID
MOV DWORD PTR rEBX, EBX
MOV DWORD PTR rECX, ECX
MOV DWORD PTR rEDX, EDX
}

How can I write this in AT&T syntax for gcc?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Some time ago, I posted my own CPUID implementation on the forums.. you're free to take a look at it.

But as for your question:

Code: Select all

#define cpuid(in, a, b, c, d) \
	__asm__ __volatile__("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in));
You may wish to expand that into a function though... whichever makes it easier.

The basic syntax is:

Code: Select all

__asm__ ("instruction"
			: outputs
			: inputs
			: clobbered registers
			);
Hope that helps.. :)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
binutils
Member
Member
Posts: 214
Joined: Thu Apr 05, 2007 6:07 am

Post by binutils »

http://www.webservertalk.com/archive107 ... 23775.html
Richard Blum wrote:

Code: Select all

#cpuid2.s View the CPUID Vendor ID string using C library calls
.section .datatext
output:
    .asciz "The processor Vendor ID is '%s'\n"
.section .bss
    .lcomm buffer, 12
.section .text
.globl _start
_start:
    movl $0, %eax
    cpuid
    movl $buffer, %edi
    movl %ebx, (%edi)
    movl %edx, 4(%edi)
    movl %ecx, 8(%edi)
    pushl $buffer
    pushl $output
    call printf
    addl $8, %esp
    pushl $0
    call exit
or

Code: Select all

#cpuid.s Sample program to extract the processor Vendor ID
.section .data
output:
   .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
.section .text
.globl _start
_start:
   movl $0, %eax
   cpuid
   movl $output, %edi
   movl %ebx, 28(%edi)
   movl %edx, 32(%edi)
   movl %ecx, 36(%edi)
   movl $4, %eax
   movl $1, %ebx
   movl $output, %ecx
   movl $42, %edx
   int $0x80
   movl $1, %eax
   movl $0, %ebx
   int $0x80
Post Reply