To assemble a file, you issue a command of the form
nasm -f <format><filename>[-o <output>]
For example,
nasm -f elf myfile.asm
will assemble myfile.asm into an ELF object file myfile.o. And
nasm -f bin myfile.asm -o myfile.com
will assemble myfile.asm into a raw binary file myfile.com.
To produce a listing file, with the hex codes output from NASM displayed on the left of the original
sources, use the -l option to give a listing file name, for example:
nasm -f coff myfile.asm -l myfile.lst
To get further usage instructions from NASM, try typing
nasm -h
The option --help is an aliasfor the -h option.
If you use Linux but aren’t sure whether your system is a.out or ELF, typefile nasm
(in the directory inwhich you put the NASM binary when you installed it). If it says something like
nasm: ELF 32-bit LSB executable i386 (386 and up) Version 1
then your system is ELF, and you should use the option -f elf when you want NASM to produce Linux
object files. If it says
nasm: Linux/i386 demand-paged executable (QMAGIC)
or something similar, your system is a.out, and you should use -f aout instead (Linux a.out systems
have long been obsolete, and are rare these days.)
Like Unix compilers and assemblers, NASM is silent unless it goes wrong: you won’t see any output at
all, unless it gives error messages.-f formatselect output fileformatbin Flat raw binary (MS-DOS, embedded, ...)[default]ith Intel Hex encoded flat binarysrec Motorola S-records encoded flat binaryaout Linux a.outaoutb NetBSD/FreeBSD a.outcoff COFF (i386)(DJGPP, some Unix variants)elf32 ELF32 (i386)(Linux, most Unix variants)elf64 ELF64 (x86-64)(Linux, most Unix variants)elfx32 ELFx32 (ELF32 for x86-64)(Linux)as86 as86 (bin86/dev86 toolchain)obj Intel/Microsoft OMF (MS-DOS, OS/2, Win16)win32 Microsoft extended COFF for Win32 (i386)win64 Microsoft extended COFF for Win64 (x86-64)rdf Relocatable Dynamic Object File Format v2.0ieee IEEE-695 (LADsoft variant) object fileformatmacho32 Mach-O i386 (Mach, including MacOS X and variants)macho64 Mach-O x86-64 (Mach, including MacOS X and variants)dbg Trace of all info passed to output stageelf Legacy aliasfor"elf32"macho Legacy aliasfor"macho32"win Legacy aliasfor"win32"
hello,world例子
(base)[myhaspl@localhost nasm]$ cat test.asm
section .data ;section declaration
msg db "Hello, world!",0xA ;our dear string
len equ $ - msg ;length of our dear string
section .text ;section declaration;we must export the entry point to the ELF linker orglobal _start ;loader. They conventionally recognize _start as their;entry point. Use ld -e foo to override the default.
_start:
;write our string to stdoutmov eax,4 ;system call number (sys_write)mov ebx,1 ;first argument: file handle (stdout)mov ecx,msg ;second argument: pointer to message to writemov edx,len ;third argument: message lengthint 0x80 ;call kernel
;and exitmov eax,1 ;system call number (sys_exit)xor ebx,ebx ;first syscall argument: exit codeint 0x80 ;call kernel
(base)[myhaspl@localhost nasm]$ (base)[myhaspl@localhost nasm]$ nasm -f elf64 test.asm
(base)[myhaspl@localhost nasm]$ ls
test.asm test.o
(base)[myhaspl@localhost nasm]$ ld -s -o test test.o
(base)[myhaspl@localhost nasm]$ lstest test.asm test.o
(base)[myhaspl@localhost nasm]$ ./test
Hello, world!