版本1:
#cpuid.s Sample program to extract the processor Vendor ID .section .data output: .ascii "The processor Vendor ID is 'xxxxxxxxxxxxxxxxx'\n" .section .text .global _start _start: mov $0,%eax cpuid mov $output,%edi mov %ebx,28(%edi) mov %edx,32(%edi) mov %ecx,36(%edi) mov $4,%eax mov $1,%ebx mov $output,%ecx mov $42,%edx int $0x80 mov $1,%eax mov $0,%ebx int $0x80
使用as编译,ld连接:
as -o cpuid.o cpuid.s ld -o cpuid cpuid.o ./cpuid
使用GCC进行汇编,需要修改脚本:将start换成main
#cpuid.s Sample program to extract the processor Vendor ID .section .data output: .ascii "The processor Vendor ID is 'xxxxxxxxxxxxxxxxx'\n" .section .text .global main main: mov $0,%eax cpuid mov $output,%edi mov %ebx,28(%edi) mov %edx,32(%edi) mov %ecx,36(%edi) mov $4,%eax mov $1,%ebx mov $output,%ecx mov $42,%edx int $0x80 mov $1,%eax mov $0,%ebx int $0x80
使用gcc汇编:
gcc -o cpuid cpuid2.s
但是会报错,错误信息如下:relocation R_X86_64_32 against `.data' can not be used when making a PIE object; recompile with -fPIE
解决方法是:增加-no-pie选项
gcc -o cpuid cpuid2.s -no-pie
连接C库函数
#cpuid.s View the CPUID Vendor ID string using C library calls .section .data output: .asciz "The processor Vendor ID is '%s'\n" .section .bss .lcomm buffer,12 .section .text .global _start _start: mov $0,%eax cpuid mov $buffer,%edi mov %ebx,(%edi) mov %edx,4(%edi) mov %ecx,8(%edi) push $buffer push $output call printf add $8,%esp push $0 call exit
编译:
as -o cpuid3.o cpuid3.s
连接:
ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid3 -lc cpuid3.o
执行报错,现阶段还不是太熟悉汇编语言,留待后续再解决