Wednesday, November 3, 2010

NASM and includes

I've been building a 'system' library, that essentially provides high level functions to do system level things like draw on the screen, read from disk, you know the kind of thing I mean.

Anyhow, the file was getting pretty large and unwieldy, so I wanted to split the code down into sections that could be maintained independently and included into the main system.asm file. NASM provides a nice way to do this so I thought I'd record it here for my own benefit and anyone else's who may stubble across this.

NASM syntax for specifying an include(from within the asm file) is %include and so my system.asm file contains the following line, to include the screen library code

%include "screen.asm"

I chose not to fill up my root directory with all these included library asms so I put them in to a sub directory named 'system', however, NASM complains that it can't find them. This is easily overcome with NASM's -i switch, which allows you to specify a prefix that should be appended to the include name when searching. Since my build script (build.bat) runs in a a directory alongside my source I needed to also allow for the change of directory in there.

My directory structure looks like this

\NickOS\dev\src\system.asm        <- this has the %include
\NickOS\dev\src\system\screen.asm <- the file I want to include
\NickOS\dev\bin\build.bat         <- my build script

The build script, since it's on a different path uses the following to assemble system.asm

>nasm.exe -f rdf ..\src\system.asm -l .\system.lst -o .\system.rdf -i ..\src\system\

When build.bat runs, NASM uses the path specified with i relative to '\NickOS\dev\bin' to locate the include and build the binary.