和NASM不同,GNU Assembly(简称gas)使用的是AT&T语法。
gas使用的汇编编译器在binutils里,可执行命令为as。
以下是nasm(intel风格)和gas(att风格)的helloworld版本:

  1. ;nasm intel
  2. section .text ;section declaration
  3. ;we must export the entry point to the ELF linker or
  4. global _start ;loader. They conventionally recognize _start as their
  5. ;entry point. Use ld -e foo to override the default.
  6. _start:
  7. ;write our string to stdout
  8. mov edx,len ;third argument: message length
  9. mov ecx,msg ;second argument: pointer to message to write
  10. mov ebx,1 ;first argument: file handle (stdout)
  11. mov eax,4 ;system call number (sys_write)
  12. int 0x80 ;call kernel
  13. ;and exit
  14. mov ebx,0 ;first syscall argument: exit code
  15. mov eax,1 ;system call number (sys_exit)
  16. int 0x80 ;call kernel
  17. section .data ;section declaration
  18. msg db "Hello, world!",0xa ;our dear string
  19. len equ $ - msg ;length of our dear string
  1. #gas,att
  2. .text # section declaration
  3. # we must export the entry point to the ELF linker or
  4. .global _start # loader. They conventionally recognize _start as their
  5. # entry point. Use ld -e foo to override the default.
  6. _start:
  7. # write our string to stdout
  8. movl $len,%edx # third argument: message length
  9. movl $msg,%ecx # second argument: pointer to message to write
  10. movl $1,%ebx # first argument: file handle (stdout)
  11. movl $4,%eax # system call number (sys_write)
  12. int $0x80 # call kernel
  13. # and exit
  14. movl $0,%ebx # first argument: exit code
  15. movl $1,%eax # system call number (sys_exit)
  16. int $0x80 # call kernel
  17. .data # section declaration
  18. msg:
  19. .ascii "Hello, world!\n" # our dear string
  20. 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.htmhttp://www.study-area.org/cyril/opentools/opentools/x969.html
最基础的用法是在代码首行编写:

  1. .intel_syntax noprefix