Our Feeds

Wednesday 18 February 2015

Ajith KP

Compile NASM Source code

          NASM is one of famous assembler which is available for Linux, Windows and Mac OSX.
          To compile with NASM we need two steps. First step is generate object file which cannot execute and second step is to generate executable file.

          To create object file of NASM source code you need to install NASM assembler by 'sudo apt-get install nasm'.

          To create object file of code mycode.asm execute command, 'nasm -f elf mycode.asm'.

          Now we will get an object code named 'mycode.o'. Then we need convert it to executable code. To convert it to execute code you need to execute command, 'ld -s -o mycode mycode.o'. This command will generate an executable file called 'mycode'. Later you can execute this file.

    
           If you are lazy fellow and wanna compile with one command I have coded a bash script.


#!/bin/bash
echo "NASM Tool"
echo "Coded By AJITH KP (ajithkp560)"
if [ "$#" -ne 1 ]; then
echo "Enter assembly file name. Eg. $0 filename"
else
nasm -f elf $1
file=${1%%.*}
filex="$file.o"
ld -s -o $file $filex
echo "'$1' is compiled. You can execute the file '$file'."
fi
          Save this code as 'asmc.sh' and copy it to directory '/usr/bin' using command 'sudo cp  asmc.sh /usr/bin/asmc.sh'.

          After copy this file to /usr/bin you can execute it by execute command 'asmc.sh'. To compile the assembly source code just execute 'asmc.sh mycode.asm'. It will produce executable file 'mycode'.