论坛有问题,那两句话竟然没出来.在这里补上:
汇编指令不是重点,不懂就查手册.
流程的理解和功能的分解是关键.
汇编指令不是重点,不懂就查手册.
流程的理解和功能的分解是关键.
天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
.386 .model flat,stdcall option casemap:none include windows.inc include kernel32.inc includelib kernel32.lib include user32.inc includelib user32.lib .data MsgBoxCaption db "我的Win32汇编",0 MsgBoxText db "Win32 Assembly is Great!",0 .code start: invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK invoke ExitProcess, NULL end start
.386 ;使用80386指令集 .model flat,stdcall option casemap:none include windows.inc include user32.inc include kernel32.inc includelib user32.lib includelib kernel32.lib ;WinMain函数声明 WinMain proto :DWORD,:DWORD,:DWORD,:DWORD ;======================数据段 .data ClassName db "SimpleWinClass",0 AppName db "我的汇编窗口",0 .data? hInstance HINSTANCE ? ;应用程序句柄 CommandLine LPSTR ? ;保存命令行传入的参数 ;========================== ;==========================代码段开始 .code start: invoke GetModuleHandle, NULL ;得到应用程序句柄 mov hInstance,eax invoke GetCommandLine ;得到命令行参数 mov CommandLine,eax ;调用主函数 invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ;退出程序 invoke ExitProcess,eax ;===================主过程======================= WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD ;注册窗口样式 LOCAL wc:WNDCLASSEX LOCAL msg:MSG LOCAL hwnd:HWND mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra,NULL mov wc.cbWndExtra,NULL push hInstance pop wc.hInstance mov wc.hbrBackground,COLOR_WINDOW+1 mov wc.lpszMenuName,NULL mov wc.lpszClassName,OFFSET ClassName invoke LoadIcon,NULL,IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm,eax invoke LoadCursor,NULL,IDC_ARROW mov wc.hCursor,eax invoke RegisterClassEx, addr wc ;创建窗口 INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\ WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\ CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\ hInst,NULL mov hwnd,eax ;显示窗口 invoke ShowWindow, hwnd,SW_SHOWNORMAL invoke UpdateWindow, hwnd ;进入消息循环 .WHILE TRUE invoke GetMessage, ADDR msg,NULL,0,0 .BREAK .IF (!eax) invoke TranslateMessage, ADDR msg invoke DispatchMessage, ADDR msg .ENDW mov eax,msg.wParam ret WinMain endp ;回调函数 WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM .IF uMsg==WM_DESTROY ;关闭消息 invoke PostQuitMessage,NULL .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ;调用系统默认的函数进行处理 ret .ENDIF xor eax,eax ret WndProc endp end start
.386 ;使用80386指令集 .model flat,stdcall option casemap:none include windows.inc include user32.inc include kernel32.inc includelib user32.lib includelib kernel32.lib include gdi32.inc includelib gdi32.lib ;WinMain函数声明 WinMain proto :DWORD,:DWORD,:DWORD,:DWORD ;======================数据段 .data ClassName db "SimpleWinClass",0 AppName db "我的汇编窗口",0 MyString db "我的第二个窗口程序",0 .data? hInstance HINSTANCE ? ;应用程序句柄 CommandLine LPSTR ? ;保存命令行传入的参数 hdc HDC ? ;========================== ;==========================代码段开始 .code start: invoke GetModuleHandle, NULL ;得到应用程序句柄 mov hInstance,eax invoke GetCommandLine ;得到命令行参数 mov CommandLine,eax ;调用主函数 invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ;退出程序 invoke ExitProcess,eax ;===================主过程======================= WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD ;注册窗口样式 LOCAL wc:WNDCLASSEX LOCAL msg:MSG LOCAL hwnd:HWND mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra,NULL mov wc.cbWndExtra,NULL push hInstance pop wc.hInstance mov wc.hbrBackground,COLOR_WINDOW+1 mov wc.lpszMenuName,NULL mov wc.lpszClassName,OFFSET ClassName invoke LoadIcon,NULL,IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm,eax invoke LoadCursor,NULL,IDC_ARROW mov wc.hCursor,eax invoke RegisterClassEx, addr wc ;创建窗口 INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\ WS_SYSMENU or WS_MINIMIZEBOX,CW_USEDEFAULT,\ CW_USEDEFAULT,200,200,NULL,NULL,\ hInst,NULL mov hwnd,eax ;显示窗口 invoke ShowWindow, hwnd,SW_SHOWNORMAL invoke UpdateWindow, hwnd invoke GetDC, hwnd mov hdc, eax ;进入消息循环 .WHILE TRUE invoke GetMessage, ADDR msg,NULL,0,0 .BREAK .IF (!eax) invoke TranslateMessage, ADDR msg invoke DispatchMessage, ADDR msg .ENDW mov eax,msg.wParam ret WinMain endp ;回调函数 WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM .IF uMsg==WM_DESTROY ;关闭消息 invoke PostQuitMessage,NULL .ELSEIF uMsg==WM_PAINT invoke TextOut, hdc, 0, 0, addr MyString, SIZEOF MyString .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ;调用系统默认的函数进行处理 ret .ENDIF xor eax,eax ret WndProc endp end start
.386 ;使用80386指令集 .model flat,stdcall option casemap:none include windows.inc include user32.inc include kernel32.inc includelib user32.lib includelib kernel32.lib include gdi32.inc includelib gdi32.lib ;WinMain函数声明 WinMain proto :DWORD,:DWORD,:DWORD,:DWORD ;======================数据段 .data ClassName db "SimpleWinClass",0 AppName db "我的汇编窗口",0 MyString db "我的第二个窗口程序",0 .data? hInstance HINSTANCE ? ;应用程序句柄 CommandLine LPSTR ? ;保存命令行传入的参数 ;hdc HDC ? ;========================== ;==========================代码段开始 .code start: invoke GetModuleHandle, NULL ;得到应用程序句柄 mov hInstance,eax invoke GetCommandLine ;得到命令行参数 mov CommandLine,eax ;调用主函数 invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ;退出程序 invoke ExitProcess,eax ;===================主过程======================= WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD ;注册窗口样式 LOCAL wc:WNDCLASSEX LOCAL msg:MSG LOCAL hwnd:HWND mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra,NULL mov wc.cbWndExtra,NULL push hInstance pop wc.hInstance mov wc.hbrBackground,COLOR_WINDOW+1 mov wc.lpszMenuName,NULL mov wc.lpszClassName,OFFSET ClassName invoke LoadIcon,NULL,IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm,eax invoke LoadCursor,NULL,IDC_ARROW mov wc.hCursor,eax invoke RegisterClassEx, addr wc ;创建窗口 INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\ WS_SYSMENU or WS_MINIMIZEBOX,CW_USEDEFAULT,\ CW_USEDEFAULT,200,200,NULL,NULL,\ hInst,NULL mov hwnd,eax ;显示窗口 invoke ShowWindow, hwnd,SW_SHOWNORMAL invoke UpdateWindow, hwnd ;狂闪的原因:在这里得到了DC ; invoke GetDC, hwnd ;mov hdc, eax ;进入消息循环 .WHILE TRUE invoke GetMessage, ADDR msg,NULL,0,0 .BREAK .IF (!eax) invoke TranslateMessage, ADDR msg invoke DispatchMessage, ADDR msg .ENDW mov eax,msg.wParam ret WinMain endp ;回调函数 WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM LOCAL hdc:HDC LOCAL ps:PAINTSTRUCT .IF uMsg==WM_DESTROY ;关闭消息 invoke PostQuitMessage,NULL .ELSEIF uMsg==WM_PAINT invoke BeginPaint,hWnd, addr ps ;在这里得到DC就不闪了 mov hdc,eax invoke TextOut, hdc, 0, 0, addr MyString, SIZEOF MyString .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ;调用系统默认的函数进行处理 ret .ENDIF xor eax,eax ret WndProc endp end start
.386 ;使用80386指令集 .model flat,stdcall option casemap:none include windows.inc include user32.inc include kernel32.inc includelib user32.lib includelib kernel32.lib include gdi32.inc includelib gdi32.lib ;WinMain函数声明 WinMain proto :DWORD,:DWORD,:DWORD,:DWORD ;======================数据段 .data ClassName db "SimpleWinClass",0 AppName db "我的汇编窗口",0 MyString db "我的第二个窗口程序",0 Date_Format db "yyyy年MM月dd日",0 ;日期格式_1 .data? hInstance HINSTANCE ? ;应用程序句柄 CommandLine LPSTR ? ;保存命令行传入的参数 ;hdc HDC ? ;========================== ;==========================代码段开始 .code start: invoke GetModuleHandle, NULL ;得到应用程序句柄 mov hInstance,eax invoke GetCommandLine ;得到命令行参数 mov CommandLine,eax ;调用主函数 invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT ;退出程序 invoke ExitProcess,eax ;===================主过程======================= WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD ;注册窗口样式 LOCAL wc:WNDCLASSEX LOCAL msg:MSG LOCAL hwnd:HWND mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW or CS_VREDRAW mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra,NULL mov wc.cbWndExtra,NULL push hInstance pop wc.hInstance mov wc.hbrBackground,COLOR_WINDOW+1 mov wc.lpszMenuName,NULL mov wc.lpszClassName,OFFSET ClassName invoke LoadIcon,NULL,IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm,eax invoke LoadCursor,NULL,IDC_ARROW mov wc.hCursor,eax invoke RegisterClassEx, addr wc ;创建窗口 INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\ WS_SYSMENU or WS_MINIMIZEBOX,CW_USEDEFAULT,\ CW_USEDEFAULT,200,200,NULL,NULL,\ hInst,NULL mov hwnd,eax ;显示窗口 invoke ShowWindow, hwnd,SW_SHOWNORMAL invoke UpdateWindow, hwnd ;狂闪的原因:在这里得到了DC ; invoke GetDC, hwnd ;mov hdc, eax ;进入消息循环 .WHILE TRUE invoke GetMessage, ADDR msg,NULL,0,0 .BREAK .IF (!eax) invoke TranslateMessage, ADDR msg invoke DispatchMessage, ADDR msg .ENDW mov eax,msg.wParam ret WinMain endp ;回调函数 WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM LOCAL hdc:HDC LOCAL ps:PAINTSTRUCT local TimeStruct:SYSTEMTIME ;时间结构 local ShowDateStr[128]:byte ;显示日期缓冲区 .IF uMsg==WM_DESTROY ;关闭消息 invoke PostQuitMessage,NULL .ELSEIF uMsg==WM_PAINT invoke BeginPaint,hWnd, addr ps ;在这里得到DC就不闪了 mov hdc,eax invoke GetSystemTime,addr TimeStruct invoke GetDateFormat,NULL,0,addr TimeStruct, addr Date_Format, addr ShowDateStr,sizeof ShowDateStr invoke TextOut, hdc, 40, 50, addr ShowDateStr, 14 .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ;调用系统默认的函数进行处理 ret .ENDIF xor eax,eax ret WndProc endp end start