Microsoft Macro Assembler
Encyclopedia
The Microsoft Macro Assembler is an x86
X86 architecture
The term x86 refers to a family of instruction set architectures based on the Intel 8086 CPU. The 8086 was launched in 1978 as a fully 16-bit extension of Intel's 8-bit based 8080 microprocessor and also introduced segmentation to overcome the 16-bit addressing barrier of such designs...

 assembler that uses the Intel syntax
X86 assembly language
x86 assembly language is a family of backward-compatible assembly languages, which provide some level of compatibility all the way back to the Intel 8008. x86 assembly languages are used to produce object code for the x86 class of processors, which includes Intel's Core series and AMD's Phenom and...

 for Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

. there was a version of the Microsoft Macro Assembler for 16-bit
16-bit
-16-bit architecture:The HP BPC, introduced in 1975, was the world's first 16-bit microprocessor. Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816. The Intel 8088 was program-compatible with the Intel 8086, and was 16-bit in that its registers were 16...

 and 32-bit
32-bit
The range of integer values that can be stored in 32 bits is 0 through 4,294,967,295. Hence, a processor with 32-bit memory addresses can directly access 4 GB of byte-addressable memory....

 assembly sources, MASM, and a different one, ML64, for 64-bit
64-bit
64-bit is a word size that defines certain classes of computer architecture, buses, memory and CPUs, and by extension the software that runs on them. 64-bit CPUs have existed in supercomputers since the 1970s and in RISC-based workstations and servers since the early 1990s...

 sources only. References below to MASM include ML64 where appropriate.

MASM is maintained by Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

, but since version 6.12 has not been sold as a separate product, but supplied with various Microsoft SDKs
Software development kit
A software development kit is typically a set of software development tools that allows for the creation of applications for a certain software package, software framework, hardware platform, computer system, video game console, operating system, or similar platform.It may be something as simple...

 and C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 compilers. MASM 8.0 was available free of charge for download from Microsoft for non-commercial use only. It will only install if Visual C++ 2005 Express, also downloadable without charge, is already installed.

History

The earliest versions of MASM date back to 1981.

Up to version 5.0, MASM was available as an MS-DOS
MS-DOS
MS-DOS is an operating system for x86-based personal computers. It was the most commonly used member of the DOS family of operating systems, and was the main operating system for IBM PC compatible personal computers during the 1980s to the mid 1990s, until it was gradually superseded by operating...

 application only. Versions 5.1 and 6.0 were available as both MS-DOS
MS-DOS
MS-DOS is an operating system for x86-based personal computers. It was the most commonly used member of the DOS family of operating systems, and was the main operating system for IBM PC compatible personal computers during the 1980s to the mid 1990s, until it was gradually superseded by operating...

 and OS/2
OS/2
OS/2 is a computer operating system, initially created by Microsoft and IBM, then later developed by IBM exclusively. The name stands for "Operating System/2," because it was introduced as part of the same generation change release as IBM's "Personal System/2 " line of second-generation personal...

 applications.

Version 6.0, released in 1992, added high-level programming support and a more C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

-like syntax.
By the end of the year, version 6.1A updated the memory management to be compatible with code produced by Visual C++
Visual C++
Microsoft Visual C++ is a commercial , integrated development environment product from Microsoft for the C, C++, and C++/CLI programming languages...

. In 1993 full support for 32-bit applications and the Pentium instruction set was added. The MASM binary at that time was shipped as a "bi-modal" DOS-extended binary (using the Phar Lap
Phar Lap (company)
Phar Lap was a software company specializing in software development tools for DOS operating systems. They were most noted for their software allowing developers to access memory beyond the 640 KiB limit of DOS and were an author of the VCPI standard.Phar Lap Software, Inc. was founded in April...

 TNT DOS extender).

Versions 6.12 to 6.14 were implemented as patches for version 6.11. These patches changed the type of the binary to native PE format; version 6.11 is the last version of MASM that will run under MS-DOS.

