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

时间:2021-08-15 02:05:31
【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

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

.data
message: .string "hello!\n"
length = . - message

.text
.global _start

_start:
movl $length, %edx
movl $message, %ecx
movl $1, %ebx
movl $4, %eax
int $0x80

movl $0, %ebx
movl $1, %eax
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查看一下。

08048074 <_start>:
.text
.global _start

_start:
movl $length, %edx
8048074: ba 08 00 00 00 mov $0x8,%edx
movl $message, %ecx
8048079: b9 9c 90 04 08 mov $0x804909c,%ecx
movl $1, %ebx
804807e: bb 01 00 00 00 mov $0x1,%ebx
movl $4, %eax
8048083: b8 04 00 00 00 mov $0x4,%eax
int $0x80
8048088: cd 80 int $0x80

movl $0, %ebx
804808a: bb 00 00 00 00 mov $0x0,%ebx
movl $1, %eax
804808f: b8 01 00 00 00 mov $0x1,%eax
int $0x80
8048094: cd 80 int $0x80
ret
8048096: c3 ret