본문 바로가기

ASSAMBLY/NASM 어셈브리어

Centos 6.4 에서 NASM 개발 환경 설정

64비트로 컴을 업글하고 나서 숙원사업 이었던 어셈공부를 하려니 교재가 32비트로 되어 있어서 virtualbox로 centos6.4를 깔고 개발 환경을 설정해 보았다.


1. nasm 다운받기

http://www.nasm.us/pub/nasm/releasebuilds/2.11.03/

저장 받은 파일을 적당한 장소에서 압축을 풀어줍니다.

저는 /root/nasm 폴더를 만들어 놓고 압축을 풀었습니다.

압축을 풀고 나면 /root/nasm/nasm-2.11.03 폴더가 생성됩니다. 그 폴더로 이동한 후 터미너털 창을 열어서

1) $./configure

2) $ make

3) $ make install

을 차례대로 실행하면 일딴 nasm 설치는 마무리 된 것입니다.

이제 nasm 이 제대로 설치되었나를 확인하기 위해서 hellow world!를 찍는 간단한 코드를 작성하고 컴파일 해보겠습니다.

section .data
	hello:     db 'Hello world!',10    ; 'Hello world!' plus a linefeed character
	helloLen:  equ $-hello             ; Length of the 'Hello world!' string
	                                   ; (I'll explain soon)

section .text
	global _start

_start:
	mov eax,4            ; The system call for write (sys_write)
	mov ebx,1            ; File descriptor 1 - standard output
	mov ecx,hello        ; Put the offset of hello in ecx
	mov edx,helloLen     ; helloLen is a constant, so we don't need to say
	                     ;  mov edx,[helloLen] to get it's actual value
	int 80h              ; Call the kernel

	mov eax,1            ; The system call for exit (sys_exit)
	mov ebx,0            ; Exit with return code of 0 (no error)
	int 80h

편집기를 이용하여 위의 코드를 작성하고 "hello.asm" 파일로 저장합니다.( 필자는 vim 이용)

터미널에서 

1) nasm -f elf hello.asm

2) ld -s -o hello hello.o

3) ./hello

를 차례로 입력하면 화면에 "Hello world"라는 문구라 출력되는 것을 확인할 수 있습니다.

(참고로 centos 설치시 개발자용으로 설치하시면 ld, gcc 등의 필요한 컴파일러 및 개발툴을 설치 시 기본으로 설치가 됨으로 편합니다.^^)