Filter SDK/Assembler optimizing

From Avisynth wiki
Jump to: navigation, search



The main reason assembly language is used in AviSynth and some of its plugins is to optimize the code for speed. Modern C++ compilers generally optimize code quite well in most cases. But there are still cases where compilers perform poorly and where dramatic increases in speed can be achieved by careful assembly programming.

Assembly language is a low-level programming language for a computer, or other programmable device, in which there is a very strong (generally one-to-one) correspondence between the language and the architecture's machine code instructions. Each assembly language is specific to a particular computer architecture, in contrast to most high-level programming languages, which are generally portable across multiple architectures, but require interpreting or compiling.

Assembly language is converted into executable machine code by a utility program referred to as an assembler; the conversion process is referred to as assembly, or assembling the code source.

There are several ways to write assembly:

  • using inline assembly in C/C++;
  • making separate assembly modules;
  • using intrinsic functions in C/C++;
  • using a library (such as Softwire) that generates assembly language code at run-time.

For each way a short description is given and its advantages and disadvantages with respect to platforms are listed [1] [2]:

Inline assembly

The inline assembler is a feature of some compilers that allows very low level code written in assembly to be embedded in a high level language like C/C++ source. Microsoft and Intel C++ compilers support inline assembly using a subset of the MASM (Microsoft Macro Assembler) syntax.

The main advantages:

  • It is easy to combine with C/C++.
  • Variables and other symbols defined in C/C++ code can be accessed from the assembly code.
  • The compiler takes care of calling conventions, name mangling and saving registers.
  • Portable to different x86 platforms when using the Intel compiler.
  • The inline assembler is built into the compiler, so you don't need a separate assembler such as MASM.

The main disadvantages:

  • Different compilers use different syntax. This is a problem if you want to use a different compilers or you want to port your inline assembly code to other platforms.
  • The Microsoft compiler does not support inline assembly on 64-bit platforms.

Separate assembly modules

To overcome the disadvantages of inline assembly you could put the assembly code in a separate file and use an external assembler (such as the Microsoft Macro Assembler or the Network Assembler).


The Microsoft Macro Assembler (MASM) is included with Microsoft C++ compilers. Free versions can sometimes be obtained by downloading the Microsoft Windows driver kit (WDK) or the platforms software development kit (SDK) or as an add-on to the free Visual C++ Express Edition. MASM has been a de-facto standard in the Windows world for many years, and the assembly output of most Windows compilers uses MASM syntax. MASM is only for windows.


The Network Assembler (NASM) is a free open source assembler with support for several platforms and object file formats. The syntax is more clear and consistent than MASM syntax. NASM has fewer high- level features than MASM, but it is sufficient for most purposes. NASM is a free multi-platform assembler.

Intrinsic functions

This is the newest and most convenient way of combining low-level and high-level code. Intrinsic functions are high-level language representatives of machine instructions.

The main advantages:

  • The code is portable to almost all x86 platforms: 32-bit and 64-bit Windows, Linux, Mac OS, etc. Some intrinsic functions can even be used on Itanium and other non-x86 platforms.
  • The code is compatible with Microsoft, Gnu and Intel compilers.
  • The compiler takes care of register variables, register allocation and register spilling. The programmer doesn't have to care about which register is used for which variable.

The main disadvantages:

  • Not all assembly instructions have intrinsic function equivalents.
  • The Microsoft compilers have no MMX intrinsics support for 64-bit systems ([3]).

Just-in-time compilation

In computing, just-in-time compilation (JIT), also known as dynamic translation, is compilation done during execution of a program – at run time – rather than prior to execution. Most often this consists of translation to machine code (...) [4].

SoftWire is a run-time x86 assembler. This makes it useful for a compiler's code generator, a JIT-compiler for scripting languages. [5]

Assembler writing

This section contains information on various experiences and information on assembler writing. This will mostly cover information on MMX and Integer SSE.

Compiling asm code

compiling asm code

Back to Filter SDK

Personal tools