Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3843203
  • 博文数量: 146
  • 博客积分: 3918
  • 博客等级: 少校
  • 技术积分: 8584
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-17 13:52
个人简介

个人微薄: weibo.com/manuscola

文章分类

全部博文(146)

文章存档

2016年(3)

2015年(2)

2014年(5)

2013年(42)

2012年(31)

2011年(58)

2010年(5)

分类: LINUX

2011-09-03 01:13:23

      最新学习代码优化,看了很多反编译的汇编代码,意识到自己的汇编水平比较烂,重新学习下汇

编编程。各位看官看我第一个例子就知道我学习用的书是《Professional Assembly Language》。 这本

书通俗易懂,我很喜欢。


    cpuid 指令可以获取cpu的一些信息,对于这个指令比较感兴趣的可以查看intel的Manual。下面这段

代码完全来自 Richard Blum大牛的book,(不知道算不算侵犯版权,我估计他不会和我这个对他无限

仰望的菜鸟计较。)


    解释下代码,cpuid指令有很多中,DOS编程技术有详细的介绍,根据eax中的数值不同,cpuid指令

返回不同的信息。当eax中的值为0的时候,cpuid指令获取的是 Vendor ID。这条指令执行完毕后:



EAX  -------------要求基本信息时,eax功能码的最大值。 换句话说,如果eax                   的值大于了最大

值,cpuid不能识别这条指令,无法返回信息


EBX EDX ECX ------返回Vendor ID的字符表示

    看下下面的调试结果: 可以看到 eax中保存的值为11,表示最大功能码是11,如果将大于11的数字
存入eax,然后调用cpuid,则不能识别。


  1. (gdb) info reg
  2. eax 0xb    11
  3. ecx 0x6c65746e    1818588270
  4. edx 0x49656e69    1231384169
  5. ebx 0x756e6547    1970169159
  6. esp 0xbffff7e0    0xbffff7e0
  7. ebp 0x0    0x0
  8. esi 0x0    0
  9. edi 0x0    0
  10. eip 0x804807c    0x804807c <_start+8>
  11. eflags 0x200212    [ AF IF ID ]
  12. cs 0x73    115
  13. ss 0x7b    123
  14. ds 0x7b    123
  15. es 0x7b    123
  16. fs 0x0    0
  17. gs 0x0    0
    
    下面讲述生成可执行文件的命令:执行完后,可以看到生成了可执行文件cpuid。

  1. as -o cpuid.o cpuid.s
  2. ld -o cpuid   cpuid.o

    执行cpuid可以看到输出。
  1. root@libin:~/program/assembly# ./cpuid
  2. the processor vendor ID is 'GenuineIntel'
 

    另外一种生成可执行文件的方法是使用gcc。由于gcc需要寻找main,所以汇编代码中的_start 需用
main来代替。

     

    我们知道,C程序如果生成debug版本,需要加上-g选项,汇编同样道理,只需要执行,下面命令,

即可生成debug版本的可执行程序。


    然后我们就可以像调试C程序一样调试汇编程序了。     
  1. as -gstabs -o cpuid.o cpuid.s
  2. ld -o cpuid cpuid.o

  1. # cpuid.s extract the the processor vendor ID
  2. .section .data
  3. output:
  4. .ascii "the processor vendor ID is 'xxxxxxxxxxxx'\n"

  5. .section .text
  6. .global _start
  7. _start:
  8.      nop
  9.      movl $0,%eax
  10.      cpuid
  11.      movl $output, %edi
  12.      movl %ebx,28(%edi)
  13.      movl %edx,32(%edi)
  14.      movl %ecx,36(%edi)
  15.      movl $4,%eax
  16.      movl $1,%ebx
  17.      movl $output,%ecx
  18.      movl $42,%edx
  19.      int $0x80
  20.      mov $1,%eax
  21.      mov $0,%ebx
  22.      int $0x80
 参考文献 :
 

1 Richard Blum : Professional Assembly Language



阅读(7086) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~