-
Notifications
You must be signed in to change notification settings - Fork 0
/
mazeGenerator.asm
2012 lines (1720 loc) · 54.7 KB
/
mazeGenerator.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; ------------------------------------------------------------------------------------------------------------
; ,ggg, ,ggg,_,ggg, ,gg,
; dP""Y8dP""Y88P""Y8b i8""8i I8
; Yb, `88' `88' `88 `8,,8' I8
; `" 88 88 88 `Y88aaad8 88888888
; 88 88 88 d8""""Y8, I8
; 88 88 88 ,gggg,gg ,gggg, ,ggg, ,8P 8b ,ggg, ,ggg,,ggg, ,ggg, ,gggggg, ,gggg,gg I8 ,ggggg, ,gggggg,
; 88 88 88 dP" "Y8I d8" Yb i8" "8i dP Y8 i8" "8i ,8" "8P" "8, i8" "8i dP""""8I dP" "Y8I I8 dP" "Y8ggg dP""""8I
; 88 88 88 i8' ,8I dP dP I8, ,8I _ ,dP' I8 I8, ,8I I8 8I 8I I8, ,8I ,8' 8I i8' ,8I ,I8, i8' ,8I ,8' 8I
; 88 88 Y8,,d8, ,d8b,,dP ,adP' `YbadP' "888,,_____,dP `YbadP' ,dP 8I Yb, `YbadP' ,dP Y8,,d8, ,d8b, ,d88b, ,d8, ,d8' ,dP Y8,
; 88 88 `Y8P"Y8888P"`Y88" ""Y8d8888P"Y888 a8P"Y888888P" 888P"Y8888P' 8I `Y8888P"Y8888P `Y8P"Y8888P"`Y888P""Y88P"Y8888P" 8P `Y8
; ,d8I'
; ,dP'8I
; ,8" 8I
; I8 8I
; `8, ,8I
; `Y8P" I8
; -------------------------------------------------------------------------------------------------------------
IDEAL
MODEL small
STACK 100h
DATASEG
;--------file--------
filehandle dw ?
Header db 54 dup (0)
Palette db 256*4 dup (0)
ScrLine db 320 dup (0)
ErrorMsg db 'Error', 13, 10,'$'
;-------------------
;-------images------
secret db 'ss.bmp', 0
libraryS db 's1.bmp',0
libraryB db 'bg1.bmp',0
libraryG db 'sg1.bmp',0
spaceS db 's2.bmp',0
spaceB db 'bg2.bmp',0
spaceG db 'sg2.bmp',0
templeS db 's3.bmp',0
templeB db 'bg3.bmp',0
templeG db 'sg3.bmp',0
;-------------------
;------flags------
is_maze dw 0
;-----------------
;-----pointers----
start_pointer dw offset templeS
bg_pointer dw offset templeB
graphics_pointer dw offset templeG
stack_pointer dw offset stackC
graphics_args dw 9 dup (0), offset start_pointer, offset bg_pointer, offset graphics_pointer
run_args dw offset bg_pointer,offset Xn, offset stackC, offset board, offset neighbors_list, offset stack_pointer, offset current_cell, offset maze_end, offset maze_start, offset is_maze, offset secret, offset maze_start, offset maze_end
;-----------------
;-------variables------
maze_start dw 0 ; works
maze_end dw 399
current_cell dw 0 ;0-399 ; previews cell
Xn dw 0
;----------------------
;------data structions-----
dw 399 dup (0)
stackC dw 0
neighbors_list dw 4 dup (0)
board dw 800 dup (0)
;--------------------------
CODESEG
; Consts colors
GREEN equ 2
WHITE equ 0fh
RED equ 4
GRAY equ 7
LIGHT_GREEN equ 0ah
; ,dPYb, ,dPYb,
; IP'`Yb IP'`Yb
; I8 8I gg I8 8I
; I8 8' "" I8 8'
; I8 dP gg I8 dP ,ggg, ,g,
; I8dP 88 I8dP i8" "8i ,8'8,
; I8P 88 I8P I8, ,8I ,8' Yb
; ,d8b,_ _,88,_,d8b,_ `YbadP' ,8'_ 8)
; PI8"88888P""Y88P'"Y88888P"Y888P' "YY8P8P
; I8 `8,
; I8 `8,
; I8 8I
; I8 8I
; I8, ,8'
; "Y8P'
;___________________________________________________________Files________________________________________________________________
;--------------------------------
; use: open a given file name print erro in case of one
; Input: file name
; Output: None
proc openFile
push bp
mov bp,sp
; Open file
mov ah, 3Dh
xor al, al
mov dx, [bp+4]
int 21h
jc openerror ;if there is en error, close the procdure with an error messege.
mov [filehandle], ax
pop bp
ret 2
openerror:
mov dx, offset ErrorMsg
mov ah, 9h
int 21h
pop bp
ret 2
endp openFile
;--------------------------------
;--------------------------------
proc readHeader
; Read BMP file header, 54 bytes
mov ah,3fh
mov bx, [filehandle]
mov cx,54
mov dx,offset Header
int 21h
ret
endp readHeader
;--------------------------------
;--------------------------------
proc readPalette
; Read BMP file color palette, 256 colors * 4 bytes (400h)
mov ah,3fh
mov cx,400h
mov dx,offset Palette
int 21h
ret
endp readPalette
;--------------------------------
;--------------------------------
proc copyPal
; Copy the colors palette to the video memory
; The number of the first color should be sent to port 3C8h
; The palette is sent to port 3C9h
mov si,offset Palette
mov cx,256
mov dx,3C8h
mov al,0
; Copy starting color to port 3C8h
out dx,al
; Copy palette itself to port 3C9h
inc dx
PalLoop:
; Note: Colors in a BMP file are saved as BGR values rather than RGB.
mov al,[si+2] ; Get red value.
shr al,2 ; Max. is 255, but video palette maximal
; value is 63. Therefore dividing by 4.
out dx,al ; Send it.
mov al,[si+1] ; Get green value.
shr al,2
out dx,al ; Send it.
mov al,[si] ; Get blue value.
shr al,2
out dx,al ; Send it.
add si,4 ; Point to next color.
; (There is a null chr. after every color.)
loop PalLoop
ret
endp copyPal
;--------------------------------
;--------------------------------
proc copyBitmap
; BMP graphics are saved upside-down.
; Read the graphic line by line (200 lines in VGA format),
; displaying the lines from bottom to top.
mov ax, 0A000h
mov es, ax
mov cx,200
PrintBMPLoop:
push cx
; di = cx*320, point to the correct screen line
mov di,cx
shl cx,6 ;*64
shl di,8 ;*256 ; 256+64 = 320
add di,cx
; Read one line to variable ScrLine (buffer)
mov ah,3fh
mov cx,320
mov dx,offset ScrLine
int 21h
; Copy one line into video memory
cld ; Clear direction flag, for movsb for inc si, inc di
mov cx,320
mov si,offset ScrLine
sub di, 320
rep movsb ; Copy line to the screen
;rep movsb is same as the following code:
;mov es:di, ds:si
;inc si
;inc di
;dec cx
;loop until cx=0
; call delay
pop cx
loop PrintBMPLoop
ret
endp copyBitmap
;--------------------------------
;--------------------------------
proc CloseFile
mov ah,3Eh
mov bx, [filehandle]
int 21h
ret
endp CloseFile
;-------------------------------
; use: print BMP file on the screen
; Input: file name(+4)
; Output: None
proc printBMP
push bp
mov bp, sp
; open file black box
push si
push cx
push ax
push dx
push bx
push di
; Process BMP file
push [bp + 4]
call openFile
call readHeader
call readPalette
call copyPal
call copyBitmap
call CloseFile
pop di
pop bx
pop dx
pop ax
pop cx
pop si
pop bp
ret 2
endp printBMP
;--------------------------------
;________________________________________________________________________________________________________________________________
; ,dPYb,
; IP'`Yb
; I8 8I gg
; I8 8' ""
; ,gggg,gg ,gggggg, ,gggg,gg gg,gggg, I8 dPgg, gg ,gggg, ,g,
; dP" "Y8I dP""""8I dP" "Y8I I8P" "Yb I8dP" "8I 88 dP" "Yb ,8'8,
; i8' ,8I ,8' 8I i8' ,8I I8' ,8i I8P I8 88 i8' ,8' Yb
; ,d8, ,d8I ,dP Y8,,d8, ,d8b,,I8 _ ,d8' ,d8 I8,_,88,_,d8,_ _,8'_ 8)
; P"Y8888P"8888P `Y8P"Y8888P"`Y8PI8 YY88888P88P `Y88P""Y8P""Y8888PPP' "YY8P8P
; ,d8I' I8
; ,dP'8I I8
; ,8" 8I I8
; I8 8I I8
; `8, ,8I I8
; `Y8P" I8
;____________________________________________________________Graphics____________________________________________________________
; Use draw current position of the start/end block
; Input: color(+6), position to draw(+4)
;--------------------------------
proc drawCurrentPos
push bp
mov bp, sp
push di
push ax
mov di, [bp + 4] ; pos
push di
call indexToPlace
pop di ; place
push 8 ; input: width, height, color, place
push 8
push [bp + 6]
push di
call drawCell
pop ax
pop di
pop bp
ret 4
endp drawCurrentPos
;--------------------------------
; Use: print a width and height cell
; Input: width(+10), height(+8), color(+6), place(+4)
; Output: None
;--------------------------------
proc drawCell
push bp
mov bp, sp
push cx
push di
push ax
;-----------------------
mov di, [bp + 4] ; place
mov ax, [bp + 6] ; color
mov cx, [bp + 8] ; height
;-----------------------
block:
push cx
mov cx, [bp + 10] ; width
line:
mov [es:di], al
inc di
loop line
pop cx
;------------
push ax
mov ax, 320
sub ax, [bp + 10]
add di, ax ; 320 - width
pop ax
;------------
loop block
pop ax
pop di
pop cx
pop bp
ret 8
endp drawCell
;--------------------------------
;--------------------------------
; Use: draw a board on the screen
; Input: width(+10), height(+8), color(+6), place(+4)
; Output: None
proc drawBoard
push bp
mov bp, sp
push di
push ax
push cx
;-----------------------
mov di, [bp + 4] ; place
mov ax, [bp + 6] ; color
mov cx, 20
;-----------------------
; Draw Loop:
y_blocks:
push cx
mov cx, 20
;-----------------------
x_blocks:
push [bp + 10] ; width
push [bp + 8] ; hieght
push ax
push di
call drawCell
add di, 10
loop x_blocks
;-----------------------
pop cx
add di, 320 * 10 - 200 ; goes dowm 10 lines!
loop y_blocks
pop cx
pop ax
pop di
pop bp
ret 8
endp drawBoard
;--------------------------------
;--------------------------------
; use: change the graphics mode
; input: offset graphics_args(+4)
; graphics_args values:
; offset_list(0-16), offset start_pointer(+18), offset bg_pointer(+20), offset graphics_pointer(+22)
; offset_list values:
; libraryS libraryB libraryG spaceS spaceB spaceG templeS templeB templeG
; output: None
proc graphics
push bp
mov bp, sp
push ax
push bx
push si
mov si, [bp + 4]
mov bx, [si + 22]
push [bx]
call printBMP
input_analyze:
; Wait for key press
mov ah,0
int 16h
;-----------------
cmp al, '1'
je library
cmp al, '2'
je space
cmp al, '3'
je temple
jmp input_analyze
;-----------------
;------update graphics pointers--------
library:
; start
mov bx, [si + 18]
mov ax, [si]
mov [word ptr bx], ax
; algorithem background
mov bx, [si + 20]
mov ax, [si + 2]
mov [word ptr bx], ax
; graphic
mov bx, [si + 22]
mov ax, [si + 4]
mov [word ptr bx], ax
jmp end1
space:
; start
mov bx, [si + 18]
mov ax, [si + 6]
mov [word ptr bx], ax
; algorithem background
mov bx, [si + 20]
mov ax, [si + 8]
mov [word ptr bx], ax
; graphic
mov bx, [si + 22]
mov ax, [si + 10]
mov [word ptr bx], ax
jmp end1
temple:
; start
mov bx, [si + 18]
mov ax, [si + 12]
mov [word ptr bx], ax
; algorithem background
mov bx, [si + 20]
mov ax, [si + 14]
mov [word ptr bx], ax
; graphic
mov bx, [si + 22]
mov ax, [si + 16]
mov [word ptr bx], ax
jmp end1
;--------------------------------------
end1:
pop si
pop bx
pop ax
pop bp
;-------------
ret 8
endp graphics
;--------------------------------
;----------------delay----------------
; input: None
; output: None
proc delay
push cx
mov cx, 0fffh
;--------------------------------
out_loop:
push cx
mov cx, 30
;----------------
inloop:
loop inloop
;----------------
pop cx
loop out_loop
;--------------------------------
pop cx ; black box
ret ; retrun the ip in order to
endp delay
;------------------------------------
;--------------------------------
; Use: draw up and down cells with connection
; up - new_cell
; down - current_cell
; Input: new cell index(+4)/current_cell(+4)
; Output: None
proc drawY
push bp
mov bp, sp
push ax
;-------draw from the current cell dowm-----
push [bp + 4]
call indexToPlace
pop ax
; draw
push 8
push 18
push 0fh
push ax
call drawCell
;-------------------------------------------
pop ax
pop bp
ret 2
endp drawY
;--------------------------------
;--------------------------------
; Use: draw left and right cells with connection
; left - new_cell
; right - current_cell
; Input: new cell index(+4)/current_cell(+4)
; Output: None
proc drawX
push bp
mov bp, sp
push ax
;-------draw from the current cell dowm-----
push [bp + 4]
call indexToPlace
pop ax
; draw
push 18
push 8
push 0fh
push ax
call drawCell
;-------------------------------------------
pop ax
pop bp
ret 2
endp drawX
;--------------------------------
;________________________________________________________________________________________________________________________________
; ,dPYb,
; IP'`Yb
; I8 8I gg
; I8 8' ""
; I8 dP ,ggggg, ,gggg,gg gg ,gggg,
; I8dP dP" "Y8ggg dP" "Y8I 88 dP" "Yb
; I8P i8' ,8I i8' ,8I 88 i8'
; ,d8b,_ ,d8, ,d8' ,d8, ,d8I _,88,_,d8,_ _
; 8P'"Y88P"Y8888P" P"Y8888P"8888P""Y8P""Y8888PP
; ,d8I'
; ,dP'8I
; ,8" 8I
; I8 8I
; `8, ,8I
; `Y8P"
;__________________________________________________________Logic__________________________________________________________________
; ,gggggggggggg,
; dP"""88""""""Y8b, I8
; Yb, 88 `8b, I8
; `" 88 `8b 88888888
; 88 Y8 I8
; 88 d8 ,gggg,gg I8 ,gggg,gg
; 88 ,8P dP" "Y8I I8 dP" "Y8I
; 88 ,8P' i8' ,8I ,I8, i8' ,8I
; 88______,dP' ,d8, ,d8b, ,d88b, ,d8, ,d8b,
; 888888888P" P"Y8888P"`Y888P""Y88P"Y8888P"`Y8
;-----------------------------------------------
; Use: finds all file names offsets
; Input: offset_list(+6), first offset libraryS(+4)
; Output:None - update a list of offsets of the file names
proc fileOffsets
push bp
mov bp, sp
push bx
push ax
push cx
push si
mov bx, [bp + 4] ; offset libraryS
mov si, [bp + 6] ; offset_list
; Update first offset
mov [si], bx
; nine pictures
mov ax, si
add si, 2
; this is the end of the list
add ax, 9*2
offset_loop:
; if there is null it is the end string
cmp [byte ptr bx], 0
jne notEndString
; the next byte is going to be the start of the next string
inc bx
; save the string offset and update si to point to the next cell
mov [si], bx
add si, 2
notEndString:
inc bx
cmp ax, si
jne offset_loop
pop si
pop cx
pop ax
pop bx
pop bp
ret 4
endp fileOffsets
; ,ggggggggggggggg ,gg,
; dP""""""88""""""" ,dPYb, i8""8i I8 ,dPYb,
; Yb,_ 88 IP'`Yb `8,,8' I8 IP'`Yb
; `"" 88 I8 8I `88' 88888888 I8 8I
; 88 I8 8' dP"8, I8 I8 8bgg,
; 88 I8 dPgg, ,ggg, dP' `8a I8 ,gggg,gg ,gggg, I8 dP" "8
; 88 I8dP" "8I i8" "8i dP' `Yb I8 dP" "Y8I dP" "Yb I8d8bggP"
; gg, 88 I8P I8 I8, ,8I _ ,dP' I8 ,I8, i8' ,8I i8' I8P' "Yb,
; "Yb,,8P ,d8 I8, `YbadP' "888,,____,dP ,d88b, ,d8, ,d8b,,d8,_ _,d8 `Yb,
; "Y8P' 88P `Y8888P"Y888 a8P"Y88888P" 88P""Y88P"Y8888P"`Y8P""Y8888PP88P Y8
;-----------------------------------------------
; Use: push given data to the stackC
; Input: data to push(+6), p stack_pointer(+4)
; Output: None
proc pushC
push bp
mov bp, sp
push bx
push di
mov bx, [bp + 4] ; bx = offset stack pointer
mov di, [bx] ; di = [stack pointer]
sub [word ptr bx], 2 ; update stack pointer
;----update stack value----
mov bx, [bp + 6]
mov [di], bx
pop di
pop bx
pop bp
ret 4
endp pushC
;-----------------------------------------------
;-----------------------------------------------
; Use: pop the data from the stackC
; Input: p stack_pointer(+4)
; Output: stack value
proc popC
push bp
mov bp, sp
push bx
push di
;------update stack pointer-------
mov bx, [bp + 4] ; bx = offset stack pointer
add [word ptr bx], 2
mov di, [bx] ; di = [stack pointer]
;-------output------
mov bx, [di]
mov [bp + 4], bx ; output the stack value
pop di
pop bx
pop bp
ret
endp popC
;-----------------------------------------------
; ,gggg,
; ,88"""Y8b, I8
; d8" `Y8 I8
; d8' 8b d8 88888888
; ,8I "Y88P' I8
; I8' ,ggggg, ,ggg,,ggg, ggg gg ,ggg, ,gggggg, I8 ,ggggg, ,gggggg, ,g,
; d8 dP" "Y8ggg ,8" "8P" "8, d8"Yb 88bg i8" "8i dP""""8I I8 dP" "Y8ggg dP""""8I ,8'8,
; Y8, i8' ,8I I8 8I 8I dP I8 8I I8, ,8I ,8' 8I ,I8, i8' ,8I ,8' 8I ,8' Yb
; `Yba,,_____, ,d8, ,d8' ,dP 8I Yb,,dP I8, ,8I `YbadP' ,dP Y8, ,d88b, ,d8, ,d8' ,dP Y8,,8'_ 8)
; `"Y8888888 P"Y8888P" 8P' 8I `Y88" "Y8P" 888P"Y8888P `Y888P""Y88P"Y8888P" 8P `Y8P' "YY8P8P
;-----------------------------------------------
; Use: convert baord index to cell on the screen
; Input: current_cell(+4)
; Output: the place on the screen
proc indexToPlace ; keep in mind the board is 800 word not 400
push bp
mov bp, sp
push ax
push dx
push bx
push cx
; reset all registers
xor ax, ax
xor bx, bx
xor dx, dx
xor cx, cx
mov ax, [bp + 4]
mov bx, 20
div bx ; dx module ax division
mov cx, dx
mov bx, 3200
mul bx ; ax = 320* division * 10
; switch values
xor ax, cx ; ax = module
xor cx, ax ; cx = 320 * division * 10
xor ax, cx
mov bx, 10
mul bx ; ax = module * 10
add cx, ax ; dx = module * 10 + 320 * division * 10
add cx, 381 ; dx = 381 + module * 10 + 320 * division * 10
mov [bp + 4], cx
pop cx
pop bx
pop dx
pop ax
pop bp
ret
endp indexToPlace
;-----------------------------------------------
;-----------------------------------------------
; Use: given index in data and output index in board list
; Input: offset board(+6), index in data(+4)
; Output: index in board
proc dataToBoard
push bp
mov bp, sp
push dx
push ax
push bx
xor dx, dx ; solve div bug
mov ax, [bp + 4]
sub ax, [bp + 6]
mov bx, 4
div bx
mov [bp + 6], ax
pop bx
pop ax
pop dx
pop bp
ret 2
endp dataToBoard
;-----------------------------------------------
;-----------------------------------------------
; Use: given index in board list and output index in data
; Input: offset board(+6), boardIndex(+4)
; Output: index in data
proc boardToData
push bp
mov bp, sp
push dx
push ax
push bx
xor dx, dx ; solve mul bug
;------index * 4 + board----
mov ax, [bp + 4]
mov bx, 4
mul bx
add ax, [bp + 6]
;--------------------------
mov [bp + 6], ax
pop bx
pop ax
pop dx
pop bp
ret 2
endp boardToData
;-----------------------------------------------
; ,ggg, ,ggggggg,
; dP""Y8,8P"""""Y8b ,dPYb, ,dPYb,
; Yb, `8dP' `88 IP'`Yb IP'`Yb
; `" 88' 88 gg I8 8I I8 8I
; 88 88 "" I8 8' I8 8'
; 88 88 ,ggg, gg ,gggg,gg I8 dPgg, I8 dP ,ggggg, ,gggggg, ,g,
; 88 88 i8" "8i 88 dP" "Y8I I8dP" "8I I8dP 88gg dP" "Y8ggg dP""""8I ,8'8,
; 88 88 I8, ,8I 88 i8' ,8I I8P I8 I8P 8I i8' ,8I ,8' 8I ,8' Yb
; 88 Y8, `YbadP' _,88,_,d8, ,d8I ,d8 I8,,d8b, ,8I ,d8, ,d8' ,dP Y8,,8'_ 8)
; 88 `Y8888P"Y8888P""Y8P"Y8888P"88888P `Y88P'"Y88P"' P"Y8888P" 8P `Y8P' "YY8P8P
; ,d8I'
; ,dP'8I
; ,8" 8I
; I8 8I
; `8, ,8I
; `Y8P"
;-----------------------------------------------
; Use: check for invalid movement to the left and right
; Input: current_cell(+6), which neighbor +1/-1/20/-20(+4)
; Output: valid = 0 invalid = -1
proc leftRight
push bp
mov bp, sp
push ax
push dx
push cx
mov ax, [bp + 6]
xor dx, dx
mov cx, 20
div cx
cmp dx, 0
je left
cmp dx, 19
je right
jmp not_left
left:
cmp [word ptr bp + 4], -1 ; if the next cell is in the wall then invalid
je invalid_edge
not_left:
jmp not_right
right:
cmp [word ptr bp + 4] , 1 ; if the next cell is in the wall then invalid
je invalid_edge
not_right:
mov [word ptr bp + 6], 0
jmp valid1
invalid_edge:
mov [word ptr bp + 6], -1
valid1:
pop cx
pop dx
pop ax
pop bp
ret 2
endp leftRight
;-----------------------------------------------
;-----------------------------------------------
; Use: find current_cell neighbors that are not visited
; Input: offset board(+8), current_cell(+6), which neighbor +1/-1/20/-20(+4)
; Output: valid: offset neighbor
; invalid: -1
proc validNeighbor
push bp
mov bp, sp
push ax
push bx
xor ax, ax
xor bx, bx
mov bx, [bp + 6] ; current_cell index in the board 0 - 399
mov ax, [bp + 4] ; X = +1/-1, Y = +20/-20
add bx, ax
; 0 <= cell in board <= 399 and sides -> %20 -> left %19 -> right
cmp bx, 0
jl invalid
cmp bx, 399
ja invalid
push [bp + 6]
push [bp + 4]
call leftRight
pop ax
cmp ax, -1
je invalid
; index in data
shl bx, 2 ; two word = 4 bytes
mov ax, [bp + 8]
add bx, ax
; cell is visited
mov ax, [bx] ;ax:dx
cmp ax, 1
je invalid
mov [bp + 8], bx ; valid cell
jmp valid
invalid:
mov [word ptr bp + 8], -1 ; invalid cell
valid:
pop bx
pop ax
pop bp
ret 4
endp validNeighbor
;-----------------------------------------------
;-----------------------------------------------
; Use: generate the neighbors list and return if there is no neighbors
; Input:offset neighbors_list(+8) p_board(+6), current_cell(+4)
; Output: sum of neighbors
proc findNeighbors
push bp
mov bp, sp
push bx
push ax
push dx
xor ax, ax
xor dx, dx
mov bx, [bp + 8]
; Up
push [bp + 6]
push [bp + 4]; offset board(+8), current_cell(+6), which neighbor +1/-1/20/-20(+4)
push -20
call validNeighbor
pop ax
mov [bx], ax
add dx, ax ; counter of invalid
; Down
push [bp + 6]
push [bp + 4]
push 20
call validNeighbor
pop ax
mov [bx + 2], ax
add dx, ax ; counter of invalid