X86 Assembly/FASM Syntax

From Wikibooks, open books for an open world
< X86 Assembly
Jump to: navigation, search

FASM is an assembler for the IA-32 architecture. The name stands for "[1]flat assembler". FASM itself is written in assembly language and is also available on DOS, DexOS, Linux, Windows, and MenuetOS systems. It shatters the "assembly is not portable at all" myth. FASM has some features that are advanced for assembly languages, such as macros, structures, and "virtual data". FASM contains bindings to the MSWindows GUI and OpenGL.

$FFFF 0xffff 0ffffh[edit]

FASM supports all popular syntaxes of hex numbers.

@@ @f @b[edit]

Anonymous labels are supported. @@ represents a anonymous label and you can use any number of anonymous label. @b represents the backward anonymous label whereas @f represents forward anonymous label Example:

@@: inc eax
    push eax
    jmp @b     ; This will result in a stack fault sooner or later

$[edit]

$ describes current location. Useful for determining the size of a block of code or data. It is just like "SIZEOF" operator in MASM. Example of use:

mystring           db    "This is my string", 0
mystring.length    =     $-mystring

Local Labels[edit]

Local Labels, which begin with a . (a period)

globallabel:

.locallabelone:

.locallabeltwo:

globallabel2:

.locallabelone:

.locallabeltwo:

You can reference local labels from their global label. For example:

globallabel.locallabelone

Macros[edit]

Macros in FASM are described in a C-like manner and are created like this:

macro (name) (parameters) {
  macro code.
}

For example, the following could be used to overload the mov instruction to accept three parameters in FASM:

   macro mov op1,op2,op3
    {
     if op3 eq
       mov   op1,op2
     else
       mov   op1,op2
       mov   op2,op3
     end if
    }

if op3 eq means "If the 3rd parameter (op3) equals nothing, or blank" then do a normal mov operation. Else, do the 3 way move operation.

External links[edit]