Skip to content
systemcoding.dev
Assembly

NASM vs GAS vs MASM vs FASM: Which x86 Assembler to Use

Choose an x86 assembler by platform, syntax, macro power and output formats. A decision table compares NASM, GNU as, MASM and FASM, with when not to pick each.

7 min read
Summarize in AI
Flat illustration of two options weighed side by side, with the Intel, GNU and C++ logos.

Four assemblers account for nearly all hand-written x86 code: NASM, the GNU assembler, Microsoft's MASM and Tomasz Grysztar's FASM. They accept different syntax, target different object formats, and come with different build tools, so the right one depends on where the code will live. This post compares them on the points that decide a project, ends with a decision table, and says when not to choose each. The comparison is for x86-64, with NASM 2.16, binutils 2.42 and FASM 1.73 as reference versions. The cornerstone guide to NASM, GAS and assembler syntax covers the syntax details; this post is about the choice.

What each one is

NASM, the Netwide Assembler, is an independent x86 and x86-64 assembler with Intel syntax, a capable macro preprocessor and output formats for ELF, Mach-O, PE and COFF, flat binaries, and others. It runs on Linux, macOS and Windows, and it is the usual choice for tutorials and for code that must build the same everywhere. It supports only x86.

GAS, the GNU assembler, is as from binutils. It is the back end of GCC, so it has to accept everything GCC emits. It supports dozens of architectures with one consistent set of directives, defaults to AT&T syntax for x86 and accepts Intel syntax through .intel_syntax noprefix. It needs the C preprocessor for macros that share headers with C, and it has its own .macro facility.

MASM, the Microsoft Macro Assembler, ships with the Visual Studio build tools as ml.exe for 32-bit and ml64.exe for 64-bit. It uses an Intel-style syntax with high-level conveniences such as PROC, ENDP, .IF and typed structures. It targets Windows PE and COFF only.

FASM, the flat assembler, is a self-hosted assembler written in assembly. It has a unique syntax, a macro system that can define new instructions, and the ability to produce flat binaries, ELF, PE and others directly without a separate linker. It is a single small executable with no dependencies.

The comparison

PointNASMGASMASMFASM
Default x86 syntaxIntelAT&TIntel (MASM)Intel (FASM)
Intel optionnative.intel_syntax noprefixnativenative
Platforms it runs onLinux, macOS, WindowsLinux, macOS, Windows, BSDWindowsLinux, Windows, DOS
OutputELF, Mach-O, COFF, PE-ish, binELF, Mach-O, COFF through binutils buildsCOFF onlyELF, PE, COFF, bin
Needs a linkeryesyesyesno, for executables
Other architecturesnoyesnono
Macro systemstrong, %macro and preprocessor.macro, plus C preprocessor with .Sstrong, with high-level directivesvery strong, can define instruction syntax
Used by GCC and Clangnoyesnono
Standard in kernel sourcesnoyesnono
Debug infoDWARF, CodeView in some formatsDWARF, CFICodeViewlimited

The table hides the biggest practical fact: GAS is the only one of the four that every GCC and Clang toolchain already has installed. If you have a compiler, you have as. NASM, MASM and FASM are extra tools to install.

Where each one fits

NASM: tutorials, cross-platform code, bootloaders

NASM is the default for learning on Linux because its Intel syntax matches the manuals, its error messages are clear, and its flat binary output (-f bin) is the standard way to produce boot sectors. It is also the right tool for a library of assembly routines that has to build on Linux, macOS and Windows with one source, since only the -f flag and a few section names change.

When not to use it: when the assembly has to share macros with C headers, because NASM does not read C preprocessor output as such. When the project must also target ARM or RISC-V with the same tool and directive vocabulary. When the code ends up inside the Linux kernel or a GCC-built project that already assumes GAS.

GAS: compiler output, kernel code, multi-architecture work

GAS is the right choice when the assembly sits next to C in a GNU or LLVM toolchain. It reads the same .cfi_* directives, .type and .size as compiler output, honors -g debug line tables, and works with .S files preprocessed by GCC. Because one assembler covers every architecture the GNU toolchain targets, a project with x86, ARM and RISC-V code can use a common set of directives.

When not to use it: when you only write Intel-syntax assembly and want the clearest macros. GAS in Intel mode has quirks, notably that a bare symbol means memory contents, which surprises anyone coming from NASM. And standalone builds on Windows outside MinGW or MSYS2 are inconvenient.

