Tool of the Week: The Netwide Assembler

I just read about NASM (The Netwide Assembler) and found a new tool to play around with.  The original developers where Simon Tatham and Julian Hall and is currently being worked on by Peter Anvin, Frank B. Kotler, Victor van den Elzen, and Cyrill Gorcunov.  NASM is released under a BSD licence,

This one might be kinda difficult, both in the documented and source code for most people.  I’ve just started reading the docs but this one looks like a lot fun to work with once you get threw it.  Here is snippet of the sections that I liked.  This one is on checking the type of a token  and contains a code recipe is for making a file.

4.4.6 %ifid, %ifnum, %ifstr: Testing Token Types

Some macros will want to perform different tasks depending on whether they are passed a number, a string, or an identifier. For example, a string output macro might want to be able to cope with being passed either a string constant or a pointer to an existing string.

The conditional assembly construct %ifid, taking one parameter (which may be blank), assembles the subsequent code if and only if the first token in the parameter exists and is an identifier. %ifnum works similarly, but tests for the token being a numeric constant; %ifstr tests for it being a string.

For example, the writefile macro defined in section 4.3.3 can be extended to take advantage of %ifstr in the following fashion:

%macro writefile 2-3+

%ifstr %2
 jmp %%endstr
 %if %0 = 3
 %%str: db %2,%3
 %%str: db %2
 %%endstr: mov dx,%%str
 mov cx,%%endstr-%%str
 mov dx,%2
 mov cx,%3
 mov bx,%1
 mov ah,0x40
 int 0x21


Then the writefile macro can cope with being called in either of the following two ways:

writefile [file], strpointer, length
writefile [file], “hello”, 13, 10
In the first, strpointer is used as the address of an already-declared string, and length is used as its length; in the second, a string is given to the macro, which therefore declares it itself and works out the address and length for itself.

Note the use of %if inside the %ifstr: this is to detect whether the macro was passed two arguments (so the string would be a single string constant, and db %2 would be adequate) or more (in which case, all but the first two would be lumped together into %3, and db %2,%3 would be required).

The usual %elif…, %ifn…, and %elifn… versions exist for each of %ifid, %ifnum and %ifstr.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s