The content on this page assumes that you’ve completed the first (ungraded) assignment assignment.
In what follows we explain how to create and run a simple NASM program, namely the ics312_first_v1.asm program discussed in class.
The course’s main Web page points to several .asm files that contain simple assembly programs used in the lecture notes as examples. In the first assignment you downloaded an archive called nasm_check.tar that contained several source files:
The sequence of command-line instructions to compile the program were:
nasm -f elf ics312_test.asm -o ics312_test.o
gcc -m32 -no-pie -c driver.c -o driver.o
nasm -f elf -d ELF_TYPE asm_io.asm -o asm_io.o
gcc -m32 -no-pie ics312_test.o driver.o asm_io.o -o ics312_test
The first command assembles the ics312_test.asm file into the ics312_test.o object file. The second command compiles the C driver into the driver.o object file. The third command assembles the asm_io.asm file into the asm_io.o object file (this is the convenient I/O “library” that we talked about in class). The fourth and last command links all the object files into an executable. The program can then be executed by typing “./ics312_test” on the command-line
All these steps were automated by a “Makefile”, included in the archive.
You can think of a makefile as an ancestor to today’s IDEs (Integrated
Development Environments). They’re still used because they are simple, and
supported everywhere. They can be created with any text editor (like the small
one we use here), but typically generated automatically by
build environments (e.g., cmake). A makefile is processed
by the make
Linux/UNIX utility. It makes it very easy to compile
code by just typing “make” on the command-line, in a way that recompiles
only what is necessary to be re-compiled. Google for “make tutorial” to find
many, many on-line instructions for using make if you’re curious. Below we
explain how we can use our Makefile for our purposes. There is no need to
become a makefile expert for this course.
Here are the steps that you can follow for each NASM program you’ll write in the course:
tar -xvf nasm_check.tar
commandmv nasm_check nasm_program
cd nasm_program
ics312_first_v1.asm
Makefile
and replace ics312_test
in the first line by ics312_first_v1
(i.e., the name of your program)make
command will now assemble, link, and run your programdefault: $(PROGRAM) run
in the makefile by default: $(PROGRAM)
. To run the program you must now type make run
. Of course you can always run your program by invoking its name from the command-line.In some assignment you will be asked to write multiple programs. You then
want to have a single Makefile to compile them all. Below is how to do this
simply for 2 programs. You can of course repeat this pattern for more
programs. There are much fancier ways to do this with shorter Makefiles,
but this is a the most basic approach. You should download the Makefile below (cut-and-pasting Makefiles is fraught with peril due to TAB characters). You then just need to replace firstprogram
and secondprogram
by the actual names of your programs (without the .asm extension). It should be easy to tweak this Makefile to extend it and make it do what you want.
PROGRAM1=firstprogram
PROGRAM2=secondprogram
CC=gcc
CFLAGS=-m32 -no-pie
ASM=nasm
ASMFLAGS=-f elf
default: $(PROGRAM1) $(PROGRAM2)
$(PROGRAM1): $(PROGRAM1).o driver.o asm_io.o
$(CC) $(CFLAGS) $(PROGRAM1).o driver.o asm_io.o -o $(PROGRAM1)
$(PROGRAM1).o: $(PROGRAM1).asm
$(ASM) $(ASMFLAGS) $(PROGRAM1).asm -o $(PROGRAM1).o
$(PROGRAM2): $(PROGRAM2).o driver.o asm_io.o
$(CC) $(CFLAGS) $(PROGRAM2).o driver.o asm_io.o -o $(PROGRAM2)
$(PROGRAM2).o: $(PROGRAM2).asm
$(ASM) $(ASMFLAGS) $(PROGRAM2).asm -o $(PROGRAM2).o
asm_io.o: asm_io.asm
$(ASM) $(ASMFLAGS) -d ELF_TYPE asm_io.asm -o asm_io.o
driver.o: driver.c
$(CC) $(CFLAGS) -c driver.c -o driver.o
clean:
/bin/rm -f *.o $(PROGRAM1) $(PROGRAM2)