X86 Assembly/FASM Syntax

FASM, also known as Flat Assembler, is an optimizing assembler for the x86 architecture. FASM is written in assembly, so it can assemble/bootstrap itself. It runs on various operating systems including DOS, Linux, Unix, and Windows. It supports the x86 and x86-64 instruction sets including SIMD extensions MMX, SSE - SSE4, and AVX.

Hexadecimal Numbers
FASM supports all popular syntaxes used to define hexadecimal numbers:

Labels
FASM supports several unique labeling features.

Anonymous Labels
FASM supports labels that use no identifier or label name.


 * @@: represents an anonymous label. Any number of anonymous labels can be defined.
 * @b refers to the closest @@ that can be found when looking backwards in source. @r and @b are equivalent.
 * @f refers to the closest @@ that can be found when looking forward in source.

Local Labels
Local labels, which begin with a . (period). You can reference a local label in the context of its global label parent.

Operators
FASM supports several unique operators to simplify assembly code.

The $ Operator
$ describes the current location in an addressing space. It is used to determine the size of a block of code or data. The MASM equivalent of the $ is the SIZEOF operator.

The # Operator
# is the symbol concatenation operator, used for combining multiple symbols into one. It can only be used inside of the body of a macro like rept or a custom/user-defined macro, because it will replace the name of the macro argument supplied with its value.

The ` Operator
 `  is used to obtain the name of a symbol passed to a macro, converting it to a string.

Built In Macros
FASM has several useful built in macros to simplify writing assembly code.

Repetition
The rept directive is used to compact repetitive assembly instructions into a block. The directive begins with the word rept, then a number or variable specifying the number of times the assembly instructions inside of the curly braces proceeding the instruction should be repeated. The counter variable can be aliased to be used as a symbol, or as part of an instruction within the rept block.

Structures
The struc directive allows assembly of data into a format similar to that of a C structure with members. The definition of a struc makes use of local labels to define member values.

Custom Macros
FASM supports defining custom macros as a way of assembling multiple instructions or conditional assembly as one larger instruction. They require a name and can have an optional list of arguments, separated by commas.

Variable Arguments
Macros can support a variable number of arguments through the square bracket syntax.

Required Operands
The FASM macro syntax can require operands in a macro definition using the * operator after each operand.

Operator Overloading
The FASM macro syntax allows for the overloading of the syntax of an instruction, or creating a new instruction. Below, the mov instruction has been overloaded to support a third operand. In the case that none is supplied, the regular move instruction is assembled. Otherwise, the data in op2 is moved to op1 and op2 is replaced by op3.

Hello World
This is a complete example of a Win32 assembly program that prints 'Hello World!' to the console and then waits for the user to press any key before exiting the application.

This is an example for x86_64 GNU+Linux: