CMSC313, Computer Organization & Assembly Language Programming, Fall 2012

NASM Help


Setting Up

On linux.gl.umbc.edu, first find out if your shell variables are configured correctly. Type: which -a nasm You should see /usr/bin/nasm. If not, need to add /usr/bin/ to your PATH environment variable.

Currently, OIT has two versions of nasm installed on the GL system. The one we want is in /usr/bin. You can use the alias command to make sure that you use the correct version:

alias nasm /usr/bin/nasm If you are using Bourne shell, use the syntax: alias nasm=/usr/bin/nasm Use the ps command to identify your shell. If you see bash or sh, you are using Bourne shell. If you see tcsh or csh, you are using C shell.

After issuing the alias command, try invoking nasm with the -version switch. You should see something like:

linux2% nasm -version NASM version 2.07 compiled on Feb 25 2010 linux2% You can place the alias command in your .cshrc or .tcshrc (which ever one you use). That way you don't have to issue the alias command every time you log in.

If you have a 32-bit Linux installation, you can run nasm on your own machine. Other flavors of Unix are not compatible, including 64-bit Linux, BSD Unix, MacOS X. You can run nasm on these machines and produce 32-bit ELF object code, but you cannot run it. Check that you have a recent installation of nasm:

nasm -version The latest version is available from the official NASM web site. (The latest version as of August 2012 is 2.10.04.)


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 tells nasm to generate debugging information for use in gdb:

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

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

nasm -help


Last Modified: 22 Jul 2024 11:28:26 EDT by Richard Chang
to Fall 2012 CMSC 313 Homepage