Microsoft Macro Assembler

from Wikipedia, the free encyclopedia

The Microsoft Macro Assembler (abbreviated MASM ) is an assembler developed by Microsoft for X86 processors . It translates assembly code into executable, native machine code .

The Microsoft Macro Assembler developed at times to be the most widely used assembler for the development of MS-DOS programs. Nowadays MASM is the preferred assembler for all Microsoft-based operating systems (probably due to the MASM32-SDK). It is included in current versions in the Microsoft Visual Studio (and Express) development environment , but it is also available in a separate download version. The free versions of MASM may not be used for commercial purposes. In addition, they may only be used for development for Microsoft operating systems - all other operating systems are expressly excluded by the EULA (End-User License Agreement).

As a commercial competitor to the Microsoft Macro Assembler is maintained particularly the Turbo Assembler of Borland . The programs jWasm, Netwide Assembler and Flat assembler , for example , have become known as free and free alternatives .

The current version 14.0 supports the following instruction sets : x86 / 64 , x87-fpu , MMX , 3DNow , SSE , SSE2 , SSE3 , SSSE3 , SSE4.1 , SSE4.2 , SSE4.A , VMX , AVX , AVX2 , AES , CVT16 , FMA3 and FMA4. From version 8.0 there are two editions with the same version number, one for the x86-32 architecture and one for the x86-64 architecture.

Expressions similar to high-level languages

MASM has expressions as you know them from C. These make programming easier and help keep the source text clearer.

  • .while / .endw
  • .repeat / .until
  • .break .continue
  • .if .elseif .else .endif
  • invoke (Function call)

Macros

A particularly striking feature of MASM, as the name suggests, is the extremely powerful macro system. With it it is possible to create program code as well as to process text. So it is e.g. B. possible to generate constructs of higher programming languages ​​like "switch" (multiple selection in C).

Versions

The IBM Macro Assembler and the IBM Macro Assembler / 2 were OEM versions of the MASM. Although MASM is no longer a commercial product, it is still supported by Microsoft. The last MASM version sold as a single software package was version 6.11.

MASM version date product Remarks
1.0 1981 (IBM) for 8086
2.0 1984 (Single product) for 8086/8087
3.0 1984 (Single product)
4.0 1985 (Single product)
5.0 1987 (Single product)
5.1 1988 (Single product) OS / 2 support
6.0 1991 (Single product) 32bit and OS / 2 support, with the integrated development environment Programmer’s WorkBench
6.1 1992 (Single product)
6.11 1993 (Single product)
6.11d 09/19/1995 Windows 95 Driver Developer Kit 6.11d is the last version for DOS
6.12 08/27/1997 (Update) Support for Intel MMX instructions; without DOS extender
6.13 05.12.1997 (Update) Support for AMD 3DNow instructions
6.14 04/12/1999 (Update) Support for SSE
6.15 2000 Visual C ++ 6.0 Processor Pack Support for SSE2
7.0 2002 Visual C ++ .NET 2002
7.1 2003 Visual C ++ .NET 2003
8.0 2005 Visual C ++ 2005 from 8.0 two separate versions: x86-32 (ml.exe) and x86-64 (ml64.exe), SSE3 / SSSE3
9.0 2008 Visual C ++ 2008 SSE4.1 / SSE4.2 / SSE4.A
10.0 2010 Visual C ++ 2010 AVX / AES
11.0 2011 Visual C ++ 2011 AVX2 / FMA / half-precision conversion
12.0 2013 Visual C ++ 2013

Sample programs

example 1

This program displays a dialog box with the text "Hello World":

.686
.model flat,stdcall
option casemap:none

   include windows.inc      ; MASM32 SDK
   include user32.inc       ;
   include kernel32.inc     ;
                            ;
   includelib user32.lib    ;
   includelib kernel32.lib  ;

StrA macro text:=<> ;macro
    IFNDEF some_cntr                                ;
        some_cntr = 0                               ;
    ELSE                                            ;
        some_cntr = some_cntr + 1                   ;
    ENDIF                                           ;
                                                    ;
    IFNB <text>                                     ;
        .data                                       ;
            @CatStr(_stra_,%some_cntr) db text,0    ;
        .code                                       ;
    %   EXITM <OFFSET @CatStr(_stra_,%some_cntr)>   ;
    ELSE                                            ;
        echo string required!                       ;
        EXITM <>                                    ;
    ENDIF                                           ;
endm

.code
start:
                                                                        ;code
    invoke MessageBox,NULL,StrA("Hello World"),StrA("Say hello"),MB_OK  ;
    invoke ExitProcess,NULL                                             ;
                                                                        ;
