ICS 312: Small NASM How To on our Linux server

 Logging in to the server


You have an account on a Linux server provided for this course. The hostname and your username/password have been discussed in class. You have to use Ssh to connect to the server. The server is a standard Linux box, which just happens to have NASM installed. You can also install NASM on your own machine if you want to, in which case you'll never have to use the server.

 Creating a simple NASM program


The course's main Web page points to several .asm files that contain simple assembly programs used in the lecture notes as examples. The simplest of all these programs is ics312_first_v0.asm, which is an empty program that does nothing. Remember that all our assembly programs are called from a C driver which we call driver.c.

You can download all these files and move them to the server using Scp (secure copy, which comes with Ssh typically).

The steps for generating an executable are:

nasm -f elf ics312_first_v0.asm -o ics312_first_v0.o
gcc -m32 -c driver.c -o driver.o
gcc -m32 ics312_first_v0.o driver.o -o ics312_first_v0
The above three commands generate the executable ics312_first_v0, which as we saw in class doesn't print anything to the screen.

 Creating a NASM program that uses asm_io


For most programs we'll write will want to print things to the screen. To do this we need to put %include "asm_io.inc" at the top of your program. For isntance, look at the program ics312_first_v1.asm. Now we need the files asm_io.asm and asm_io.inc. We now generate three .o files as follows:
nasm -f elf ics312_first_v1.asm -o ics312_first_v1.o
gcc -m32 -c driver.c -o driver.o
nasm -f elf -d ELF_TYPE asm_io.asm -o asm_io.o
gcc -m32 ics312_first_v1.o driver.o asm_io.o -o ics312_first_v1
This generates an executable that prints "21" to the screen (without a carriage return).

 Using a makefile


Whenever developing code, especially in a Linux environment without a fancy IDE, on should always use makefiles. The Linux make utility is very powerful. It makes it very easy to compile code by just typing "make", in a way that recompiles only what is necessary to be compiled. Your programming assignments will require that you have a makefile. Google for "make tutorial" to find many, many on-line instructions for using make. An absolutely non-fancy makefile for the previous program, and essentially for all your assignments is downloadable here and looks like:
PROGRAM=ics312_first_v1
CC=gcc
CFLAGS=-m32
ASM=nasm
ASMFLAGS=-f elf

default: $(PROGRAM)

$(PROGRAM): $(PROGRAM).o driver.o asm_io.o
    $(CC) $(CFLAGS) $(PROGRAM).o driver.o asm_io.o -o $(PROGRAM)

$(PROGRAM).o: $(PROGRAM).asm
    $(ASM) $(ASMFLAGS) $(PROGRAM).asm -o $(PROGRAM).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 $(PROGRAM)
IMPORTANT: The extra spaces before the "action" lines are TAB characters, which will be lost if you simply cut-and-paste this.

To compile the code, just type "make". To remove all built files type "make clean".

 Random UNIX stuff


Useful commands in UNIX are:


henric@hawaii.edu