* AMD 64 Problem with assembling
@ 2005-07-09 20:23 Florian Michel
2005-07-09 20:37 ` Mike Stump
2005-07-12 23:04 ` Michael Meissner
0 siblings, 2 replies; 3+ messages in thread
From: Florian Michel @ 2005-07-09 20:23 UTC (permalink / raw)
To: gcc
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!
Greetings,
Florian
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: AMD 64 Problem with assembling
2005-07-09 20:23 AMD 64 Problem with assembling Florian Michel
@ 2005-07-09 20:37 ` Mike Stump
2005-07-12 23:04 ` Michael Meissner
1 sibling, 0 replies; 3+ messages in thread
From: Mike Stump @ 2005-07-09 20:37 UTC (permalink / raw)
To: Florian Michel; +Cc: gcc
On Saturday, July 9, 2005, at 01:23 PM, Florian Michel wrote:
> I have a question concerning successfully assembling and linking the
> following assembly program on a linux AMD 64 machine:
Wrong list, this list isn't for help with how to program is assembly.
gcc-help would be more appropriate, though, that list is really for
help in how to program in a language compiled by gcc, .s files aren't
compiled.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: AMD 64 Problem with assembling
2005-07-09 20:23 AMD 64 Problem with assembling Florian Michel
2005-07-09 20:37 ` Mike Stump
@ 2005-07-12 23:04 ` Michael Meissner
1 sibling, 0 replies; 3+ messages in thread
From: Michael Meissner @ 2005-07-12 23:04 UTC (permalink / raw)
To: gcc
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-07-12 23:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-09 20:23 AMD 64 Problem with assembling Florian Michel
2005-07-09 20:37 ` Mike Stump
2005-07-12 23:04 ` Michael Meissner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).