AMD 64 Problem with assembling

Michael Meissner gcc-mail@the-meissners.org
Tue Jul 12 23:04:00 GMT 2005


On Sat, Jul 09, 2005 at 10:23:14PM +0200, Florian Michel wrote:
> 
> Hello,
> 
> I have a question concerning successfully assembling and linking the following assembly program on a linux AMD 64 machine:
> 
> #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 main
> main:
>     movl $0, %eax
>     cpuid
>     movl $buffer, %edi
>     movl %ebx, (%edi)
>     movl %edx, 4(%edi)
>     movl %ecx, 8(%edi)
>     push $buffer
>     push $output
>     call printf
>     addl $8, %esp
>     push $0
>     call exit
> 
> This part of a book on assembly programming I am reading.
> 
> Compile and Link: gcc -o cpuid2 cpuid2.s
> When running cpuid2 it crashes with a segmentation fault.
> Which switches do I have to add to call gcc?
> 
> Thanks a lot!

If you are running on a Linux AMD 64 machine, the default compiler is 64 bit,
which uses a different calling sequence than the traditional 32-bit x86 world.
The above code however is 32 bit.  Use the -m32 option.

However, rather than write in assembler, you should use the asm extension to
get the asm code you need.  This works on both 32 and 64 bit modes:

	#include <stdio.h>

	int main(int argc, char *argv[])
	{
	  int edx;
	  int ebx;
	  int ecx;
	  union {
	    int i[4];
	    char c[16];
	  } u;

	  __asm__ ("cpuid"
		   : "=b" (ebx), "=d" (edx), "=c" (ecx)
		   : "a" (0));

	  u.i[0] = ebx;
	  u.i[1] = edx;
	  u.i[2] = ecx;
	  u.i[3] = 0;
	  printf ("The processor Vendor ID is '%s'\n", u.c);
	  return 0;
	}

-- 
Michael Meissner
email: gnu@the-meissners.org
http://www.the-meissners.org



More information about the Gcc mailing list