|This article relies on references to primary sources. (February 2012)|
|Original author(s)||Simon Tatham, Julian Hall|
|Developer(s)||H. Peter Anvin, et al.|
|Stable release||2.11.05 / May 21, 2014|
|Operating system||Microsoft Windows, Unix-like, OS/2, OS X, DOS|
The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered to be one of the most popular assemblers for Linux.
NASM was originally written by Simon Tatham with assistance from Julian Hall and is currently maintained by a small team led by H. Peter Anvin. It is available as free software under the terms of the simplified (2-clause) BSD license.
NASM can output several binary formats including COFF, Portable Executable, a.out, ELF, Mach-O and .bin (binary disk image, used for compiling operating systems), though position-independent code is only supported for ELF object files. NASM also has its own binary format called RDOFF.
The variety of output formats allows programs to be retargeted to virtually any x86 operating system. In addition, NASM can create flat binary files, usable in writing boot loaders, ROM images, and in various facets of OS development. NASM can run on non-x86 platforms, such as SPARC and PowerPC, though it cannot generate programs usable by those machines.
NASM uses a variation of Intel assembly syntax instead of AT&T syntax. It also avoids features such as automatic generation of segment overrides (and the related ASSUME directive) used by MASM and compatible assemblers.
Examples of programs for various operating systems
section .text org 0x100 mov ah, 0x9 mov dx, hello int 0x21 mov ax, 0x4c00 int 0x21 section .data hello: db 'Hello, world!', 13, 10, '$'
An example of a similar program for Microsoft Windows:
global _main extern _MessageBoxA@16 extern _ExitProcess@4 section code use32 class=code _main: push dword 0 ; UINT uType = MB_OK push dword title ; LPCSTR lpCaption push dword banner ; LPCSTR lpText push dword 0 ; HWND hWnd = NULL call _MessageBoxA@16 push dword 0 ; UINT uExitCode call _ExitProcess@4 section data use32 class=data banner: db 'Hello, world!', 0 title: db 'Hello', 0
An equivalent program for Linux:
global _start section .text _start: mov eax, 4 ; write mov ebx, 1 ; stdout mov ecx, msg mov edx, msg.len int 0x80 ; write(stdout, msg, strlen(msg)); mov eax, 1 ; exit mov ebx, 0 int 0x80 ; exit(0) section .data msg: db "Hello, world!", 10 .len: equ $ - msg
Below is a 64-bit program for Apple OS X that works on NASM version 2.11.06 that inputs a keystroke and shows it on the screen. The newest compiled Apple version of the NASM binaries that are available on the website should be copied into /usr/bin. If your assembly program is called hello.s, then the Terminal window command to build a 64-bit Apple object file (with extension .o) would be nasm -f macho64 hello.s; and then to create the executable, the following direct linker command would be fine:
ld -arch x86_64 -macosx_version_min 10.0 -o hello hello.o
global _start section .data query_string: db "Enter a character: " query_string_len: equ $ - query_string out_string: db "You have input: " out_string_len: equ $ - out_string section .bss in_char: resw 4 section .text _start: mov rax, 0x2000004 ; put the write-system-call-code into register rax mov rdi, 1 ; tell kernel to use stdout mov rsi, query_string ; rsi is where the kernel expects to find the address of the message mov rdx, query_string_len ; and rdx is where the kernel expects to find the length of the message syscall ; read in the character mov rax, 0x2000003 ; read system call mov rdi, 0 ; stdin mov rsi, in_char ; address for storage, declared in section .bss mov rdx, 2 ; get 2 bytes from the kernel's buffer (one for the carriage return) syscall ; show user the output mov rax, 0x2000004 ; write system call mov rdi, 1 ; stdout mov rsi, out_string mov rdx, out_string_len syscall mov rax, 0x2000004 ; write system call mov rdi, 1 ; stdout mov rsi, in_char mov rdx, 2 ; the second byte is to apply the carriage return expected in the string syscall ; exit system call mov rax, 0x2000001 ; exit system call mov rdi, 0 syscall
NASM principally outputs object files, which are generally not executable in and of themselves. The only exception to this are flat binaries (e.g., .COM) which are inherently limited in modern use. To translate the object files into executable programs, an appropriate linker must be used, such as the Visual Studio "LINK" utility for Windows or ld for UNIX-like systems.
On 28 November 2007, version 2.00 was released, adding support for x86-64 extensions. The development versions are not uploaded to SourceForge.net; instead, they are checked in to the project's own Git repository with binary snapshots available from the project web page.
A search engine for NASM docs is also available.
As of version 2.07, NASM is now under the Simplified (2-clause) BSD license.
- Ram Narayan. "Linux assemblers: A comparison of GAS and NASM". "two of the most popular assemblers for Linux, GNU Assembler (GAS) and Netwide Assembler (NASM)"
- "The Netwide Assembler". Retrieved 2008-06-27.
- "NASM Version History". Retrieved 2009-07-19.
- "NASM Manual". Retrieved 2009-08-15.
- Randall Hyde. "NASM: The Netwide Assembler". Retrieved 2008-06-27.
- "NASM Doc Search Engine". Retrieved 2009-09-14.
- NASM website
- SourceForge Page
- Special edition for Win32 and BeOS.
- A comparison of GAS and NASM at IBM
- intel2gas : a converter between the source format of the NASM and GAS assemblers