Search code examples
assemblyx86masm32

x86 assembly masm32 window application stops responding


I have managed to create a window (thanks to everyone here at SO!), but whenever it runs, the window forms, but it immediately breaks and a window pops up saying "test.exe has stopped responding" (test.exe being the name of the program).

Here is the code:

.386 
.model flat,stdcall 
option casemap:none
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

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

 .data 
 ClassName db "SimpleWinClass",0 
 AppName  db "If you get this, it worked.",0 
 char WPARAM 20h                       

.data? 
hInstance HINSTANCE ? 
CommandLine LPSTR ?

.code 
start: 
push NULL
call GetModuleHandle 
mov    hInstance,eax 
call GetCommandLine
mov CommandLine,eax
push SW_SHOWDEFAULT
push CommandLine
push NULL
push hInstance
call WinMain
push eax
call ExitProcess

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  hInst 
pop   wc.hInstance 
mov   wc.hbrBackground,COLOR_WINDOW+1 
mov   wc.lpszMenuName,NULL 
mov   wc.lpszClassName,OFFSET ClassName 
push IDI_APPLICATION
push NULL
call LoadIcon
mov   wc.hIcon,eax 
mov   wc.hIconSm,eax 
push IDC_ARROW
push NULL
call LoadCursor
mov   wc.hCursor,eax 
lea eax, wc
push eax
call RegisterClassEx
push NULL
push hInst
push NULL
push NULL
push CW_USEDEFAULT
push CW_USEDEFAULT
push CW_USEDEFAULT
push CW_USEDEFAULT
push WS_OVERLAPPEDWINDOW
lea eax, AppName
push eax
lea eax, ClassName
push eax
push NULL
call CreateWindowEx
    mov   hwnd,eax 
push SW_SHOWNORMAL
push hwnd
call ShowWindow  
  push hwnd
  call UpdateWindow
     .WHILE TRUE 
         push 0
         push 0
         push NULL
         lea eax, msg
         push eax
         call GetMessage
         .BREAK .IF (!eax)
        lea eax, msg
        push eax
        call TranslateMessage
        lea eax, msg
        call DispatchMessage
     .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 
    push NULL
    call PostQuitMessage
    .ELSEIF uMsg==WM_CHAR 
        push wParam 
        pop  char 
    push TRUE
    push NULL
    push hWnd
    call InvalidateRect
     .ELSEIF uMsg==WM_PAINT 
    lea eax, ps
    push eax
    push hWnd
    call BeginPaint
        mov    hdc,eax 
    push 1
    lea eax, char
    push eax
    push 0
    push 0
    push hdc
    call TextOut
    lea eax, ps
    push eax
    push hWnd
    call EndPaint
    .ELSE 
    push lParam
    push wParam
    push uMsg
    push hWnd
    call DefWindowProc
        ret 
    .ENDIF 
        xor    eax,eax 
        ret 
    push 0
    call ExitProcess
WndProc endp 
end start  

Why does it keep breaking? I've seen the same thing occur when I don't put in call ExitProcess but I have this time, so why would it keep shutting down?

Thanks in advance


Solution

  • Since your a beginner and using MASM, I HIGHLY recommend you NOT using push/calls!!!! You gain NOTHING in not using INVOKE besides bugs.

    If you have used invoke, ml would of caught the problem.

    Are you missing something here?

    lea eax, msg
    call DispatchMessage
    

    Your forgetting to push eax!

    Also, for your sake and everyone else that has to read your code, please use an indentation style. Usually there is 4 spaces between the left margin and mnuemonics.