;-searchfile.asm------------------------------------------------------------- ; x86 - Dos & Win32 - Assembly Language Programming ; ; ; ; Written by: John A Lyons (megablast) ; ; Email : asm@megablast.8k.com ; ; Page : http://www.asmsource.8k.com/ ; ; Compiler : Masm32 v6.13 Microsoft Macro Assembler ; ; Date : 21-Feb-2001 ; ; Purpose : Searches throught a file for all instances of a string, and ; ; prints out that line. ; ; ; ;---------------------------------------------------------------------------- ; Compile with nmake .386 .MODEL FLAT , STDCALL include windows.inc include user32.inc include kernel32.inc include gdi32.inc includelib user32.lib includelib kernel32.lib includelib gdi32.lib EXTRN wsprintfA:PROC .const IDI_ICON1 equ 5 .data ClassName db "SimpleWinClass",0 AppName db "Search File",0 filename db "search.asm",0 searchit db "invoke",0 num db "Line %lu",0 buff db 200 dup(0) hInstance HINSTANCE ? CommandLine LPSTR ? hwnd HWND ? .code WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:SDWORD LOCAL wc:WNDCLASSEX LOCAL msg:MSG mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_BYTEALIGNWINDOW 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, hInstance, IDI_ICON1 mov wc.hIcon,eax mov wc.hIcon,eax mov wc.hIconSm,0 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, 300,100, ;size 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 mov eax,hWnd mov hwnd,eax mov eax,uMsg .IF eax==WM_DESTROY invoke PostQuitMessage,NULL .ELSEIF eax==WM_PAINT call openfile .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ret .ENDIF xor eax,eax ret WndProc endp openfile proc LOCAL filehand:DWORD LOCAL filesize:DWORD LOCAL mem:DWORD LOCAL memhand:DWORD LOCAL hdc:HDC LOCAL ps:PAINTSTRUCT LOCAL filebuff:OFSTRUCT LOCAL temp:DWORD LOCAL x:DWORD invoke BeginPaint,hwnd, ADDR ps mov hdc,eax ; invoke TextOut,hdc,0,0,ADDR TestString,(SIZEOF TestString) -1 invoke OpenFile, ADDR filename,ADDR filebuff,OF_READ cmp eax,-1 je notopen mov filehand,eax invoke TextOut,hdc,0,0,ADDR filename,(SIZEOF filename) -1 invoke GetFileSize,filehand,addr temp mov filesize,eax invoke GlobalAlloc,GMEM_MOVEABLE + GMEM_ZEROINIT,filesize mov memhand,eax invoke GlobalLock,eax mov [mem],eax invoke ReadFile,filehand,mem,filesize,addr temp,NULL mov [x],32 ;--------- Search Routine mov esi,[mem] mov ecx,[temp] lea edi,searchit mov al,[edi] xor ebx,ebx regosearch: mov ah,[esi] cmp ah,al jne notyet internal: inc edi mov al,[edi] or al,al jz gotit inc esi cmp [esi],al jne nicetry loop internal jmp enditall gotit: push edi push esi push ecx push ebx push ebx push offset num push offset buff call wsprintfA add esp,0ch invoke lstrlen,ADDR buff invoke TextOut,hdc,0,x,ADDR buff,eax mov ecx,10 searchforstart: dec esi cmp [esi],byte ptr 10 je searchforstart1 loop searchforstart searchforstart1: inc esi push esi xor ecx,ecx searchforend: inc esi inc ecx cmp [esi],byte ptr 13 jne searchforend pop esi invoke TextOut,hdc,60,x,esi,ecx add [x],16 pop ebx pop ecx pop esi pop edi nicetry: lea edi,searchit mov al,[edi] jmp notreturn notyet: cmp ah,10 jne notreturn inc ebx notreturn: inc esi dec cx cmp cx,0 je enditall jmp regosearch enditall: invoke GlobalUnlock,memhand invoke GlobalFree,memhand invoke CloseHandle,filehand notopen: invoke EndPaint,hwnd, ADDR ps ret openfile endp start: invoke GetModuleHandle, NULL mov hInstance,eax invoke GetCommandLine invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT invoke ExitProcess,eax end start