一个显示CPU厂商信息的小程序

$ cat cpuid.s
#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
mov     $0,     %ebx
int     $0x80

现在的情况如下:

$ as -o cpuid.o cpuid.s
$ ld -o cpuid cpuid.o
cpuid.o:fake:(.text+0xf):对‘ebx’未定义的引用
cpuid.o:fake:(.text+0x16):对‘edx’未定义的引用
cpuid.o:fake:(.text+0x1d):对‘ecx’未定义的引用
$

ebx/edx/ecx都是x386的标准寄存器,as无法识别它们,是否需要加载指令集文件或CPU型号参数?

找到问题了,ebx/edx/ecx应该使用"%"前缀而非"$",即:

movl    %ebx,   28(%edi)
movl    %edx,   32(%edi)
movl    %ecx,   36(%edi)

接下来程序可以正确编译和连接了, 但运行时发生错误:

$ ./cpuid
Segmentation fault

"Segmentation fault"意即"分段错误",太有挫折感了,去弄个“Hello world!”先。

你可能感兴趣的:(一个显示CPU厂商信息的小程序)