Difference between revisions of "CSC231 nasmld script"

From dftwiki3
Jump to: navigation, search
(Version 1)
(A More Sophisticated Assemble/Link Script: Version 2)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 09:18, 7 November 2012 (EST)<br />
 
--[[User:Thiebaut|D. Thiebaut]] 09:18, 7 November 2012 (EST)<br />
revised --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 08:12, 16 September 2014 (EDT)
+
revised --[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:06, 30 September 2015 (EDT)
 
----
 
----
 +
<br />
 +
__TOC__
 +
<br />
 +
<br />
 +
<bluebox>
 +
This page presents a ''super fast'' introduction to writing bash scripts to assemble and link programs. 
 +
</bluebox>
 +
<br />
 +
<br />
 +
=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 />
 +
===Step 1===
 +
<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
 +
 +
<br />
 +
* You should now understand what $0, $1, and so on refer to in a bash script.
 +
===Step 2===
 +
<br />
 +
* Modify your script and add a few lines:
 +
<br />
 +
::<source lang="bash">
 +
#! /bin/bash
 +
# script1.sh
 +
# your name
 +
# demo script
 +
 +
echo "Your input line is $@"
 +
echo "You have entered $# parameters"
 +
echo "program name =  $0"
 +
echo "first parameter = $1"
 +
echo "second parameter = $2"
 +
</source>
 +
<br />
 +
===Step 3: testing if the # of parameters is correct===
 +
<br />
 +
* Another modification: we check to see if $1, the first parameter on the command line, exists.  If it doesn't, then we display some syntax/usage information for the user, and the script stops.  You may want to look at this [http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html page] on how to test various quantities in bash.
 +
<br />
 +
::<source lang="bash">
 +
#! /bin/bash
 +
# script1.sh
 +
# your name
 +
# an example script that makes sure there's 1 parameter before displaying
 +
# what is found on the command line.
 +
 +
 +
# display syntax if user forgets Parameter 1.  We test is the length of parameter 1
 +
# is 0. (-n is true if length is non zero, so ! -n is true if the length is 0)
 +
if [ ! -n "$1" ]; then 
 +
  echo "Usage: `basename $0` parameter1"
 +
  exit 1
 +
fi 
 +
 +
# display various quantities referring to the command line.
 +
echo "your input line is $@"
 +
echo "you have entered $# parameters"
 +
echo "program name =  $0"
 +
echo "first parameter = $1"
 +
echo "second parameter = $2"
 +
</source>
 +
<br />
 +
<br />
 +
----
 +
<br />
  
=Version 1=
+
=An Assemble/Link Script: Version 1=
<source lang="bash">
+
<br />
 +
::<source lang="bash">
 
#! /bin/bash
 
#! /bin/bash
 
# nasmld
 
# nasmld
Line 10: Line 116:
 
# assembles and links an assembly program for  
 
# assembles and links an assembly program for  
 
# a Pentium processor running Linux
 
# 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")   # remove the path in front of the file name
filename=$(basename "$1")
+
filename="${filename%.*}"   # remove the extension from the file name
filename="${filename%.*}"
 
  
 
nasm -f elf -F stabs $filename.asm
 
nasm -f elf -F stabs $filename.asm
 
ld -o $filename -melf_i386 $filename.o
 
ld -o $filename -melf_i386 $filename.o
 +
rm $filename.o
 +
./$filename
 
</source>
 
</source>
 
<br />
 
<br />
 
<br />
 
<br />
  
=Version 2: more sophisticated=
+
=A More Sophisticated Assemble/Link Script: Version 2=
 
<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">
 
#! /bin/bash
 
#! /bin/bash
 
# nasmld
 
# nasmld
 
# D. Thiebaut
 
# D. Thiebaut
 
# assembles and links an assembly program for  
 
# assembles and links an assembly program for  
# a Pentium processor running Linux
+
# 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
 
# display syntax if user forgets
Line 40: Line 158:
  
 
# remove the ".asm" part of the filename
 
# remove the ".asm" part of the filename
filename=$(basename "$1")
+
filename=$(basename "$1")   # remove path
filename="${filename%.*}"
+
filename="${filename%.*}"   # remove extension
  
 
# assemble and generate a listing (optional)
 
# assemble and generate a listing (optional)
Line 62: Line 180:
  
 
# no errors, run the program!
 
# no errors, run the program!
 +
rm $filename.o
 
./$filename
 
./$filename
  
 
</source>
 
</source>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
+
<br />
 +
 
 +
=Usage=
 +
<br />
 +
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
 +
 +
 +
 
 +
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 
[[Category:CSC231]]
 
[[Category:CSC231]]

Latest revision as of 09:11, 30 September 2015

--D. Thiebaut 09:18, 7 November 2012 (EST)
revised --D. Thiebaut (talk) 09:06, 30 September 2015 (EDT)





This page presents a super fast introduction to writing bash scripts to assemble and link programs.



Preparation and Mini Lab


  • Creating a bash script is a very simple process:
  1. 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).
  2. write this as the first line of the script:
  #! /bin/bash

  1. add comments that start with the # character. Document the script with a header.
  2. add Linux commands, one per text line.
  3. save your file
  4. and make it executable:
  chmod a+x myScript.sh

(make sure the file name is the one you created with emacs!)
  1. and you're done!
  2. You can now run your script as follows:
  ./myScript.sh


Understanding Command-Line Parameters


Step 1


  • 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


  • You should now understand what $0, $1, and so on refer to in a bash script.

Step 2


  • Modify your script and add a few lines:


#! /bin/bash
# script1.sh
# your name
# demo script

echo "Your input line is $@"
echo "You have entered $# parameters"
echo "program name =  $0"
echo "first parameter = $1"
echo "second parameter = $2"


Step 3: testing if the # of parameters is correct


  • Another modification: we check to see if $1, the first parameter on the command line, exists. If it doesn't, then we display some syntax/usage information for the user, and the script stops. You may want to look at this page on how to test various quantities in bash.


#! /bin/bash
# script1.sh
# your name
# an example script that makes sure there's 1 parameter before displaying
# what is found on the command line.


# display syntax if user forgets Parameter 1.  We test is the length of parameter 1 
# is 0. (-n is true if length is non zero, so ! -n is true if the length is 0)
if [ ! -n "$1" ]; then   
  echo "Usage: `basename $0` parameter1"
  exit 1
fi  

# display various quantities referring to the command line.
echo "your input line is $@"
echo "you have entered $# parameters"
echo "program name =  $0"
echo "first parameter = $1"
echo "second parameter = $2"





An Assemble/Link Script: 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")   # remove the path in front of the file name
filename="${filename%.*}"   # remove the extension from the file name

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



A More Sophisticated Assemble/Link Script: Version 2


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")    # remove path
filename="${filename%.*}"    # remove extension

# 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