Passion/Programming

[Assembly] NASM and hello world

sunshout 2013. 2. 28. 10:28

1.   NASM

1.1      NASM intro

¡ NASM Installation

CentOS Install

yum install nasm

 

Ubuntu Install

apt-get install nasm

 

¡ Hello World (hello.nasm)

global _start

section .text

_start:

        mov eax, 4  ;sys_write

        mov ebx, 1  ;stdout

        mov ecx, message ;msg address

        mov edx, length  ;msg length

        int 80h

 

        mov eax, 1      ;sys_exit

        mov ebx, 0      ;return 0

        int 80h

 

section .data

        message: db 'Hello, World!',0x0a

        length: equ $-message   ;NASM definition pseudo-instruction

 

¡ Compile Assembly program

64bit system

nasm -f elf64 -o hello.o hello.nasm

 

32bit system

nasm -f elf -o hello.o hello.nasm

 

¡ Link the machine code into ELF binary

ld -o hello hello.o

 

¡ run execution code

[root@tesla-xen nasm]# ls

hello  hello.nasm  hello.o

[root@tesla-xen nasm]# ./hello

Hello, World!

 

To DO

. Analyze assembly language and CPU ring state change (usermode to kernel mode)