MASM

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.


Microsoft's 'in-house' x86 assembler, utilising Intel syntax; the only one that is really compatible with Microsoft's Visual Studio.

MASM can be obtained by downloading the Visual Studio Processor Pack (you must have either VS Service Pack 4 or 5 installed first):

NB: Visual Studio Service Pack 6 is not compatible with the processor pack


Contents

MASM And Games Programming

Very few games nowadays are written entirely in assembly (the main exception being older consoles such as the Sega Genesis, NES and SNES). However, it is still used where optimisation is required in a higher-level language such as C or C++. One can program either a small section of assembly 'embedded' into the high level language, or a whole routine seperately linked into the final program.

Inline Assembly

When assembly is written inside another, higher level language, it is said to be inline, mainly because any variables in scope at the time are also in the scope of the assembly.

Here are some examples, for both Microsoft Visual Studio 6 and GCC.

// Visual Studio 6
#include <iostream>
 
int main (int argc, char* argv[])
{
    int a = 10;
    int b = 10;
    int result;
    
    __asm mov eax, a
    __asm add b, eax
    __asm mov result, eax
    
    std::cout << "Answer: " << result << std::endl;
    return 0;
}

// GCC (G++)
#include <iostream>
 
int main (int argc, char* argv[])
{
    int a = 10;
    int b = 10;
    int result;
    
    asm ("movl %2, %%eax;
          addl %%eax, %1;
          movl %%eax, %0"
          :"=r"(result)
          :"r"(a), "r"(b)
          :"%eax");
    
    std::cout << "Answer: " << result << std::endl;
    return 0;
}

External Routines

Where larger blocks of assembly are needed, it is common to write them in a seperate assembly file which is assembled externally, and the resulting object file linked into the final executable or library.

These routines need to be declared as 'extern' in C/C++, and 'global' (Intel syntax) or '.globl' ( AT&T syntax) in the corresponding assembly file.