Skip to content

GNU Assembler

The GNU Assembler, commonly known as gas or simply as, its executable name, is the assembler used by the GNU Project. It is the default back-end of GCC. It is used to assemble the GNU operating system and the Linux kernel, and various other software. It is a part of the GNU Binutils package.

The GAS executable is named as, the standard name for a Unix assembler. GAS is cross-platform, and both runs on and assembles for a number of different computer architectures. Released under the GNU General Public License v3, GAS is free software.

Hello, world

.data                           # 섹션 .data 가 여기서부터 시작한다.

msg:
    .string "Hello, world!\n"   # 우리의 사랑스런 문자열
    len = . - msg               # 친애하는 문자열의 길이

.text                           # 섹션 .text 가 여기서부터 시작한다.

                    # ELF 링커나 로더에게 프로그램의 엔트리 포인트를 알려주어야
    .global _start  # 한다. 로더 혹은 링커는 일반적으로 엔트리 포인트가 _start 
                    # 라고 가정하고 있다. 이 디폴트 설정을 바꿀려면,
                    # ld -e foo 를 사용하라.

_start:

# stdout 에 우리의 사랑스런 문자열을 출력하자.

    movl    $len,%edx   # 시스템 콜 4번(sys_write)은 세번째 인수로 출력할 
                        # 메세지의 길이를 취한다.
    movl    $msg,%ecx   # 시스템 콜 4번은 두번째 인수로 출력할 메세지가 
                        # 담긴 메모리 주소를 취한다. 즉, 출력할 메세지로의 
                        # 포인터를 취한다.
    movl    $1,%ebx     # 시스템 콜 4번은 첫번째 인수로 파일의 
                        # 핸들(디스크립터)을 취한다.
    movl    $4,%eax     # eax 레지스터에 호출할 커널 시스템 콜의 번호를 
                        # 넣어 준다. 이 경우에는 4번(sys_write) 이다. 
    int     $0x80       # 커널을 호출한다. 

# 출력이 끝났으면, exit 를 호출한다.

    movl    $0,%ebx     # exit 코드로 0을 준다. (c 코드로는 exit(0);)
    movl    $1,%eax     # 시스템 콜 1번 (sys_exit)
    int     $0x80       # 커널을 호출한다.

main.s로 저장한 다음 아래와 같이 컴파일한다.

$ as -o main.o main.s
$ ld -s -o a.out main.o

See also

Favorite site

References


  1. Korea.gnu.org-manual-as.zip