和NASM不同,GNU Assembly(简称gas)使用的是AT&T语法。
gas使用的汇编编译器在binutils里,可执行命令为as。
以下是nasm(intel风格)和gas(att风格)的helloworld版本:
;nasm intelsection .text ;section declaration;we must export the entry point to the ELF linker orglobal _start ;loader. They conventionally recognize _start as their;entry point. Use ld -e foo to override the default._start:;write our string to stdoutmov edx,len ;third argument: message lengthmov ecx,msg ;second argument: pointer to message to writemov ebx,1 ;first argument: file handle (stdout)mov eax,4 ;system call number (sys_write)int 0x80 ;call kernel;and exitmov ebx,0 ;first syscall argument: exit codemov eax,1 ;system call number (sys_exit)int 0x80 ;call kernelsection .data ;section declarationmsg db "Hello, world!",0xa ;our dear stringlen 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 stdoutmovl $len,%edx # third argument: message lengthmovl $msg,%ecx # second argument: pointer to message to writemovl $1,%ebx # first argument: file handle (stdout)movl $4,%eax # system call number (sys_write)int $0x80 # call kernel# and exitmovl $0,%ebx # first argument: exit codemovl $1,%eax # system call number (sys_exit)int $0x80 # call kernel.data # section declarationmsg:.ascii "Hello, world!\n" # our dear stringlen = . - 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