By the end of 1997 MASM fully supported Windows 95 and included some AMD-specific instructions.

In 1999 Intel released macros for SIMD
SIMD
Single instruction, multiple data , is a class of parallel computers in Flynn's taxonomy. It describes computers with multiple processing elements that perform the same operation on multiple data simultaneously...

 and MMX instructions, which were shortly after supported natively by MASM. With the 6.15 release in 2000, Microsoft discontinued support for MASM as a separate product, instead subsuming it into the Visual Studio toolset. Though it was still compatible with Windows 98
Windows 98
Windows 98 is a graphical operating system by Microsoft. It is the second major release in the Windows 9x line of operating systems. It was released to manufacturing on 15 May 1998 and to retail on 25 June 1998. Windows 98 is the successor to Windows 95. Like its predecessor, it is a hybrid...

, current versions of Visual Studio were not. Support for 64-bit processors was not added until the release of Visual Studio 2005, with MASM given the version number 8.0.

MASM assembly language details

MASM has been the main vehicle for preserving the earlier Intel assembler notation and it can still be written as a fully specified language, a format that many disassemblers produce.

The OFFSET operator

If a data label is referenced in an expression, MASM will generally assume that the content of the memory reference is to be targeted. To access the address and not the content of a data label, MASM supplies addditional operators - OFFSET and SEG - to tell the assembler the different intention:

mov eax, data_label ; load the content of data_label ( assumed DWORD here )
mov eax, OFFSET data_label; load the address ( the offset part of it ) of data_label

The OFFSET operator is not required to get the address of code labels; it is accepted, but ignored.

Square bracket operator []

The square bracket operator behaves differently depending on the context.
For direct addressing, the square brackets are similar to the + operator. As a result, square brackets around a data label have no effect:

mov eax, local_var ; Standard MASM notation
mov eax, [local_var] ; Square brackets won't change semantics here.

However, if the square brackets are used to enclose general purpose registers (GPR), the operator has a different meaning: now it is indicating that the register(s) enclosed is/are to be used as base or index register(s) for indirect memory addressing.

mov eax, [eax] ; dereference the CONTENT of the variable and copy it into the EAX register.

Type coercion with the PTR operator

MASM's built-in PTR operator may be used to a) set a type for an "untyped" memory reference or b) override the type for a data label. The semantics are quite similar to C typecasts.

movzx eax, [esi] ; This fails as the assembler cannot determine the size of data to be zero extended into the EAX register.

movzx eax, BYTE PTR [esi] ; this is the correct notation if the programmer wishes to zero extend a BYTE into a 32 bit register.
mov ax, WORD PTR byte_label ; override the type of a variable

Argument type checking

From at least version 6.0, MASM has supported a high-level notation for creating procedures that perform argument size and count checking. It is part of a system using the PROC ENDP PROTO and INVOKE directives. The PROTO directive is used to define prototypes, PROC/ENDP pairs define so-called procedures. Both can then be called with the INVOKE directive, which includes argument size and argument count checking. Using those directives is optional.

Using an example prototype from the 32 bit Windows API
Windows API
The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces available in the Microsoft Windows operating systems. It was formerly called the Win32 API; however, the name "Windows API" more accurately reflects its roots in 16-bit Windows and its support on...

 function set,

SendMessage PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD
SendMessage equ

The code to call this function using the INVOKE notation is as follows.

invoke SendMessage,hWin,WM_COMMAND,wParam,lParam

Which is translated exactly to,

push lParam
push wParam
push WM_COMMAND
push hWin
call SendMessage

The 64-bit version of MASM (ML64) did not support INVOKE.

Calling conventions

MASM supports a number of different calling conventions on both the 16 bit real mode DOS operating system, the 16 bit Windows versions and the later 32 bit versions. MASM supports the C, SYSCALL, STDCALL, BASIC, FORTRAN and PASCAL calling conventions
X86 calling conventions
This article describes the calling conventions used on the x86 architecture.Calling conventions describe the interface of called code:* The order in which atomic parameters, or individual parts of a complex parameter, are allocated...

