Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9963
  • 博文数量: 2
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-26 16:16
文章分类
文章存档

2011年(2)

我的朋友
最近访客

分类: Mysql/postgreSQL

2011-12-26 16:19:20

最新学习代码优化,看了很多反编译的汇编代码,意识到自己的汇编水平比较烂,重新学习下汇编编程。各位看官看我第一个例子就知道我学习用的书是《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
阅读(3179) | 评论(3) | 转发(1) |
0

上一篇:system函数返回值探究

下一篇:没有了

给主人留下些什么吧!~~

娃哈哈8752011-12-29 09:34:40

入门啊入门啊,还是看不懂

Bean_lee2011-12-27 23:13:07

呵呵兄弟,麻烦加个转载二字,尊重下博主的劳动成果。
希望兄弟多出原创好文章,互相学习,互相提高。

Heartwork2011-12-27 12:58:55

哥们,你抄别人的blog了……