flat assembler
Message board for the users of flat assembler.

Index > Windows > Simple code crashes. Is it my system or the code?

Author
Thread Post new topic Reply to topic
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg 14 Aug 2013, 04:54
I just wanted to start an OpenGL project and guess what? I've run into a problem I never had before. And it's not with OpenGL, it's with Windows 7(64 bit)! I wrote the followng code(it's actually bigger, but I stripped it down as much as I can)
Code:
format PE GUI 6.0
include "win32axp.inc"

        invoke GetModuleHandle, 0
        mov [wc.hInstance], eax
        invoke RegisterClass, wc
        invoke CreateWindowEx, WS_EX_WINDOWEDGE, eax, title, WS_CAPTION+WS_SYSMENU+WS_VISIBLE, 300, 300, 300, 300, 0, 0, 0, 0

msg_loop:
        invoke GetMessage, msg, 0, 0, 0
        invoke TranslateMessage, msg
        invoke DispatchMessage, msg
        jmp msg_loop

exit:
        invoke  ExitProcess, 0

proc WindowProc, hWnd, uMsg, wParam, lParam
     cmp [uMsg], WM_CLOSE
     je exit
     invoke DefWindowProc, [hWnd], [uMsg], [wParam], [lParam]
     ret

endp

msg MSG
title           db "Hello, OpenGL!",0
wc WNDCLASS 0,WindowProc,0,0,0,0,0,COLOR_WINDOW,0,title

data import
library kernel32,'KERNEL32.DLL',\
        user32,'USER32.DLL'

        include "api\kernel32.inc" 
        include "api\user32.inc"
end data    


It generates an APPCRASH. Now what's funny is that I completely gave up on the idea that I could find the problem, since I've been "debugging" this for 24 hours straight!!! At least I have some informaton: when running this in ollydbg the program doesn't crash, in fact it works. Then I decided to post this code here and wait for a solution. So I changed db "Hello, OpenGL!",0 to db "Hello, APPCRASH!",0 (since the problem has nothing to do with OpenGL)and guess what? There is no APPCRASH! I was laughing my ass off. I am semi experienced in asm but never had this kind of error getting in my way. I'm certain this has to do with data alignment. I was playing around with the align directive, but it does nothing at all. Any help would be appreciated.
Post 14 Aug 2013, 04:54
View user's profile Send private message Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 14 Aug 2013, 06:30
this is wrong:
Code:
je exit    

the jump outside of procedure corrupts the stack
but I don't believe this causes crash because ExitProcess is called then immediately
anyway briefly looking at your sample I don't see anything suspicious there
seems you compiled your app as 32 bit - if you compile as 64 bit then also take care of aligning stack at 16 (no need for 32 bits)
try to search some examples here in forum, e.g. use this search pattern:
DefWindowProc

being stuck for 24 hours with so simple thing is too depressive
Post 14 Aug 2013, 06:30
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 767
tthsqe 14 Aug 2013, 06:37
Code:
format PE GUI 6.0
include "win32axp.inc"

        invoke GetModuleHandle, 0 
        mov [wc.hInstance], eax 
        invoke RegisterClass, wc 
        invoke CreateWindowEx, WS_EX_WINDOWEDGE, class, title, WS_CAPTION+WS_SYSMENU+WS_VISIBLE, 300, 300, 300, 300, 0, 0, 0, 0
                                               ; ^^^^ use class here

msg_loop: 
        invoke GetMessage, msg, 0, 0, 0
        test   eax,eax                        ;
        jz     exit                           ; must check for exit !
        invoke TranslateMessage, msg 
        invoke DispatchMessage, msg 
        jmp msg_loop 

exit: 
        invoke  ExitProcess, 0 

proc WindowProc hwnd,wmsg,wparam,lparam         ; < no comma after WindowProc
        cmp     [wmsg], WM_DESTROY
        je      .wmdestroy
        invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .finish
.wmdestroy:
        invoke  PostQuitMessage,0
        xor     eax,eax

.finish:
        ret

endp 

msg MSG
wc WNDCLASS 0,WindowProc,0,0,0,0,0,COLOR_WINDOW,0,class
class  db 'opengl',0                                 ; <<< class here
title  db "Hello, OpenGL!",0


data import 
library kernel32,'KERNEL32.DLL',\ 
        user32,'USER32.DLL' 

        include "api\kernel32.inc"  
        include "api\user32.inc" 
end data    
Post 14 Aug 2013, 06:37
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4039
Location: vpcmpistri
bitRAKE 14 Aug 2013, 06:41
Do you still get the error if you align wc. I'm not able to produce the error here, but I've had similar errors with passing unaligned structures. I'm sure the convention is to align by largest item in structure. Sure it works sometimes without alignment, but it doesn't have to.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 14 Aug 2013, 06:41
View user's profile Send private message Visit poster's website Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg 14 Aug 2013, 08:27
The error had nothing to do with aligning as far as I can tell...
I made a mistake with mixing up the 2 strings, and also, giving the return value of RegisterClass to the lpClassName instead of putting the class name pointer there. Thank you tthsqe for the help!
Post 14 Aug 2013, 08:27
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.