Difference between revisions of "CSC231 nasmld script"
(→Version 2: more sophisticated) |
|||
Line 2: | Line 2: | ||
revised --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 08:12, 16 September 2014 (EDT) | revised --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 08:12, 16 September 2014 (EDT) | ||
---- | ---- | ||
+ | =Preparation and Mini Lab= | ||
+ | <br /> | ||
+ | * Creating a bash script is a very simple process: | ||
+ | :# use emacs to create a text file (the same as an assembly file). Give it a catchy name. For example ''myScript.sh'' (.sh is a common extension for scripts). | ||
+ | :# write this as the first line of the script: | ||
+ | |||
+ | #! /bin/bash | ||
+ | |||
+ | :# add comments that start with the # character. Document the script with a header. | ||
+ | :# add Linux commands, one per text line. | ||
+ | :# save your file | ||
+ | :# and make it executable: | ||
+ | |||
+ | chmod a+x myScript.sh | ||
+ | |||
+ | ::(make sure the file name is the one you created with emacs!) | ||
+ | :# and you're done! | ||
+ | :# You can now run your script as follows: | ||
+ | |||
+ | ./myScript.sh | ||
+ | |||
+ | <br /> | ||
+ | == Understanding Command-Line Parameters== | ||
+ | <br /> | ||
+ | * Create the following script, called '''script1.sh''': | ||
+ | <br /> | ||
+ | <source lang="bash"> | ||
+ | #! /bin/bash | ||
+ | # script1.sh | ||
+ | # your name | ||
+ | # demo script | ||
+ | |||
+ | echo "program name = $0" | ||
+ | echo "first parameter = $1" | ||
+ | echo "second parameter = $2" | ||
+ | </source> | ||
+ | <br /> | ||
+ | * make your script executable and run it with a few parameters: | ||
+ | <br /> | ||
+ | chmod a+x | ||
+ | ./script1.sh | ||
+ | ./script1.sh hello beautiful world! | ||
+ | ./script1.sh 1 + 2 = 3 | ||
+ | |||
=Version 1= | =Version 1= | ||
+ | <br /> | ||
<source lang="bash"> | <source lang="bash"> | ||
#! /bin/bash | #! /bin/bash |
Revision as of 07:54, 30 September 2015
--D. Thiebaut 09:18, 7 November 2012 (EST)
revised --D. Thiebaut (talk) 08:12, 16 September 2014 (EDT)
Contents
Preparation and Mini Lab
- Creating a bash script is a very simple process:
- use emacs to create a text file (the same as an assembly file). Give it a catchy name. For example myScript.sh (.sh is a common extension for scripts).
- write this as the first line of the script:
#! /bin/bash
- add comments that start with the # character. Document the script with a header.
- add Linux commands, one per text line.
- save your file
- and make it executable:
chmod a+x myScript.sh
- (make sure the file name is the one you created with emacs!)
- and you're done!
- You can now run your script as follows:
./myScript.sh
Understanding Command-Line Parameters
- Create the following script, called script1.sh:
#! /bin/bash
# script1.sh
# your name
# demo script
echo "program name = $0"
echo "first parameter = $1"
echo "second parameter = $2"
- make your script executable and run it with a few parameters:
chmod a+x ./script1.sh ./script1.sh hello beautiful world! ./script1.sh 1 + 2 = 3
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