Skip to main content
Engineering LibreTexts

5.4: Assemble/Link Script

  • Page ID
    19886
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    When programming, it is often necessary to type the assemble and link commands many times with various different programs. Instead of typing the assemble (yasm) and link (ld) commands each time, it is possible to place them in a file, called a script file. Then, the script file can be executed which will just execute the commands that were entered in the file. While not required, using a script file can save time and make things easier when working on a program.

    A simple example bash(For more information, refer to: http://en.Wikipedia.org/wiki/Loader_(computing)) assemble/link script is as follows:

         #!/bin/bash
         # Simple assemble/link script.    
         if [ -z $1 ]; then
           echo "Usage:  ./asm64 <asmMainFile> (no extension)"
           exit
         fi
         #  Verify no extensions were entered
    
         if [ ! -e "$1.asm" ]; then
           echo "Error, $1.asm not found."
           echo "Note, do not enter file extensions."
           exit
         fi
         #  Compile, assemble, and link.
    
         yasm -Worphan-labels -g dwarf2 -f elf64 $1.asm -l $1.lst
         ld -g -o $1 $1.o
    

    The above script should be placed in a file. For this example, the file will be named asm64 and placed in the current working directory (where the source files are located).

    Once created, execute privilege will need to be added to the script file as follows:

           chmod +x asm64
    

    This will only need to be done once for each script file.

    The script file will read the source file name from the command line. For example, to use the script file to assemble the example from the previous chapter (named example.asm), type the following:

           ./asm64 example
    

    The ".asm" extension on the example.asm file should not be included (since it is added in the script). The script file will assemble and link the source file, creating the list file, object file, and executable file.

    Use of this, or any script file, is optional. The name of the script file can be changed as desired.


    This page titled 5.4: Assemble/Link Script is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Ed Jorgensen.

    • Was this article helpful?