笔者在学习Windows下的图形界面应用程序(GUI,Graphical User Interface)的时候碰到的第一个麻烦就是架设WIN32汇编程序的开发环境,在这里笔者愿意和大家分享这段经历。
笔者认为大家应该首先具备下列概念和文件:
好了,现在我们开始架设WIN32汇编程序的开发环境
安装完成以后在Masm32文件夹下新建一个“MyDocument”文件夹,将下面代码放入其中
TITLE Demo.asm
; ===============================================
; Author: 狼の禅
; Date: 23/07/2009
; ===============================================
. 486
.model flat,stdcall
option casemap: none
include \masm32\include\windows. inc
include \masm32\include\user32. inc
include \masm32\include\kernel32. inc
include \masm32\include\gdi32. inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
; -----------------------------------------------
WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD
WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
; ===============================================
.data
hInstance dd 0
hWnd dd 0
szClassName db " Demo " , 0
szDisplayName db " My Demo " , 0
; ===============================================
.code
start:
invoke GetModuleHandle, 0
mov hInstance,eax
invoke WinMain,hInstance, 0 , 0 ,SW_SHOWDEFAULT
invoke ExitProcess,eax
; ===============================================
WinMain proc hInst: DWORD, hPrevInst: DWORD,
CmdLine: DWORD, CmdShow: DWORD
LOCAL wndclass: WNDCLASSEX
LOCAL Msg: MSG
mov wndclass.cbSize,sizeof WNDCLASSEX
mov wndclass.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
mov wndclass.lpfnWndProc,OFFSET WndProc
mov wndclass.cbClsExtra, 0
mov wndclass.cbWndExtra, 0
mov eax,hInst
mov wndclass.hInstance,eax
mov wndclass.hbrBackground,COLOR_WINDOW+ 1
mov wndclass.lpszMenuName, 0
mov wndclass.lpszClassName,OFFSET szClassName
invoke LoadIcon,hInst,NULL
mov wndclass.hIcon,eax
invoke LoadCursor, 0 ,IDC_ARROW
mov wndclass.hCursor,eax
mov wndclass.hIconSm, 0
invoke RegisterClassEx,ADDR wndclass
invoke CreateWindowEx,WS_EX_OVERLAPPEDWINDOW, ADDR szClassName,
ADDR szDisplayName,
WS_OVERLAPPEDWINDOW,
200 , 50 , 500 , 300 ,
0 , 0 ,
hInst, 0
mov hWnd,eax
invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd
MessageLoop:
invoke GetMessage,ADDR Msg, 0 , 0 , 0
cmp eax, 0
je ExitProgram
invoke TranslateMessage,ADDR Msg
invoke DispatchMessage,ADDR Msg
jmp MessageLoop
ExitProgram:
mov eax,Msg.wParam
ret
WinMain endp
; ===============================================
WndProc proc hWin: DWORD, uMsg: DWORD, wParam: DWORD, lParam: DWORD
.if uMsg == WM_DESTROY
invoke PostQuitMessage, 0
.else
invoke DefWindowProc,hWin,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
; ===============================================
end start
在命令行程序中进入在bin目录下输入 ml /coff ..\MyDocument\demo.asm
显示如下
在bin文件夹中可以看到demo.obj和demo.exe文件生成,打开demo.exe文件显示如下,那么你的环境就已经配置成功了