# X86 Assembly/NASM Syntax

The Netwide Assembler is an x86 and x86-64 assembler that uses syntax similar to Intel. It supports a variety of object file formats, including:
* ELF32/64
* Linux a.out
* NetBSD/FreeBSD a.out
* MS-DOS 16-bit/32-bit object files
* Win32/64 object files
* COFF
* Mach-O 32/64
* rdf
* binary
NASM runs on both Unix/Linux and Windows/DOS.

## NASM Syntax

The Netwide Assembler (NASM) uses a syntax "designed to be simple and easy to understand, similar to Intel's but less complex". This means that the operand order is dest then src, as opposed to the AT&T style used by the GNU Assembler. For example,
loads the number 9 into register ax.
For those using gdb with nasm, you can set gdb to use Intel-style disassembly by issuing the command:

### Comments

A single semi-colon is used for comments, and functions the same as double slash in C++: the compiler ignores from the semicolon to the next newline.

### Macros

NASM has powerful macro functions, similar to C's preprocessor. For example,

## Example I/O (Linux and BSD)

To pass the kernel a simple input command on Linux, you would pass values to the following registers and then send the kernel an interrupt signal. To read in a single character from standard input (such as from a user at their keyboard), do the following:
After the int 0x80, eax will contain the number of bytes read. If this number is < 0, there was a read error of some sort.
Outputting follows a similar convention:
BSD systems (MacOS X included) use similar system calls, but convention to execute them is different. While on Linux you pass system call arguments in different registers, on BSD systems they are pushed onto stack (except the system call number, which is put into eax, the same way as in Linux). BSD version of the code above:

## Hello World (Linux)

Below we have a simple Hello world example, it lays out the basic structure of a nasm program:
In order to assemble, link and run the program we need to do the following:

## Hello World (Using only Win32 system calls)

In this example we are going to rewrite the hello world example using Win32 system calls. There are several major differences:
* The intermediate file will be a Microsoft Win32 (i386) object file
* We will avoid using interrupts since they may not be portable and, this is Windows, not DOS, therefore we need to bring in several calls from kernel32 DLL
In order to assemble, link and run the program we need to do the following:
In this example we use the -e command line option when invoking ld to specify the entry point for program execution. Otherwise we would have to use _WinMain@16 as the entry point rather than _start. This example was run under cygwin, in a Windows command prompt the link step would be different. One last note, WriteConsole does not behave well within a cygwin console, so in order to see output the final exe should be run within a Windows command prompt.

## Hello World (Using C libraries and Linking with gcc)

In this example we will rewrite Hello World to use printf(3) from the C library and link using gcc. This has the advantage that going from Linux to Windows requires minimal source code changes and a slightly different assemble and link steps. In the Windows world this has the additional benefit that the linking step will be the same in the Windows command prompt and cygwin. There are several major changes:
* The "hello, world" string now becomes the format string for printf(3) and therefore needs to be null terminated. This also means we do not need to explicitly specify its length anymore.
* gcc expects the entry point for execution to be main
* Microsoft will prefix functions using the cdecl calling convention with a underscore. So main and printf will become _main and _printf respectively in the Windows development environment.
In order to assemble, link and run the program we need to do the following.
The Windows version with prefixed underscores:
In order to assemble, link and run the program we need to do the following.