X86 Assembly/NASM Syntax
The Netwide Assembler is an assembler that uses Intel syntax; it is often used on Linux.
Contents |
[edit] 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,
mov ax, 9
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:
set disassembly-flavor intel
[edit] 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.
[edit] Macros
NASM has powerful macro functions, similar to C's preprocessor. For example,
%define newline 0xA %define func(a, b) ((a) * (b) + 2)
[edit] Example I/O (Linux)
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:
; read a byte from stdin mov eax, 3 ; 3 is recognized by the system as meaning "read" mov ebx, 0 ; read from standard input mov ecx, variable ; address to pass to mov edx, 1 ; input length (one byte) int 0x80 ; call the kernel
Outputting follows a similar convention:
; print a byte to stdout mov eax, 4 ; the system interprets 4 as "write" mov ebx, 1 ; standard output (print to terminal) mov ecx, variable ; pointer to the value being passed mov edx, 1 ; length of output (in bytes) int 0x80 ; call the kernel