flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem with 'proc'

Author
Thread Post new topic Reply to topic
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 12:44
Well, guess what! Twisted Evil , I'm having a problem with the 'proc' macro.
when I call a no-argument function with the standard call instruction my program crashes, but when I add an argument to the function and use stdcall, it works correctly. Example:

crashes:
Code:
call myfunction
.
.
proc myfunction
    


works:
Code:
stdcall myfucntion, dummy
.
.
proc myfunction, dummy
    
Post 04 Jul 2005, 12:44
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 04 Jul 2005, 13:09
I have no such problem here, please post the minimum source that reproduces the problem.
Post 04 Jul 2005, 13:09
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 14:19
I'll try and explain what is happening as best as I can, because my crash dump doesn't help, i had to use the message box to debug.

my program goes through the normal window creation routine ok, and Executes GAMEINIT ok until the ret, which is where somehow, to the best I can figure, the return address is wrong.

Code:
     ;Store the handle (pointer) to the window.
     mov     [mainhwnd], eax

     call GAMEINIT  ;<- crash happens on ret from this function 

     ;Debug Info Code <---- Never gets executed
     ;mov     byte [strbuffer],0
     ;invoke  CnvtItoA64, strbuffer, 0, eax
     ;invoke  ShowCursor,TRUE
     ;invoke  MessageBox, [mainhwnd], strbuffer, strbuffer, MB_OK
     ;invoke  ShowCursor,FALSE
    


Here is the code:
Code:
proc GAMEINIT
     ;This is the function where you do all the initialization
     ;for your game.

     ;create directdraw object and set the screen mode
     invoke  DDInit, [mainhwnd], SCREENWIDTH, SCREENHEIGHT, SCREENBPP
     cmp     eax,DD_OK
     jne     GAMEINIT_error

     ;load in all sprite frames for game at once,
     ;then convert to a directdraw surface for blitting
     invoke  DDLoadBmpImage, starferfile
     invoke  DDCreateSurfaceFromBmp, 0
     invoke  DDUnLoadBmpImage

     ;set clip region of Directdraw commands
     invoke  DDSetClipRectangles, screenrect, 1

     ;set the clip reigion to full screen
     invoke  DDSetSoftClipRegion, 0, 0, SCREENWIDTH, SCREENHEIGHT

     ;set software drawing to 8-bit palettized mode
     invoke  DDSetDrawMode8, 0
     finit

     ;initialize Star animation structures
     call    InitStars

     ;initialize rock animation structures
     call    InitRocks

     ;initialize starferret animation structures (main character)
     call    InitStarferret

     ;initialize starferret plasma  animation structures (gun fire)
     call    InitPlasma

     ;initialize bursts animation structures (explosions)
     call    InitBursts

     ;Starferret Game Introduction Introduction
     call    DoIntro

     ;Intialize 3 large rocks to start
     ;set there speed and direction
     mov     [index],0
     .repeat
             ;random float between -2 to 2 (X - direction and speed)
             fld     [fxvhi]
             fld     [fxvlow]
             invoke  RndSetFRange
             invoke  RndFRange
             fstp    [tempx1]

             ;random float between 0 and 4 (Y - direction and speed)
             fld     [fyvhi]
             fldz
             invoke  RndSetFRange
             invoke  RndFRange
             fadd    [fxvhi]
             fstp    [tempy1]

             ;random integer between 0 and SCREENWIDTH-1
             invoke  RndInt32Range,0,SCREENWIDTH-1
             mov     [tempx0],eax
             mov     [tempy0],0
             fild    [tempx0]
             fild    [tempy0]
             fstp    [tempy0]
             fstp    [tempx0]

             ;get the rock ready for drawing
             stdcall StartRock, [tempx0], [tempy0], ROCKLARGE, [tempx1], [tempy1]

             ;loop 3 times for 3 rocks
             inc     [index]
     .until  [index], e, 3

     clc
     ret

GAMEINIT_error:
     stc
     ret
endp     


The 'StartRock' function seems to work good because I call it three times and no crashes. But right after that, it crashes on the 'ret' because my message box code (just below the call to the GAMEINIT function) never gets executed. And as explained above, when I add a dummy variable to the 'GAMEINIT' function and use stdcall it returns correctly.

Here is the startrock code:
Code:
 proc StartRock  x, y, size, xv, yv
     ;this function starts a rock up.
     ;scan for a rock to initialize
     mov     [index],0
     mov     [saddr],0

