CSC231 nasmld script

From dftwiki3
Revision as of 10:18, 18 September 2014 by Thiebaut (talk | contribs) (Version 1)
Jump to: navigation, search

--D. Thiebaut 09:18, 7 November 2012 (EST)
revised --D. Thiebaut (talk) 08:12, 16 September 2014 (EDT)


Version 1

#! /bin/bash
# nasmld
# D. Thiebaut
# assembles and links an assembly program for 
# a Pentium processor running Linux
# To create the script, edit with your favorite text editor,
# save to current directory or to ~/bin, and make executable:
# 
#     chmod +x nasmld
#
set -e   # stop on error

filename=$(basename "$1")
filename="${filename%.*}"

nasm -f elf -F stabs $filename.asm
ld -o $filename -melf_i386 $filename.o
rm $filename.o
./$filename



Version 2: more sophisticated


This version stops if nasm or ld return an error.

#! /bin/bash
# nasmld
# D. Thiebaut
# assembles and links an assembly program for 
# a Pentium processor running Linux.
# To create the script, edit with your favorite text editor,
# save to current directory or to ~/bin, and make executable:
# 
#     chmod +x nasmld
#

# display syntax if user forgets
if [ ! -n "$1" ]
then
  echo "Usage: `basename $0` progName"
  exit 1
fi  

# remove the ".asm" part of the filename
filename=$(basename "$1")
filename="${filename%.*}"

# assemble and generate a listing (optional)
nasm -f elf -F stabs $filename.asm -l $filename.lst

# if we get errors, stop
if [ "$?" -ne "0" ]; then
    echo "*** Nasm ERROR! ***"
    exit
fi

# link
ld -melf_i386 -o $filename $filename.o

# if we get errors, stop
if [ "$?" -ne "0" ]; then
    echo "Link ERROR! ***"
    exit
fi

# no errors, run the program!
./$filename


Usage


If you have a program, say HelloWorld.asm, and want to assemble, link and run it, simply type:

  nasmld  HelloWorld.asm

at the command line, or, if that doesn't work:

  ./nasmld HelloWorld.asm