Source Code of Minesweeper Game in Assembly Language

Minesweeper is a single-participant puzzle video game. The goal of the game is to clear a rectangular board containing hidden “mines” or bombs without detonating any of them, with help from clues about the wide variety of neighboring mines in every subject. The game is performed by using revealing squares of the grid by clicking or in any other case indicating each square.

If a rectangular containing a mine is revealed, the player loses the game. If no mine is revealed, a digit is alternatively displayed within the square, indicating what number of adjacent squares comprise mines; if no mines are adjacent, the square becomes blank, and all adjacent squares might be recursively revealed.

Instruction: Remove the Comments in the Code to run successfully

Source Code:

.386
.model flat , stdcall
.stack 4096

GetStdHandle PROTO,a1:DWORD
ReadConsoleA PROTO,a1:dword,a2:ptr byte,a3:dword,a4:ptr dword,a5:ptr dword
WriteConsoleA PROTO,a1:DWORD,a2:PTR BYTE,a3:DWORD,a4:PTR DWORD,a5:DWORD

CreateFileA PROTO,a1:PTR BYTE,a2:DWORD,a3:DWORD,a4:DWORD,a5:DWORD,a6:DWORD,a7:DWORD
ReadFile PROTO,a1:DWORD,a2:PTR BYTE,a3:DWORD,a4:PTR DWORD,a5:PTR DWORD

GetTickCount PROTO
ExitProcess PROTO,a1:DWORD
SetConsoleCursorPosition PROTO,a1:DWORD,a2:DWORD
SetConsoleTextAttribute PROTO,a1:DWORD,a2:DWORD
.data
x dword ?
gridarray byte 1000 dup(?)
Random byte 100 dup(?)
seed dword ?
xyz dword ?
five byte 5
Count dword ?
space byte 20h,0
checknear dword ?
thatbyte dword ?
spacer byte 2dh,0
Rno byte 5 dup(?)
ten byte 10
Cno byte 5 dup(?)
rowcounter byte 2 dup(?)
Rnum dword ?
Cnum dword ?
inhandle dword ?
outhandle dword ?
msg1 byte “Row”,0
msg2 byte “Column”,0
found byte “End”,0
.code
generaterandom proc uses ebx edx
mov ebx, eax ; maximum value
mov eax, 343FDh
imul seed
add eax, 269EC3h
mov seed, eax ; save the seed for the next call
ror eax,8 ; rotate out the lowest digit
mov edx,0
div ebx ; divide by max value
mov eax, edx ; return the remainder
mov eax, edx ; return the remainder
ret
generaterandom endp

//Grid Procedure

Grid proc uses eax ebx ecx edx esi edi
invoke GetTickCount
mov esi , offset gridarray
mov ecx, 10
mov ebx , 10
l1:
l3:
mov eax , 20h
mov [esi] , eax
add esi , 4
mov eax , ‘+’
mov [esi],eax
add esi , 1
loop l3
l2:
dec ebx
mov ax , 0a0dh
mov [esi] , ax
add esi , 2
mov ecx , 10
add dx , 1
cmp ebx , 0
ja l1
jmp endwhile
endwhile:
mov dx , 0a0dh
inc esi
mov [esi] , dx
ret
grid endp

ToDecimal proc uses ebx ecx edx esi edi
push esi
cmp byte ptr [esi], ‘-‘
jne itspostive
inc esi
itspostive:
mov ebx, 0 ; x = 0
L1:
mov cl, [esi] ; Get a character
cmp cl, 0 ; check if its a null character
je endL1
mov edi, 10
mov eax, ebx
mul edi ; 10*x
sub cl, 48 ; (s[i]-‘0’)
; the character has been converted to its corrsponding number
movzx ecx, cl
add eax, ecx ; x*10 + (s[i]-48)
mov ebx, eax ; x = x*10 + (s[i]-48)
inc esi
jmp L1
endL1:
mov eax, ebx
pop esi
cmp byte ptr[esi], ‘-‘
jne itwaspositive
neg eax
itwaspositive:
ret
ToDecimal ENDP

//Checkloc Procedure function

checkloc proc uses ebx ecx edx esi
mov eax , Rnum
mov ebx , Cnum
mov ecx , 10
mul ecx
add eax , ebx
ret
checkloc endp

