AT&T汇编试讲--获取CPU Vendor ID

纯汇编代码如下:

 

# a test program to get the processor vendor id

# data segment
.section .data
output:
	.ascii "The processor Vendor id is 'xxxxxxxxxxxx'\n"

# code segment
.section .text
.global _start
_start:
	movl $0,%eax
	cpuid
	movl $output,%edi	# 将output位置加载进edi
	movl %ebx,28(%edi)	# 将结果最低4字节,即ebx中的值放到edi+28内存位置处
	movl %edx,32(%edi)	# 结果中间4字节
	movl %ecx,36(%edi)	# 结果最高4字节

# 接下来显示信息
	movl $4,%eax	# 内核执行的显示函数代号
	movl $1,%ebx	# 要写入的文件描述符
	movl $output,%ecx # 字符串的初始位置
	movl $42,%edx	  # 字符串的长度
	int $0x80		  # 软件中断,执行系统调用
	
# 退出程序
	movl $1,%eax	 # 退出代码
	movl $0,%ebx
	int $0x80


 

如果要用gcc编译的话,注意程序的起始处标签就不能是_start了,要改为main


反汇编cpuid.o:


在汇编中调用C函数:

 

.section .data
output:
# printf函数要求以空字符结尾的字符串,所以为asciz
	.asciz "The processor vendor id is '%s'\n"

.section .bss
# 12字节的缓冲区域
	.lcomm buf,12

.section .text
.global _start
_start:
	movl $0,%eax
	cpuid
	movl $buf,%edi
	movl %ebx,(%edi)
	movl %edx,4(%edi)
	movl %ecx,8(%edi)
# 给printf传参数
	pushl $buf
	pushl $output
	call printf

	addl $8,%esp
# 给exit传递参数
	pushl $0
	call exit


编译cpuid2.s时,注意下图情况:

 


-lc:表明要动态链接/lib/libc.so; -dynamic-linker:使用动态加载器查找libc.so

如果用gcc编译,就方便了,因为它能自动链接必需的库

 

posted on 2013-09-11 20:25  新一  阅读(680)  评论(0编辑  收藏  举报

导航