zoukankan      html  css  js  c++  java
  • 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'
    "
    
    # 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'
    "
    
    .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编译,就方便了,因为它能自动链接必需的库

  • 相关阅读:
    django中的分页管理
    python 时间字符串和时间戳之间的转换
    随机生成英文字母,并且统计频数
    html 中a标签的问题(无反应,跳转,调用方法)
    oracle sid,instance_name,db_name,oracle_sid之间的关系
    备份
    修改yum源
    sql基本语法
    第二章 shell编程基础
    第一章 初识shell程序
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3315499.html
Copyright © 2011-2022 走看看