checknum proc uses esi ebx ecx eax edi edx
mov ebx , [esp+28]; Cnum
mov edi , [esp+32]; Rnum
mov Count , 0
mov esi , offset Random
call checkloc
mov thatbyte , eax
cmp byte ptr [esi+eax-1] , 4Dh
je endthecheck
push edi
push ebx
mov ecx , 3
sub ebx , 1
L1:
mov esi , offset Random
mov eax , edi
mul ten
add eax , ebx
add esi,eax
inc ebx
movzx ebp , byte ptr [esi-1]
cmp byte ptr [esi-1],4Dh
jne jmpoutL1
inc Count
jmpoutL1:
Loop L1
pop ebx
pop edi
push edi
push ebx
mov ecx , 3
sub ebx , 1
add edi , 1
L2:
mov esi , offset Random
mov eax , edi
mul ten
add eax , ebx
add esi , eax
inc ebx
movzx ebp , byte ptr [esi-1]
cmp byte ptr [esi-1],4Dh
jne jmpoutL2
inc Count
jmpoutL2:
Loop L2
pop ebx
pop edi
push edi
push ebx
mov ecx , 3
sub ebx , 1
sub edi ,1
L3:
mov esi , offset Random
mov eax , edi
mul ten
add eax , ebx
add esi , eax
inc ebx
movzx ebp , byte ptr [esi-1]
cmp byte ptr [esi-1],4Dh
jne jmpoutL3
inc Count
jmpoutL3:
Loop L3
pop ebx
pop edi
jmp successfullycheck
endthecheck:
mov Count , -1
successfullycheck:
ret 8
checknum endp

ToString PROC uses esi edi eax ebx ecx edx
mov edi, 10
mov ecx, 0 ; Ecx will have the digit count in the end
findnumofdigits:
cmp eax, 0
je exitfinddigits
mov edx, 0
div edi
inc ecx
jmp findnumofdigits
exitfinddigits:
pop eax
push ecx
mov byte ptr [esi+ecx] , 0 ; null terminate the string
savecharacters:
mov edx, 0
div edi
add dl, 48
mov [esi+ecx-1], dl
loop savecharacters
ret
ToString ENDP

//CPOS function

Cpos proc uses ebx eax
invoke GetStdHandle , -11
mov ebx , [esp+12]
rol ebx , 16
mov bx , 0
invoke SetConsoleCursorPosition , eax , ebx
ret
Cpos endp

endingstatement proc uses ebx eax
invoke GetStdHandle , -11
mov ebx , 200
rol ebx , 16
mov bx , 0
invoke SetConsoleCursorPosition , eax , ebx
ret
endingstatement endp

main proc
invoke GetStdHandle , -11
mov outhandle , eax
invoke GetStdHandle , -10
mov inhandle , eax
call Grid
invoke WriteConsoleA,outhandle,offset gridarray,lengthof gridarray, offset x,0
invoke GetTickCount
mov seed , eax
mov ecx , Lengthof Random
mov esi , offset Random
L1:
mov eax , 26
call generaterandom
add eax , 65
mov [esi],eax
inc esi
Loop L1
mov xyz , 10
whileloop:
push xyz
add xyz , 2
call Cpos
invoke WriteConsoleA,outhandle,offset msg1,lengthof msg1-1 , offset x,0
invoke ReadConsoleA,inhandle,offset Rno,lengthof Rno , offset x,0
mov ebx, x
mov Rno[ebx-2], 0
mov esi , offset Rno
call ToDecimal
mov Rnum , eax
cmp Rnum , 0
jnl nextCheck2
jmp whileloop
nextCheck2:
cmp Rnum , 9
jng checkRnum
jmp whileloop
checkRnum:
invoke WriteConsoleA,outhandle,offset msg2,lengthof msg2-1 , offset x,0
invoke ReadConsoleA,inhandle,offset Cno,lengthof Cno , offset x,0
mov ebx, x
mov Cno[ebx-2], 0
mov esi , offset Cno
call ToDecimal
mov Cnum , eax
cmp Cnum , 0
jnl nextcheck1
jmp whileloop
nextcheck1:
cmp Cnum , 9
jng checkCnum
jmp whileloop
checkCnum:

push Rnum
push Cnum
call checknum
cmp Count , 0
jl gameend
cmp Count , 0
je dontadd
cmp Count , 0
jg counter

pushad
dontadd:
mov eax , 0
mov ax , word ptr [Cnum]
mul five
sub ax , 1
mov ebx , Rnum
rol ebx , 16
mov bx , ax
invoke SetConsoleCursorPosition , outhandle , ebx
invoke WriteConsoleA,outhandle,offset spacer,lengthof spacer-1, offset x,0
popad
jmp whileloop
pushad
counter:
mov eax , 0
mov ax , word ptr [Cnum]
mul five
sub ax , 1
mov ebx , Rnum
rol ebx , 16
mov bx , ax
invoke SetConsoleCursorPosition , outhandle , ebx
push esi
mov esi , offset Count
call ToString
invoke WriteConsoleA,outhandle,offset Count,lengthof Count, offset x,0
popad
jmp whileloop
call endingstatement
popad
jmp completed
gameend:
pushad
invoke WriteConsoleA,outhandle,offset found,lengthof found, offset x,0
call endingstatement
popad
completed:
invoke ExitProcess , 0
main endp
end main

Do comment if have any confusion or query. Thanks !!!

Add a Comment

Your email address will not be published. Required fields are marked *