锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

汇编手记之第一个窗口程序

时间:2022-11-08 07:30:00 stps10l40ct二三极管

文章目录

    • 写在前面
    • 汇编代码
    • MakeFile
    • 窗口长这样
    • C 版本

写在前面

我又来写汇编了。最近微信逆向做头大。
如果汇编,直接从书中复制代码仍然是轻松愉快的。调用
基本上是Windows API,也不晦涩难懂。
而且,看看你敲的代码OD一行一行的执行,瞬间觉得自己理解了编程的本质(雾)。
编译后只有2个窗口程序.5kb左右,不知道怎么用C 实现编译出来会多大。

汇编代码

.386 .model flat,stdcall option casemap:none ;----------------------------------- ; Include ;----------------------------------- 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 ?  .const szClassName     db '我的窗口类',0 szCaptionMain   db 第一个窗口程序,0 szText db 'Win32汇编,简单且强力!',0 ;----------------------------------- ; 代码段 ;----------------------------------- .code ;----------------------------------- ; 窗口过程 ;----------------------------------- _ProcWinMain 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 _ProcWinMain endp ;---------------------------------- _WinMain 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 push hInstance pop @stWndClass.hInstance mov @stWndClass.cbSize,sizeof WNDCLASSEX mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW mov @stWndClass.lpfnWndProc,offset _ProcWinMain mov @stWndClass.hbrBackground,COLOR_WINDOW + 1 mov @stWndClass.lpszClassName,offset szClassName invoke RegisterClassEx,addr @stWndClass ;---------------------------------- ; 建立并显示窗口 ;---------------------------------- 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 .break .if eax == 0 invoke TranslateMessage,addr @stMsg invoke DispatchMessage,addr @stMsg .endw ret _WinMain endp ;---------------------------------- start: call _WinMain invoke ExitProcess,NULL ;---------------------------------- end start 

不知道怎么弄汇编高亮,凑合看吧。。

MakeFile

顺便把MakeFile也记录过来,多保留几份,嘿嘿。

OBJS = FirstWindow.obj
EXE = FirstWindow.exe
RES = 

LINK_FLAG = /subsystem:windows
ML_FLAG = /c /coff

$(EXE):$(OBJS)
    Link $(LINK_FLAG) $(OBJS) $(RES) /out:$(EXE)

.asm.obj:
    ml $(ML_FLAG) $<
	
.rc.res:  
    rc{ 
        1}lt
	
clean:
	del *.obj
	del *.res

窗口长这样

在这里插入图片描述

嗯!很不错。

C++版本

趁着刷课,把C++版本的搞出来了,Release版本大小10kb,不知道微软加了什么料进去,字符集也都一样的,看看差别:

#include
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

HINSTANCE hInstance = NULL;
HWND hwinMain = NULL;

const char* szClassName = "我的窗口类";
const char* szCaptionMain = "第一个窗口程序";
const char* szText = "珍爱生命远离CPP!";

VOID WINAPI _ProcWinMain(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 
        
	PAINTSTRUCT stPs = { 
         0 };
	RECT stRect = { 
         0 };
	HDC hDc = NULL;
	if (uMsg == WM_PAINT) { 
        
		hDc = BeginPaint(hWnd,&stPs);
		GetClientRect(hWnd, &stRect);
		DrawText(hDc,szText,-1,&stRect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hWnd, &stPs);
	}
	else if (uMsg == WM_CLOSE) { 
        
		DestroyWindow(hwinMain);
		PostQuitMessage(NULL);
	}
	else { 
        
		DefWindowProc(hWnd,uMsg,wParam,lParam);
		return;
	}
	return;
}

VOID _WinMain() { 
        
	WNDCLASSEX stWndClass = { 
         0 };
	MSG stMsg = { 
         0 };
	hInstance = GetModuleHandle(NULL);
	ZeroMemory(&stWndClass, sizeof(stWndClass));
	stWndClass.hCursor = LoadCursor(0, IDC_ARROW);
	stWndClass.hInstance = hInstance;
	stWndClass.cbSize = sizeof(WNDCLASSEX);
	stWndClass.style = (CS_HREDRAW | CS_VREDRAW);
	stWndClass.lpfnWndProc = (WNDPROC)_ProcWinMain;
	stWndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	stWndClass.lpszClassName = szClassName;
	RegisterClassEx(&stWndClass);
	hwinMain = CreateWindowEx(WS_EX_CLIENTEDGE, szClassName, szCaptionMain, WS_OVERLAPPEDWINDOW,
		100, 100, 600, 400,
		NULL, NULL, hInstance, NULL);
	ShowWindow(hwinMain, SW_SHOWNORMAL);
	UpdateWindow(hwinMain);
	while (1) { 
        
		if (!GetMessage(&stMsg, NULL, 0, 0)) { 
        
			break;
		}
		TranslateMessage(&stMsg);
		DispatchMessage(&stMsg);

	}
	return;
}

int main() { 
        
	_WinMain();
	ExitProcess(NULL);
	return 0;
}

样式和汇编版本完全一致。

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章