Assembly programming using NASM

Posted: April 9, 2012 in Technology
Tags: , , , ,

After coming across to NASM(netwide assembler) today through some random browsing, I did some programming in Assembly today after a long time (last time I remember was in second year of my undergrad days). Here is a hello world program

section .data
hello : db 'Hello World',10 ;initialise hello with string hello world and terminate with endline character
helloLen : equ $ - hello ;declare constant helloLen
;$ is current position while hello is the start position of string hello
;subtraction gives the length of the hello
section .text ;code section
global _start ;_start is the label where execution should begin
_start: ;_start label definition starts here
mov eax,4 ;system call write()
mov ebx,1 ;file descriptor,first argument to write()
mov ecx,hello ;second argument, our string
mov edx,helloLen ;third argument stringlength
int 80h ;call the system call

mov eax,1 ;system call exit()
mov ebv,0 ;first argument to exit()
int 80h ;call system call exit()

Save the file as hello.asm
Compile the code using nasm as follows:
nasm -f elf hello.asm
For x86-64 platform, instead of above command use:
nasm -f elf64 hello.asm
this will produce a binary object file hello.o. Link this hello.o to an executable using ld as follows:
ld -s -o hello hello.o
This will give you an executable hello. execute it using
it should print
Hello World
Happy Programming


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s