end start

Example 2

This example program shows a window with the text "Hello World!":

include masm32rt.inc ; MASM32 SDK
.data
    ClassName   db "WinClass",0
    AppName     db "Hello World App",0
    Text        db "Hello World!",0
.data?
    msg         MSG <>
    wc          WNDCLASSEX <>
.code
start:
    mov wc.hInstance,rv(GetModuleHandle,NULL)           ; fill WNDCLASSEX-struct
    mov wc.cbSize,SIZEOF WNDCLASSEX                     ;
    mov wc.style,CS_HREDRAW or CS_VREDRAW               ;
    mov wc.lpfnWndProc,OFFSET WndProc                   ;
    mov wc.hbrBackground,rv(CreateSolidBrush,0FFFFFFh)  ;
    mov wc.lpszClassName,OFFSET ClassName               ;
    mov wc.hIcon,rv(LoadIcon,NULL,IDI_APPLICATION)      ;
    mov wc.hIconSm,eax                                  ;
    mov wc.hCursor,rv(LoadCursor,NULL,IDC_ARROW)        ;

    invoke RegisterClassEx,OFFSET wc
    invoke CreateWindowEx,NULL\                       ; create window
                         ,OFFSET ClassName\           ;
                         ,OFFSET AppName\             ;
                         ,WS_OVERLAPPEDWINDOW\        ;
                         ,CW_USEDEFAULT,CW_USEDEFAULT\;
                         ,CW_USEDEFAULT,CW_USEDEFAULT\;
                         ,NULL\                       ;
                         ,NULL\                       ;
                         ,wc.hInstance\               ;
                         ,NULL                        ;

    push eax                                            ; tricky parameter passing ;)
    invoke ShowWindow,DWORD ptr [esp+4],SW_SHOWNORMAL   ;
    call UpdateWindow                                   ;

    .while TRUE                               ; message loop
        invoke GetMessage,OFFSET msg,NULL,0,0 ;
        .break .if (!eax)                     ;
        invoke TranslateMessage, OFFSET msg   ;
        invoke DispatchMessage, OFFSET msg    ;
    .endw                                     ;

    invoke ExitProcess,0                      ; exit Program

WndProc proc hWnd:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD ; window call-back function
LOCAL ps:PAINTSTRUCT
LOCAL rect:RECT
    .if uMsg == WM_DESTROY
        invoke PostQuitMessage,NULL
    .elseif uMsg == WM_PAINT
        invoke BeginPaint,hWnd,ADDR ps                           ; paint text "Hello World!"
        invoke GetClientRect,hWnd,ADDR rect                      ;
        invoke DrawText,ps.hdc\                                  ;
                       ,OFFSET Text\                             ;
                       ,SIZEOF Text\                             ;
                       ,ADDR rect\                               ;
                       ,DT_CENTER or DT_VCENTER or DT_SINGLELINE ;
        invoke EndPaint,hWnd,ADDR ps                             ;
    .else
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
    .endif
    xor eax,eax
    ret
WndProc endp
end start

Example 3

This program demonstrates an algorithm for the determination of character string lengths with the help of SSE2 instructions:

include masm32rt.inc ; MASM32 SDK
.686p ; choose instruction set
.mmx ;
.xmm ;
szLenXmm proto :DWORD

.data
    szTextA db "This string is 55 bytes long (without termination-zero)",0
.code

start:
    invoke szLenXmm,OFFSET szTextA                                 ; call szLenXmm
    invoke MessageBox,0,OFFSET szTextA\
                       ,cat$(chr$("string size = "),udword$(eax))\ ; generate title using macros
                       ,MB_OK
    invoke ExitProcess,0                                           ; exit program

    szLenXmm proc lpString:DWORD   ; determine string size
        mov edx,lpString           ; using XMM-instrutions
        pxor xmm1,xmm1             ;
        xor ecx,ecx                ; PS: under extremely rarely
        align 16                   ; conditions this func. may
    @@: movdqu xmm0,OWORD ptr [edx]; cause an access violation
        pcmpeqb xmm0,xmm1          ;
        pmovmskb eax,xmm0          ;
        test eax,eax               ;
        lea ecx,[ecx+16]           ;
        lea edx,[edx+16]           ;
        jz @B                      ;
    @@: lea ecx,[ecx-16]           ;
        bsf eax,eax                ;
        .if !ZERO?                 ;
            lea ecx,[eax+ecx]      ;
        .endif                     ; str. size returns
        mov eax,ecx                ; through EAX
        ret                        ;
    szLenXmm endp                  ;
end start

Web links