Written with Visual Studio 2015 MASM assembler (ii) develop from scratch a Win32 assembler

First, the establishment of a VC console-type empty project:

1, select "File" from the VS menu -> "New" -> "Project."

2, select the new project: "Visual c ++" -> "Win32" -> "Win32 console application" and then enter the project name, then click on the "OK" button.

3, then click on the "Next" button.

4, then select "Console Application" -> "Empty Project" and then click on the "Finish" button.

Here, we have created a console-type VC empty project.

Second, write your first MASM assembler of Win32 window procedure.

1, project name in the top right mouse button, then click "Add" on the right-click menu -> "New Item."

2. Select "Add New Item" dialog "C ++ file (.cpp)", and then enter the file name you want in the following file name, file extension must pay attention to is the "asm", I use file name is "FirstWindow.asm", then click on the "Add" button.

3, which is to create a screenshot of the assembler I want to create, have created a blank assembler needs to be added here assembler code.

 Third, the project is set to compile Win32, written in assembler, compiler of this assembler.

 1, set the project dependencies.

A, Right project name, select "Generate dependencies" in the context menu -> "Generate Custom."

B, then select the "masm", then click "OK" button.

 2, add assembler file written assembly.

A, project name above the right mouse button, click "Add" in the context menu -> "New Item."

B, select "C ++ file (.cpp)", and then enter the file name you want to use our name in the following, note that the file extension must be "asm", the file name I use is "FirstWindow.asm", then click on the "Add" button.

C, written in assembly code.

All source code compilation:

.386
.model flat,stdcall
option casemap:none
;---------------------------------------------------------------------------------
; Include 文件定义

comment * 多行注释
include     \masm32\include\windows.inc
include    \masm32\include\gdi32.inc
includelib  \masm32\includelib\gdi32.lib
include     \masm32\include\user32.inc
includelib  \masm32\includelib\user32.lib
include    \masm32\include\kernel32.inc
includelib  \masm32\includelib\kernel32.lib
*

;当前正在引入的inc和lib
include     windows.inc
include     gdi32.inc
includelib  gdi32.lib
include     user32.inc
includelib  user32.lib
include    kernel32.inc
includelib  kernel32.lib



;---------------------------------------------------------------------------------
; 数据段(未初始化的变量) 
.data?
hInstance dd ?
hWinMain dd ?

bResult dd ? ;运行结果

.const
   szClassName db  'MyClass',0
   szCaptionMain  db  'My first Window !',0
   szText db  'Win32 Assembly, Simple and powerful !',0

   szRegisterSuccess db '注册窗口成功!',0 ;操作成功的提示信息

   szAppName db 'FirstMASM',0

;---------------------------------------------------------------------------------
; 代码段
.code

;---------------------------------------------------------------------------------
;windows窗口程序的入口函数
WinMainProc proc
        local   @stWndClass:WNDCLASSEX
        local   @stMsg:MSG

        ;得到当前程序的句柄
        invoke  GetModuleHandle,NULL
        mov hInstance,eax

        ;给当前操作分配内存
        invoke  RtlZeroMemory,addr @stWndClass,sizeof @stWndClass

    ;得到光标
        invoke  LoadCursor,0,IDC_ARROW
        mov @stWndClass.hCursor,eax  ;从eax中取出光标句柄,并设置到窗口类中
        push    hInstance
        pop @stWndClass.hInstance
        mov @stWndClass.cbSize,sizeof WNDCLASSEX
        mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
        mov @stWndClass.lpfnWndProc,offset DoMessageProc
        mov @stWndClass.hbrBackground,COLOR_WINDOW + 1
        mov @stWndClass.lpszClassName,offset szClassName

        invoke  RegisterClassEx,addr @stWndClass ;注册窗口类

        mov bResult,eax ;得到注册窗口结果

        ;对注册窗口类结果判断
        .if bResult==0
           invoke ExitProcess,NULL ;注册窗口类失败,直接退出当前程序
        .else
           invoke  MessageBox,NULL,offset szRegisterSuccess,offset szAppName,MB_OK
        .endif

;---------------------------------------------------------------------------------
; 建立并显示窗口
        invoke  CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,offset szCaptionMain,\
            WS_OVERLAPPEDWINDOW,\
            100,100,600,400,\
            NULL,NULL,hInstance,NULL
        mov hWinMain,eax
        invoke  ShowWindow,hWinMain,SW_SHOWNORMAL
        invoke  UpdateWindow,hWinMain

        ; 消息循环
        .while  TRUE
            invoke  GetMessage,addr @stMsg,NULL,0,0

            .if eax == 0
              .break
            .endif

            invoke  TranslateMessage,addr @stMsg
            invoke  DispatchMessage,addr @stMsg
        .endw
        ret

WinMainProc endp

;---------------------------------------------------------------------------------
; 处理windows消息的过程,Windows的回调函数
DoMessageProc    proc    uses ebx edi esi hWnd,uMsg,wParam,lParam
        local   @stPs:PAINTSTRUCT
        local   @stRect:RECT
        local   @hDc

        mov eax,uMsg

        .if eax ==  WM_PAINT
            invoke  BeginPaint,hWnd,addr @stPs
            mov @hDc,eax

            invoke  GetClientRect,hWnd,addr @stRect
            invoke  DrawText,@hDc,addr szText,-1,\
                addr @stRect,\
                DT_SINGLELINE or DT_CENTER or DT_VCENTER

            invoke  EndPaint,hWnd,addr @stPs
        .elseif eax ==  WM_CLOSE
            invoke  DestroyWindow,hWinMain
            invoke  PostQuitMessage,NULL
        .else
            invoke  DefWindowProc,hWnd,uMsg,wParam,lParam
            ret
        .endif

        xor eax,eax
        ret

DoMessageProc endp

;---------------------------------------------------------------------------------
;程序入口点,启动WinMainProc函数
start:
    call WinMainProc
    invoke ExitProcess,NULL
end start

到这里,建立一个汇编工程,并且编写了汇编程序代码,但是还是不能编译,需要进一步对工程设置。

 3,对"Microsoft Macro Assembler"的设置。

 

Guess you like

Origin www.cnblogs.com/sunylat/p/6242373.html