用汇编调用C库的时候,链接总是出错。先把汇编程序贴上来吧。
这是《Wrox Professional Assembly Language》中的程序:
1 #cpuid2.s Simple program to extract the processor Vendor ID
2 .section .data
3 output:
4 .asciz "The processor Vendor ID is '%s'\n"
5 .section .bss
6 .lcomm buffer, 12
7 .section .text
8 .globl _start
9 _start:
10 movl $0, %eax
11 cpuid
12 movl $buffer, %edi
13 movl %ebx, (%edi)
14 movl %edx, 4(%edi)
15 movl %ecx, 8(%edi)
16
17 pushl $buffer
18 pushl $output
19 call printf
20
21 addl $8, %esp
22 pushl $0
23 call exit
24
他用
$ as -o cpuid2.o cpuid2.s
$ ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
就可以产生可执行文件
由于我是64位机,所以加了两个参数
$ as --32 -o cpuid2.o cpuid2.s
$ ld -elf_i386 -dynamic-linker /lib32/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
但一直出现错误
ld: cannot find -lc
我修改了一下参数
$ as --32 -o cpuid2.o cpuid2.s
$ ld -m elf_i386 -dynamic-linker /lib32/ld-linux.so.2 /lib32/libc.so.6 -o cpuid2 cpuid2.o
终于成功了
$ ./cpuid2
The processor Vendor ID is 'GenuineIntel'