-2

So right now I am attempting to code minesweeper for a class. The part that I am stuck on is attempting to update all the grid after a user clicks on a blank tile.

Unfortunately whenever I try to run my code it just crashes. Can anyone help?

whitespaceClick PROC uses eax ebx ecx esi edi
call convertXYval
mov GridValues[eax], 250
call updateSurroundings

mov edi, 0
call clearAroundArray
call fillAroundArray

cmp edi, 0 ;if no spaces around it
je done
mov ecx, 8
mov esi, 0

store:
  cmp aroundArray[esi], 3
  jne skp
    mov bh, aroundArrayX[esi]
    mov bl, aroundArrayY[esi]
    push bx
  skp:
  inc esi
loop store

mov eax, 0
mov ecx, edi
update:
  pop ax
  mov X, ah
  mov Y, al
  call whitespaceClick
loop update
Done:
ret
whitespaceClick ENDP

Notes:

  • X and Y are variables of type BYTE that are used to keep track of the current location

  • convertXYval is a procedure that converts the x,y values into a single value that corresponds to the location in an array

  • fillAroundArray will fill an 8 element array with values (0,1,2,3) and also returns the number of spaces surrounding the current location in edi

  • whitespaceClick updates the surrounding values to numbers if they are touching bombs

Jake Hall
  • 17
  • 4
  • 1
    Learn to use a debugger. It is extremely suspicious that you have 2 loops with different iteration counts where the first pushes values and the second pops. Also comment your code, especially if you want others to help. See also [mcve]. – Jester Nov 08 '15 at 22:45
  • I have a valid reason for the two loops The purpose of the first loop is to find the xy values adjacent that contain whitespace and store them The second loop pulls the values one at a time, sets x and y to them then calls the whitespaceClick proc recursaivly – Jake Hall Nov 08 '15 at 22:50
  • That may be, but it's not obvious that the two loops will process the same amount of items and if the stack becomes unbalanced, a crash will result. The first loop runs 8 times but only conditionally pushes a value and the second runs `edi` times, whatever `edi` is. Also it isn't obvious why you can't just `call whitespaceClick` in the first loop without storing stuff on the stack. As I said, comment your code (sensibly) and use a debugger. – Jester Nov 08 '15 at 22:53
  • @Jester edi contains the number of white spaces adjacent, I mention that In my notes. The two loops will iterate the same number of times – Jake Hall Nov 08 '15 at 22:56
  • _"The two loops will iterate the same number of times"_ There's no way for us to verify that with the code you've shown us. – Michael Nov 09 '15 at 07:07
  • 1
    It would help if you provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). I don't think it possible to answer this question without seeing the surrounding code. A minimal complete running program that produces the problem would be ideal. – Michael Petch Nov 11 '15 at 19:51

1 Answers1

0

Both loops will iterate the same number of times only if fillAroundArray knows that white space is encoded with number 3. To verify...

 whitespaceClick PROC uses eax ebx ecx esi edi

Does tagging your procedure with uses ... automatically means that those registers are preserved?
If it doesn't then you need to save/restore the value of the loop counter around your recursive call.

 mov  Y, al
 push ecx
 call whitespaceClick
 pop  ecx
loop update

In 16-bit code using ECX for a small loop counter would be silly. So I conclude this is 32-bit code and then I would advice to change both push bx and pop ax into dword variants. This would maintain a dword aligned stack throughout those recursive calls.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • _USES_ on a _PROC_ statement in _MASM/TASM_ assembly tells the assembler that these registers need to be preserved. They are saved and restored on the stack automatically in the functions prologue and epilogue code. – Michael Petch Nov 11 '15 at 19:45