linux下的C语言开发(AT&T 汇编语言) 04

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

    同样是x86的cpu,但是却可以用不同形式的汇编语言来表示。在window上面我们使用的更多是intel格式的汇编语言,而在Linux系统上面使 用的更多的常常是AT&T格式的汇编语言。那什么是AT&T格式的汇编代码呢?我们可以写一个试试看。

  1. .data  
  2.     message: .string "hello!\n"  
  3.     length = . - message  
  4.   
  5. .text  
  6. .global _start  
  7.   
  8. _start:  
  9.     movl $length, %edx  
  10.     movl $message, %ecx  
  11.     movl $1, %ebx  
  12.     movl $4, %eax  
  13.     int $0x80  
  14.   
  15.     movl $0, %ebx  
  16.     movl $1, %eax  
  17.     int $0x80  

    这是一个简单的汇编文件,我们可以分两步进行编译。首先,输入 as -gstabs -o hello.o hello.s, 接着输入ld -o hello hello.o即可。为了验证执行文件是否正确,可以输入./hello验证一下。

    在as命令当中,由于我们使用了-gstabs选项,因此在hello执行文件中是包含调试信息的。所以,如果想单步调试的朋友可以输入gdb hello进行调试。

    那么,hello执行文件反汇编的代码又是什么样的呢?我们可以输入objdump -S -d hello查看一下。

  1. 08048074 <_start>:  
  2. .text  
  3. .global _start  
  4.   
  5. _start:  
  6.         movl $length, %edx  
  7.  8048074:       ba 08 00 00 00          mov    $0x8,%edx  
  8.         movl $message, %ecx  
  9.  8048079:       b9 9c 90 04 08          mov    $0x804909c,%ecx  
  10.         movl $1, %ebx  
  11.  804807e:       bb 01 00 00 00          mov    $0x1,%ebx  
  12.         movl $4, %eax  
  13.  8048083:       b8 04 00 00 00          mov    $0x4,%eax  
  14.         int $0x80  
  15.  8048088:       cd 80                   int    $0x80  
  16.   
  17.         movl $0, %ebx  
  18.  804808a:       bb 00 00 00 00          mov    $0x0,%ebx  
  19.         movl $1, %eax  
  20.  804808f:       b8 01 00 00 00          mov    $0x1,%eax  
  21.         int $0x80  
  22.  8048094:       cd 80                   int    $0x80  
  23.         ret  
  24.  8048096:       c3                      ret  
  25.     
posted @ 2012-01-18 12:17  董雨  阅读(166)  评论(0编辑  收藏  举报