Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101870943
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: LINUX

2008-03-23 17:14:32

来源:赛迪网    作者:korn

第一个版本

x86-ubuntu-64位环境,at&t格式(gas汇编),使用linux系统调用:

#cpuid.s show extract the processor vendor ID
.section .data
output:
.ascii "the processor vendor ID is 'XXXXXXXXXXXX'\n"
.section .text
.globl main
main:
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
movl $0, %ebx
int $0x80

第二个版本

x86-ubuntu-64位环境,at&t格式(gas汇编),使用C库函数,注意使用了64位寄存器,想改成32位只要把所有带r的改成带e的就可以了

# cpuidinlib.s show extract the processor vendor ID using library
.section .data
output:
.asciz "the processor vendor ID is %s\n"

.section .bss
.lcomm buffer, 12

.section .text
.globl _start
_start:
movq $0, %rax
cpuid
movq $buffer, %rdi
movl %ebx, (%rdi)
movl %edx, 4(%rdi)
movl %ecx, 8(%rdi)

movq $output, %rdi
movq $buffer, %rsi
xorq %rax, %rax
call printf
movq $0, %rdi
xorq %rax, %rax
call exit
不过上面有一个问题
printf("%s", str);

这样的形式不常见,既然是字符串, str应该是以0结束的,但是注意上面并没有以0结束,那么printf是怎么判断字符串结束了呢? 在C下一样成功,不知道为什么,知道的告诉我下。

第三个 x86-win32,使用intel语法(masm32开发包), 调用windows API.这个估计都熟悉

;; cpuid
.686
.model flat, stdcall
option casemap: none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
includelib msvcrt.lib
sprintf PROTO C:DWORD, :DWORD, :VARARG

.data
msg db 'the processor vendor ID is %s', 0
note db '注意', 0
.data?
result db 13 dup(?)
buff db 128 dup(?)
.code
start:
call go
invoke ExitProcess,NULL
go:
mov eax, 0
cpuid
mov edi, offset result
mov [edi], ebx
mov 4[edi], edx
mov 8[edi], ecx
mov byte ptr 12[edi], 0

push dword ptr offset result
push dword ptr offset msg
push dword ptr offset buff
;call sprintf 
mov eax, 77c0f931H
call eax
add esp, 12

invoke MessageBox, NULL, addr buff, addr note, MB_OK
ret
end start
阅读(187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~