.

High-level control loops

MASM provides a notation to emulate a variety of high level control and loop structures.

It supports the .IF block structure,

.if
-
.elseif
-
.else
-
.endif

It also supports the .WHILE loop structure,

.while eax > 0
sub eax, 1
.endw

And the .REPEAT loop structure.

.repeat
sub eax, 1
.until eax < 1

The high level emulation also supports C runtime comparison operators that work according to the same rules as Intel mnemonic comparisons.

The 64-bit version of Masm (ML64) does not support these features.

Preprocessor

MASM has a very powerful preprocessor that has considerably more functionality than C preprocessors. The MASM preprocessor is an integral part of the assembler and hence has access to the assembler's symbol table and expression evaluator. Seen from the outside, the preprocessor consists of
  • assembly-time conditional directives IF, IFDEF, IFB, IFIDN, ...
  • string manipulation and query directives CATSTR, SUBSTR, INSTR, ...
  • assembly-time loop directives REPEAT, WHILE, FOR, FORC,...
  • MACRO, EXITM and GOTO directives
  • operators, i.e. OPATTR, that allows to detect a symbol's type at assembly-time.


Using the EXITM notation a macro can return a value or register in a way that can be used similar to a function call. A very simple example:

addregs32 MACRO reg1, reg2
add reg1, reg2
EXITM
ENDM
;--- now "calling" macro addregs32
mov ecx, 16
mov edx, 8
mov eax, addregs32(ecx, edx)

Object module formats supported by MASM

Early versions of MASM generated object modules using the OMF
Relocatable Object Module Format
The Relocatable Object Module Format is an object file format used primarily for software intended to run on Intel 80x86 microprocessors. It was originally developed by Intel under the name Object Module Format, and is perhaps best known to DOS users as a .OBJ file...

 format, which was used to create binaries for MS-DOS
MS-DOS
MS-DOS is an operating system for x86-based personal computers. It was the most commonly used member of the DOS family of operating systems, and was the main operating system for IBM PC compatible personal computers during the 1980s to the mid 1990s, until it was gradually superseded by operating...

 or OS/2
OS/2
OS/2 is a computer operating system, initially created by Microsoft and IBM, then later developed by IBM exclusively. The name stands for "Operating System/2," because it was introduced as part of the same generation change release as IBM's "Personal System/2 " line of second-generation personal...

.

Since version 6, MASM is able to produce object modules in the Portable Executable
Portable Executable
The Portable Executable format is a file format for executables, object code and DLLs, used in 32-bit and 64-bit versions of Windows operating systems. The term "portable" refers to the format's versatility in numerous environments of operating system software architecture...

 (PE/COFF) format. PE/COFF is compatible with recent Microsoft C compilers, and object modules produced by either MASM or the C compiler can be routinely intermixed and linked into Win32 and Win64 binaries.

Assemblers compatible with MASM

Some other assemblers can assemble most code written for MASM, with the exception of more complex macros.
  • Pelle's Macro Assembler, a component of the Pelles C
    Pelles C
    Pelles C is a lightweight freeware integrated development environment for Windows and Pocket PC programming in the C language built and maintained by Pelle Orinius, featuring:* Integrated source code editor with Win32 API call tips, and auto indenting....

     development environment.
  • JWASM Macro Assembler, licenced under the Sybase Open Watcom EULA.
  • Turbo Assembler
    Turbo Assembler
    Turbo Assembler is an assembler package developed by Borland which runs on and produces code for 16- or 32-bit x86 MS-DOS or Microsoft Windows. It can be used with Borland's high-level language compilers, such as Turbo Pascal, Turbo Basic and Turbo C. The Turbo Assembler package is bundled with...

     (TASM) developed by Borland, later owned by Embarcadero, last updated in 2002 and supplied with Delphi and C++Builder for several years, later discontinued.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK