Difference between revisions of "CSC231 nasmld script"
(→Version 1) |
(→Version 2: more sophisticated) |
||
Line 30: | Line 30: | ||
=Version 2: more sophisticated= | =Version 2: more sophisticated= | ||
<br /> | <br /> | ||
− | This version stops if nasm or ld return an error. | + | This version stops and gives an error message if nasm or ld return an error. |
<br /> | <br /> | ||
<source lang="bash"> | <source lang="bash"> | ||
Line 74: | Line 74: | ||
# no errors, run the program! | # no errors, run the program! | ||
+ | rm $filename.o | ||
./$filename | ./$filename | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
=Usage= | =Usage= | ||
<br /> | <br /> |
Revision as of 10:19, 18 September 2014
--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 and gives an error message 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!
rm $filename.o
./$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