NASM on linux.gl.umbc.edu

Setting Up

On linux.gl.umbc.edu, you do not need to do any setting up. You should be able to invoke NASM on the command line just by typing "nasm" as long as "/usr/local/bin/" is in your PATH (it should be).

If you are going to install the NASM assembler on your own machine, you should use the same version as the one installed on linux.gl.umbc.edu.

Running NASM

Suppose you have an assembly language program in a file called "hello.asm". You can assemble it for Linux with the command:

 nasm -f elf hello.asm

The "-f elf" option tells nasm to output the object code in the Executable  and Linking Format (ELF) that Linux uses. This creates an object file  called "hello.o".  The object code is still not executable.  To create an executable file, we must use the linking loader "ld":
 

   ld hello.o
 

This then creates an "a.out" file that you can execute from the Linux command line.

If the loader complains that it cannot find the entry symbol _start, it is because you do not have a global label _start for the entry point of your program. To fix this problem, the beginning of the code section of your program should look like:

        SECTION .text                   ; Code section.
        global _start
_start:                                 ; Entry point.
Another useful option in nasm is "-l" for specifying a listing file. The command:

   nasm -f elf hello.asm -l hello.lst

which will produce a listing file named "hello.lst".  The listing file conatins both the source listing of the assembly language program and the machine code for each assembly language operation.
The "-g" option for nasm unfortunately does next to nothing. It is supposed to generate debugging information placed in the object file for the debugger. However, this is still an unimplemented feature in NASM.

For more information on running NASM, consult Chapter 2 of the online NASM manual, or type:

   nasm -help

GCC vs LD

There are two ways to create a binary file (executable) starting with a nasm source program (file).

Both methods use
   nasm -f elf src_file_name.asm
to create an object file, src_file_name.o

This object file must then be converted into an executable file before you can execute it (object files are not executable).

First (using ld directly):
   ld -s -o executable_name src_file_name.o

Second (using gcc, which calls ld):
   gcc -o executable_name src_file_name.o

The executable that results from either of these methods is virtually identical EXCEPT for the way the stack is set up with the command line arguments, argv and argc. I've given example code that show both. The last two slides in the NASM slides give the code to access the arguments using ld directly. The link labeled."The code to get the command line if you are using GCC" gives the code for the second method.

Which one do you use then? Well, if you going to use libc functions such as printf, scanf, etc, you will need to use gcc. I have not determined how to tell ld to access  libc as a means of resolving uses of the libc functions in the source code.

FYI: The reason for the two step process is to allow you to combine the source code from several files together into a single executable image. Each source
file is first "compiled" or "assembled" into an object file. The linker (ld on linux) combines these together, resolving any cross dependencies, into the executable.