MASM: Windows-only work with Microsoft tools

MASM is correct when the project is built by Visual Studio or MSBuild and the assembly must follow Microsoft's x64 conventions, integrate with structured exception handling directives (.PUSHREG, .ALLOCSTACK, .ENDPROLOG) and appear in .vcxproj files. Its high-level directives save typing for large Windows drivers and legacy code.

When not to use it: for anything that must also build on Linux or macOS, since no other platform has it, and for new learners, because the high-level directives hide what the instructions are. 64-bit MASM also drops some conveniences from the 32-bit version, such as INVOKE and .MODEL, so older examples fail with unhelpful errors.

FASM: small tools, boot code and self-contained binaries

FASM is attractive when you want one tiny tool that writes a finished executable. format ELF64 executable produces a runnable program without a linker, and format binary produces a flat image. Its macro language can build entire domain-specific instruction sets, which is why OS hobbyists favor it.

When not to use it: when collaborators expect a mainstream toolchain, since FASM syntax is not shared by any other assembler and tooling support such as debugger line numbers and IDE integrations is thinner. When your build system is based on object files and a linker, because FASM's strength is bypassing them.

A single program in all four

The same program makes the differences visible. Each version exits with status 42 on Linux x86-64 or Windows as noted.

NASM, Linux, built with nasm -f elf64 exit.asm -o exit.o && ld exit.o -o exit:

exit.asmnasm
global _start
section .text
_start:
    mov    eax, 60
    mov    edi, 42
    syscall

GAS, Linux, built with as exit.s -o exit.o && ld exit.o -o exit:

exit.sasm
    .globl  _start
    .text
_start:
    movl    $60, %eax
    movl    $42, %edi
    syscall

FASM, Linux, built with fasm exit.fasm exit, with no linker involved:

exit.fasmasm
format ELF64 executable 3
entry start
segment readable executable
start:
    mov    eax, 60
    mov    edi, 42
    syscall

MASM on Windows exits through the C runtime or the kernel API rather than a system call, because the Windows system call numbers change between releases and are not a stable interface. The standard route calls ExitProcess:

exit.asmasm
extrn ExitProcess : proc

.code
main PROC
    sub    rsp, 40              ; shadow space plus alignment
    mov    ecx, 42
    call   ExitProcess
main ENDP
END
Shell
ml64 exit.asm /link /subsystem:console /entry:main kernel32.lib

After running each, echo $? on Linux or echo %ERRORLEVEL% on Windows prints:

Text
42

Only the Windows version needs the shadow space reservation. Only the FASM version needed no linker and no section directives. The GAS and NASM versions are identical in structure and differ in spelling, which is why moving between them is easy.

Decision table

If your project...UseReason
is a Linux tutorial or learning exerciseNASMIntel syntax matches the manuals, clear errors
lives in a GCC, LLVM or Linux kernel treeGASAlready installed, shares CFI and preprocessor with compiler output
must support x86 and ARM or RISC-V with one toolGASOne directive set across architectures
builds only on Windows with Visual StudioMASMNative to the toolchain and exception directives
must build on Windows, Linux and macOS with one sourceNASMOne syntax and format flags for each platform
produces boot sectors or flat firmware blobsNASM or FASMBoth write raw binaries directly
needs a tiny self-contained assembler with no linkerFASMEmits executables on its own
needs heavy macro metaprogrammingFASM or NASMBoth have strong preprocessors
includes C headers in the assemblyGAS with .SThe C preprocessor handles the headers

What about YASM and LLVM

Two other tools come up. YASM is a rewrite of NASM that accepted both NASM and GAS syntax. Its last release was 1.3.0 in 2014, so it is a legacy choice, though some old build systems still call it. And LLVM's integrated assembler, used by Clang, reads GAS-compatible syntax, so a GAS-syntax file builds with Clang without binutils installed. That makes GAS syntax the most widely accepted dialect, even when the tool is not GAS.

A reasonable default

If you have no constraints, learn NASM on Linux for Intel syntax and its tooling, and learn to read GAS output because every compiler listing uses it. If you are joining an existing project, use the assembler it already uses, and avoid converting for taste. The cost of translating a code base between dialects is far higher than the benefit of a preferred notation.

Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.