|
楼主 |
发表于 2007-10-30 11:14:55
|
显示全部楼层
再发一个:用汇编获取CPU ID,这回用asm编写库,c调用库
cpuid.asm
- extern _GLOBAL_OFFSET_TABLE_
- global get_cpuid:function
- %macro get_GOT 0
- call %%getgot
- %%getgot:
- pop ebx
- add ebx,_GLOBAL_OFFSET_TABLE_+$$-%%getgot wrt ..gotpc
- %endmacro
- [section .data]
- ver db "CPUID library version 0.0.1",0xa,0
- len equ $-ver
- [section .text]
- _start:
- get_GOT
- mov edx,len
- lea ecx,[ebx+ver wrt ..gotoff]
- mov ebx,1
- mov eax,4
- int 0x80
- mov ebx,0
- mov eax,1
- int 0x80
- get_cpuid:
- push edi
- mov edi,[esp+8]
- push ebx
- push ecx
- push edx
- xor eax,eax
- cpuid
- mov [edi],ebx
- mov [edi+4],edx
- mov [edi+8],ecx
- mov [edi+12],byte 0
- pop edx
- pop ecx
- pop ebx
- pop edi
- ret
复制代码
cpuid.h
- #ifndef _CPUID_H
- #define _CPUID_H 1
- void get_cpuid(char *buf);
- #endif
复制代码
- #include <stdio.h>
- #include <stdlib.h>
- #include "cpuid.h"
- int main(int argc, char *argv[])
- {
- char buf[16];
- get_cpuid(buf);
- printf("The processor vender is "%s"\n", buf);
- exit(EXIT_SUCCESS);
- }
复制代码
编译:
nasm -f elf cpuid.asm
ld -shared -o libcpuid.so cpuid.o
gcc -o demo demo.o -L. -lcpuid
运行:
./libcpuid.so
CPUID library version 0.0.1
env LD_LIBRARY_PATH=. ./demo
The processor vender is "GenuineIntel"
静态库:
nasm -f elf cpuid.asm
ar cr libcpuid.a cpuid.o
gcc -o demo demo.o libcpuid.a
./demo
The processor vender is "GenuineIntel" |
|