StartRock_l2:
     mov     ecx,[saddr]
     mov     eax,[ecx+rocks.state]
     and     eax, DDSPRITEON
     jne     StartRock_next
             finit
             mov     ecx,[saddr]
             mov     eax,[ecx+rocks.ivars]
             cmp     eax,[size]
             jne     StartRock_next

             invoke  RndInt32Range, 0, 4
             mov     ecx,[saddr]
             add     eax,1
             mov     [ecx+rocks.animspeed],eax
             fld     [xv]
             fld     [yv]
             fstp    [ecx + rocks.dyv]
             fstp    [ecx + rocks.dxv]
             fld     [x]
             fld     [y]
             fstp    [ecx + rocks.fy]
             fstp    [ecx + rocks.fx]

             ;set strength of rock
             mov     eax,[size]
             cmp     eax,ROCKLARGE
             jne     StartRock_strength1
                     invoke  RndInt32Range,0,99
                     add     eax,100
                     mov     ecx,[saddr]
                     mov     [ecx+rocks.ivars+4],eax
                     jmp     StartRock_strengthdn

             StartRock_strength1:
             cmp     eax,ROCKMEDIUM
             jne     StartRock_strength2
                     invoke  RndInt32Range,0,29
                     add     eax,40
                     mov     ecx,[saddr]
                     mov     [ecx + rocks.ivars + 4],eax
                     jmp     StartRock_strengthdn

             StartRock_strength2:
                     mov     ecx,[saddr]
                     mov     dword [ecx + rocks.ivars + 4], 10

             StartRock_strengthdn:
             mov     ecx,[saddr]
             or      [ecx + rocks.state], DDSPRITEON
             jmp     StartRock_dn

StartRock_next:
     add     [index],1
     add     [saddr],sizeof.ANIM
     cmp     [index],MAXROCKS
     jne     StartRock_l2

StartRock_dn:
     clc
     ret
endp
    


Last edited by madmatt on 04 Jul 2005, 14:39; edited 2 times in total
Post 04 Jul 2005, 14:19
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 04 Jul 2005, 14:28
The only difference between proc with dummy parameter and proc without any parameters is that the first one sets up the stack frame (does PUSH EBP/MOV EBP,ESP at startup and LEAVE on return), while the second one doesn't at all.


Last edited by Tomasz Grysztar on 04 Jul 2005, 14:33; edited 2 times in total
Post 04 Jul 2005, 14:28
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 14:31
I'll go ahead and edit and post the 'StartRock' code, because this could be the problem too.
Post 04 Jul 2005, 14:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 04 Jul 2005, 14:33
Maybe you are destroying EBP somewhere inside your procedure (this would lead to ESP being restored incorrectly).
Post 04 Jul 2005, 14:33
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 14:41
If I was, it would crash with the stdcall too, wouldn't it? It works with stdcall. I'll try ollydebug again And see if i can see what is going on.
Post 04 Jul 2005, 14:41
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 04 Jul 2005, 15:01
Well, it would crash with stdcall but not crash with call then - sorry, I've mistaken which one crashes for you.
However unless you provide the whole source I could compile and try I cannot help with finding the problem.
Post 04 Jul 2005, 15:01
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 16:55
Olly debug didn't help. I'll see if I can put together something you can download and re-compile and test yourself. I could be overwriting something, or the old macros let bad code work and the new macros won't. I don't know, anyways, give me a little while to gather everything together
Post 04 Jul 2005, 16:55
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 17:17
Here is the sources, the windows game programming for dummies chapter 12 example is giving me the problem. Just extract this archive into the c:\ directory and compile. If you cannot recreate the problem then don't fuss with it any further, I'll do a major upgrade to the code and see if that helps. Thanks!
MadMatt
Post 04 Jul 2005, 17:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 04 Jul 2005, 21:14
OK, found what the problem is: you were calling DDCreateSurfaceFromBmp with one argument, while is needs two, and therefore stack frame was restored inproperly.

With the new includes you can switch freely to WIN32AXP include to check out for such mistakes - but if you import some functions from DLLs you have to provide definitions of parameter count yourself.
Post 04 Jul 2005, 21:14
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 04 Jul 2005, 23:07
Well, your right, thanks! I'll have to keep my documentation more up to date. As you can see I still have some work to do. Another piece of good news is that your system was able to run it, besides my own! By the way, what system do you have, and what version of DirectX are you using?
Post 04 Jul 2005, 23:07
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 04 Jul 2005, 23:39
XP SP2, DirectX 9
Post 04 Jul 2005, 23:39
View user's profile Send private message Visit poster's website Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner 04 Jul 2005, 23:57
nice to hear u got XP already Smile
Post 04 Jul 2005, 23:57
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 05 Jul 2005, 00:01
Thanks to coconut.
Post 05 Jul 2005, 00:01
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 05 Jul 2005, 01:55
Did you get a new computer? Last time you mentioned having win95 and a very old computer.
Post 05 Jul 2005, 01:55
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 05 Jul 2005, 11:26
I have a new computer for quite a some time, the problem was that I had no license for any Win32 system for this new machine. After I wrote about this problem on the forums, the coconut sent me the spare original WinXP CDs he had.
Post 05 Jul 2005, 11:26
View user's profile Send private message Visit poster's website 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.