Wednesday, December 31, 2008

NASM : My First Program

Some years ago, Assembly Language was very popular. I used TASM , MASM and Microsoft Debug on that time. I think Debug one of the very powerful tools for cracking.

I just figure out, is there any Assembly Language in Linux?

Because, usually Assembly Language use DOS and BIOS Interrupt Services.

I finally found a good application, named NASM (Net wide Assembler). I have tried the sample codes from

NASM doesn’t have a text editor, so you can use your familiar text editor then save the file with .asm file extension.

There are few main differences to be considered when you use assembly language in Linux:

* In DOS mostly you use interrupt int 21h, int 10h and int 16h. In Linux, everything is handled by kernel with int 80h.
*Linux is a 32-bit protected mode operating system, so have to use extended 32-bit registers, such as EAX, EBX, ECX and so on. In 16-bit register, you still use AX, BX,CX etc …. registers.
*In 32-bit programming, you don’t have to think about segments at all because it runs in the flat memory model.
*I think no body write DOS assembly anymore. Hey… it’s 16 bit.

How I can get Nasm?

NASM is listed in Ubuntu Repository, so just download it using apt.

vishnu@vishnu-desktop:~$ sudo apt-get install nasm

Write your first Linux Assembly Language Application
Here I will Show you a step by step example for Displaying "hello World"

Step 1. Type the code

You can use, vim, vi, gedit etc. The extension must be .asm.

vishnu@vishnu-desktop:~$ sudo vim hello.asm

section .data

hello: db ‘Hello world!’,10

helloLen: equ $-hello

section .text

global _start


mov eax,4

mov ebx,1

mov ecx,hello

mov edx,helloLen

int 80h

mov eax,1

mov ebx,0

int 80h

save the file.

Actually, I’m new myself in Assembly Programming in Linux. I took the codes above from the NASM website. I just wanna show you that we can do Assembly Programming in Linux. If you want to know deeper you can go

to this link: ……………………..

Step 2. Compile .asm (source code) to .o (object)

vishnu@vishnu-desktop:~$ nasm -f elf hello.asm

Step 3. Link the object file to produce an executable file.

vishnu@vishnu-desktop:~$ ld -s -o hello hello.o

Now, you have 3 files, hello.asm (your source code), hello.o (your object file) and hello (your program).

Step 4. Ready to run

You can run your program by typing:

vishnu@vishnu-desktop:~$ ./hello

Hello world!

Congratulations! You have just written your first assembly program in Linux.

If you want to know more details about nasm and ld, you can see the manual.

vishnu@vishnu-desktop:~$ man ld

vishnu@vishnu-desktop:~$ man nasm

No comments: