X86 Assembly/NASM Syntax
From Wikibooks, the open-content textbooks collection
- This section of the x86 Assembly book is a stub. You can help by expanding this section.
[edit] NASM Syntax
NASM syntax looks like:
mov ax, 9
This loads the number 9 into register ax. Notice that the instruction format is "dest, src". This follows the Intel style x86 instruction formatting, as opposed to the AT&T style used by the GNU Assembler. Note for people using gdb with nasm, you can set gdb to use Intel-style disassembly by issuing the command:
set disassembly-flavor intel
[edit] NASM Comments
A single semi-colon is used for comments, and can be used like a double slash in C/C++.
[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 edx, 1 ; input length (one byte) mov ecx, variable ; address to pass to mov ebx, 1 ; read from standard input int 0x80 ; call the kernel
Outputting follows a similar convention:
mov eax, 4 ; the system interprets 4 as "write" mov ecx, variable ; pointer to the value being passed mov ebx, 1 ; standard output (print to terminal) mov edx, 4 ; length of output (in bytes) int 0x80
Passing values to the registers in different orders won't affect the execution when the kernel is called, but deciding on a methodology can make it much easier to read.