Page 1 of 1

Using CPUID with GCC

Posted: Fri Jan 11, 2008 1:39 pm
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?

Posted: Fri Jan 11, 2008 2:40 pm
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.. :)

Posted: Fri Jan 11, 2008 7:53 pm
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