.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)