Wednesday, January 12, 2011

First assembly programme on 64bit linux machine??

Platform : x64 bit intel arch
Processor : intel core i5 processor
OS : RHEL6 linux
Compiler name : NASM
Assembly compiler src : Download nasm-2.09.03.tar.gz from 

  • Example of asm programme :

cat hello.asm

section .data ;section declaration

msg db "Hello, world!",0xa ;our dear string
len equ $ - msg ;length of our dear string

section .text ;section declaration

;we must export the entry point to the ELF linker or
global _start ;loader. They conventionally recognize _start as their
;entry point. Use ld -e foo to override the default.


;write our string to stdout

mov edx,len ;third argument: message length
mov ecx,msg ;second argument: pointer to message to write
mov ebx,1 ;first argument: file handle (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel

;and exit

mov ebx,0 ;first syscall argument: exit code
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel

  • Compiling above program :

 $ nasm -f elf hello.asm

Linking(making one executable file using object files) :

$ ld -s -o hello hello.o


$ ld -m elf_i386 -s -o hello hello.o

  • Execution of binary :

Hello, world!

NOTE : $ ld -m elf_i386 -s -o hello hello.o

 Here code has compatible on 32 bit machine. But to compile it on 64bit machine, I used emulator here for 32bit machine. It's "elf_i386". An emulator in computer sciences duplicates (provides an emulation of) the functions of one system using a different system, so that the second system behaves like (and appears to be) the first system.

No comments:

Post a Comment