;-view.asm-v0.1-------------------------------------------------------------- ; x86 - Dos & Win32 - Assembly Language Programming ; ; ; ; Written by: John A Lyons ; ; Email : sysop@megablast.8k.com ; ; Page : http://www.asmsource.8k.com/ ; ; Compiler : Masm32 v6.13 Microsoft Macro Assembler ; ; Date : 26-Jan-2001 ; ; Purpose : View the contents of a file, in Text, Hex and Dump. ; ; ; ; ; ; ; ; ; ; ; ;---------------------------------------------------------------------------- ; Compile with nmake ; .386 .MODEL FLAT , STDCALL include windows.inc include user32.inc include kernel32.inc include gdi32.inc include comctl32.inc include comdlg32.inc includelib user32.lib includelib kernel32.lib includelib gdi32.lib includelib comctl32.lib includelib comdlg32.lib EXTRN wsprintfA:PROC .const IDM_NEW equ 0c0h IDM_OPEN equ 0c1h IDM_SAVE equ 0c2h .data ClassName db "SimpleWinClass",0 AppName db "Open",0 TestString db "Toolbar",0 num db "%lu",0 hInstance HINSTANCE ? CommandLine LPSTR ? hwnd HWND ? toolbarhand DWORD ? temp DWORD ? counter DWORD ? filesize DWORD ? mem DWORD 0 memhand dword ? buff db 200 dup(0) fname db 200 dup(0) sizeread dword ? hsize dword ? vsize dword ? hdc dword ? retno dword ? dtype db 0ffh hrows dword 0,0 hlen dword 0 ofn OPENFILENAME <> FilterString db "All Files",0,"*.*",0 db "Text Files",0,"*.txt",0 db "Asm Files",0,"*.asm",0,0 toolinfo dd 6h,IDM_NEW db TBSTATE_ENABLED db TBSTYLE_BUTTON dw 0 dd 0,0 dd 4h,IDM_OPEN db TBSTATE_ENABLED db TBSTYLE_BUTTON dw 0 dd 0,0 dd 7h,IDM_SAVE db TBSTATE_ENABLED db TBSTYLE_BUTTON dw 0 dd 0,0 .code WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:SDWORD LOCAL wc:WNDCLASSEX LOCAL msg:MSG LOCAL filehand1:HANDLE mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_HREDRAW ;or CS_VREDRAW ;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,NULL,IDI_APPLICATION 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, 500,400, ;size NULL,NULL,\ hInst,NULL mov hwnd,eax push 14h push 14h push 14h push 0h push 0h ; push 6h push 3h push OFFSET toolinfo push 80h push hInstance ; push 14 push 4h push 300h push 54000521h push hwnd call CreateToolbarEx mov toolbarhand,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 count proc push esi mov eax,0 noc2: cmp [esi],byte ptr 0 je noc1 inc esi inc eax jmp noc2 noc1: mov [counter],eax pop esi ret count endp WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM LOCAL ps:PAINTSTRUCT LOCAL filehand1:HANDLE LOCAL scr:SCROLLINFO mov eax,uMsg .IF eax==WM_DESTROY invoke CloseHandle,filehand1 invoke PostQuitMessage,NULL .ELSEIF eax==WM_CREATE mov ofn.lStructSize,SIZEOF ofn push hWnd pop ofn.hWndOwner push hInstance pop ofn.hInstance mov ofn.lpstrFilter, OFFSET FilterString mov ofn.lpstrFile, OFFSET fname mov ofn.nMaxFile,200 .ELSEIF eax==WM_PAINT invoke BeginPaint,hWnd, ADDR ps mov hdc,eax cmp dtype,0ffh je nodisppaint call displayfile nodisppaint: invoke EndPaint,hWnd, ADDR ps ;------------------------------------------------------------------- ; Deal with window resizing .ELSEIF eax==WM_SIZE invoke GetDC,hWnd mov hdc,eax mov eax,lParam and eax,0ffffh ; shr eax,3 ; inc eax mov hsize,eax ; mov scr.nPage,eax ; mov scr.cbSize,SIZEOF scr ; mov scr.fMask,SIF_PAGE ; invoke SetScrollInfo,hwnd,SB_HORZ,addr scr,TRUE mov eax,lParam shr eax,16 shr eax,4 inc eax mov vsize,eax ; mov scr.nPage,eax ; mov scr.cbSize,SIZEOF scr ; mov scr.fMask,SIF_PAGE ; invoke SetScrollInfo,hwnd,SB_VERT,addr scr,TRUE invoke SendMessageA,toolbarhand,421h,0,0 mov eax,[hsize] push eax push offset num push offset buff call wsprintfA add esp,0ch lea esi,buff call count invoke TextOut,hdc,250,32,esi,counter invoke EndPaint,hWnd, ADDR ps ;------------------------------------------------------------------- ; Deal with commands .ELSEIF eax==WM_COMMAND invoke GetDC,hWnd mov hdc,eax mov eax,wParam ;.if lParam==0 .if ax==IDM_OPEN invoke CloseHandle,filehand1 mov ofn.Flags, OFN_FILEMUSTEXIST or \ OFN_PATHMUSTEXIST or OFN_LONGNAMES or\ OFN_EXPLORER or OFN_HIDEREADONLY invoke GetOpenFileName, ADDR ofn cmp eax,TRUE jne notop1 invoke CreateFile,ADDR fname,\ GENERIC_READ ,0,\ NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,\ NULL cmp eax,INVALID_HANDLE_VALUE je notop1 mov filehand1,eax invoke SetWindowText,hdc,buff invoke GetFileSize,filehand1,addr temp mov filesize,eax ; push eax ; push offset num ; push offset buff ; call wsprintfA ; add esp,0ch ; lea esi,buff ; call count ; invoke TextOut,hdc,0,70,esi,counter cmp memhand,0 je skipclose invoke GlobalUnlock,memhand invoke GlobalFree,memhand skipclose: invoke GlobalAlloc,GMEM_MOVEABLE + GMEM_ZEROINIT,filesize mov memhand,eax invoke GlobalLock,eax mov mem,eax mov esi,eax mov [esi],dword ptr 1 invoke ReadFile,filehand1,mem,filesize,addr sizeread,NULL push edi mov edi,mem mov ecx,filesize xor eax,eax nod1: cmp [edi],byte ptr 0dh jne nod inc eax nod: inc edi loop nod1 mov retno,eax pop edi invoke CloseHandle,filehand1 mov dtype,1 notop1: .endif ;.endif ; push eax ; push offset num ; push offset buff ; call wsprintfA ; add esp,0ch ; lea esi,buff ; call count ; invoke TextOut,hdc,0,50,esi,counter call displayfile invoke EndPaint,hWnd, ADDR ps .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ret .ENDIF xor eax,eax ret WndProc endp hex16 proc LOCAL ypos:DWORD LOCAL zpos:DWORD mov ypos,esi push esi mov esi,72 mov zpos,esi mov eax,hlen mov ebx,21 mul ebx add eax,8 add [zpos],eax hexl1: push ecx mov al,[edi] mov buff+20,al call hex2 push esi lea esi,buff mov [esi+2],word ptr 32 call count pop esi invoke TextOut,hdc,esi,ypos,addr buff,counter add esi,20 mov [buff+21],byte ptr 32 invoke TextOut,hdc,zpos,ypos,addr buff+20,2 add zpos,14 pop ecx inc edi loop hexl1 pop esi ret hex16 endp displayfile proc LOCAL vlen:DWORD cmp dtype,0ffh je notloaded push edi push esi push ebp cmp dtype,1 jne typenot1 mov eax,[hsize] sub eax,72+12 mov ebx,34 xor edx,edx div ebx mov [hlen],eax mov [hlen+4],edx push eax pop ebx mov eax,filesize xor edx,edx div ebx dec eax mov [hrows],eax mov [hrows+4],edx ; mov eax,[hlen] push eax push offset num push offset buff call wsprintfA add esp,0ch lea esi,buff call count invoke TextOut,hdc,0,32,esi,counter mov esi,46 mov edi,mem mov ecx,[vsize] cmp ecx,[hrows] jbe main1 mov ecx,[hrows] main1: push ecx mov eax,edi sub eax,mem call hex8 push esi lea esi,buff call count pop esi invoke TextOut,hdc,0,esi,addr buff,counter mov ecx,hlen call hex16 ; add edi,[hlen] add esi,16 pop ecx loop main1 jmp typeend typenot1: mov esi,56 mov edi,mem mov ecx,vsize sub ecx,2 cmp ecx,retno jb po1 mov ecx,retno po1: push ecx push edi xor ecx,ecx po3: cmp [edi],byte ptr 0dh je po2 inc edi inc ecx jmp po3 po2: pop ebx invoke TextOut,hdc,0,esi,ebx,ecx add edi,2 add esi,16 pop ecx loop po1 typeend: pop ebp pop esi pop edi notloaded: ret displayfile endp hex1 proc push ax and al,0fh cmp al,10 jb hex1ok add al,'A'-10 jmp hex2ok hex1ok: add al,'0' hex2ok: mov [esi],al pop ax ret hex1 endp hex8 proc push esi lea esi,buff add esi,7 mov [esi+1],byte ptr ':' mov [esi+2],byte ptr 0 mov ecx,8 hex8l: call hex1 dec esi shr eax,4 loop hex8l pop esi ret hex8 endp hex2 proc push esi lea esi,buff add esi,1 mov [esi+1],byte ptr 0 call hex1 dec esi shr eax,4 call hex1 pop esi ret hex2 endp start: invoke GetModuleHandle, NULL mov hInstance,eax invoke GetCommandLine invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT invoke ExitProcess,eax end start ;Note: Good programmers dont comment their code; It was hard to write, it should ; be hard to understand.