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编译,就方便了,因为它能自动链接必需的库

 

转载于:https://www.cnblogs.com/suncoolcat/p/3315499.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值