?
.section .data#初始化的变量 output: .ascii "hello,world\n" #要打印的字符串,.data为初始化值的变量。output是标签,指示字符串开始的位置,ascii为数据类型 .section .bss#未初始化的变量,由0填充的缓冲区 .lcomm num,20 #lcomm为本地内存区域,即本地汇编外的不能进行访问。.comm是通用内存区域。 .section .text#汇编语言指令码 .globl _start#启动入口 _start: movl $4,%eax#调用的系统功能,4为write movl $output,%ecx#要打印的字符串 movl $1,%ebx#文件描述符,屏幕为1 movl $12,%edx#字符串长度 int $0x80#显示字符串hello,world movl $0,%eax movl $num,%edi movl $65,1(%edi)#A 的ascii movl $66,2(%edi)#B 的ascii movl $67,3(%edi)#C 的ascii movl $68,4(%edi)#D 的ascii movl $10,5(%edi)#\n的ascii movl $4,%eax#调用的系统功能,4为write movl $num,%ecx#要打印的字符串 movl $1,%ebx#文件描述符,屏幕为1 movl $6,%edx#字符串长度 int $0x80#显示字符串ABCD movl $1,%eax#1为退出 movl $0,%ebx#返回给shell的退出代码值 int $0x80#内核软中断,退出系统
效果及调试,?-gstabs用于生成调试信息
?
deepfuture-lx@deepfuture-lx-desktop:~/private/mytest$ as -gstabs -o hello.o hello.s
deepfuture-lx@deepfuture-lx-desktop:~/private/mytest$ ld -o hello hello.o
deepfuture-lx@deepfuture-lx-desktop:~/private/mytest$ ./hello
hello,world
ABCD
deepfuture-lx@deepfuture-lx-desktop:~/private/mytest$ gdb hello
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. ?Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/deepfuture-lx/private/mytest/hello...done.
(gdb) list
1 .section .data#初始化的变量
2 output:
3 ? .ascii "hello,world\n"
4 ? #要打印的字符串,.data为初始化值的变量。output是标签,指示字符串开始的位置,ascii为数据类型
5 .section .bss#未初始化的变量,由0填充的缓冲区
6 ? .lcomm num,20
7 ? #lcomm为本地内存区域,即本地汇编外的不能进行访问。.comm是通用内存区域。
8 .section .text#汇编语言指令码
9 ? .globl _start#启动入口
10 ? _start:
(gdb) run
Starting program: /home/deepfuture-lx/private/mytest/hello?
hello,world
ABCD
?
Program exited normally.
(gdb)?