和NASM不同,GNU Assembly(简称gas)使用的是AT&T语法。
gas使用的汇编编译器在binutils里,可执行命令为as。
以下是nasm(intel风格)和gas(att风格)的helloworld版本:
;nasm intel
section .text ;section declaration
;we must export the entry point to the ELF linker or
global _start ;loader. They conventionally recognize _start as their
;entry point. Use ld -e foo to override the default.
_start:
;write our string to stdout
mov edx,len ;third argument: message length
mov ecx,msg ;second argument: pointer to message to write
mov ebx,1 ;first argument: file handle (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
;and exit
mov ebx,0 ;first syscall argument: exit code
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data ;section declaration
msg db "Hello, world!",0xa ;our dear string
len equ $ - msg ;length of our dear string
#gas,att
.text # section declaration
# we must export the entry point to the ELF linker or
.global _start # loader. They conventionally recognize _start as their
# entry point. Use ld -e foo to override the default.
_start:
# write our string to stdout
movl $len,%edx # third argument: message length
movl $msg,%ecx # second argument: pointer to message to write
movl $1,%ebx # first argument: file handle (stdout)
movl $4,%eax # system call number (sys_write)
int $0x80 # call kernel
# and exit
movl $0,%ebx # first argument: exit code
movl $1,%eax # system call number (sys_exit)
int $0x80 # call kernel
.data # section declaration
msg:
.ascii "Hello, world!\n" # our dear string
len = . - msg # length of our dear string
gas支持intel语法
由于两种语法差异比较大,同时学习两种语法比较困难,如果可以在gas里编写intel语法风格的汇编代码最好不过。实际上gas自binutils 2.11之后也支持intel语法,具体参看:http://www.mirbsd.org/htman/i386/man7/gas-intel-howto.htm 和 http://www.study-area.org/cyril/opentools/opentools/x969.html
最基础的用法是在代码首行编写:
.intel_syntax noprefix