-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.asm
2307 lines (1897 loc) · 39.4 KB
/
maze.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
%include "/usr/share/csc314/asm_io.inc"
; how to represent everything
%define SPACE ' '
%define HALF_SPACE ' '
%define HOR_WALL '##'
%define VER_WALL '#'
%define PLAYER ';;'
%define CRUMB '..'
%define PATH '!!'
%define HOLE 0xF0,0x9F,0x95,0xB3,' '
%define LADDER 0xe2,0x98,0xb0,' '
%define FLAG 0xe2,0x9a,0x91,' '
; the player starting position.
; top left is considered (0,0)
%define STARTX 0
%define STARTY 0
%define START_LAYER 0
; these keys do things
%define EXITCHAR 'x'
%define NCHAR 'w'
%define WCHAR 'a'
%define SCHAR 's'
%define ECHAR 'd'
%define UCHAR 'q'
%define DCHAR 'e'
%define SPEED_UP '='
%define SPEED_DOWN '-'
; maze buffer offsets
%define E_OFFSET 0
%define S_OFFSET 1
%define D_OFFSET 2
; MAX_SPEED should be a multiple of SPEED_INC
%define MAX_SPEED 200
%define SPEED_INC 5
; Files to read from
%define START_SCREEN "start_scrn.txt"
%define END_SCREEN "end_scrn.txt"
%define RAND_FILE "/dev/urandom"
segment .data
; Files we need to read from
rand_file_str db RAND_FILE,0
start_scrn_file db START_SCREEN,0
end_scrn_file db END_SCREEN,0
; used to change the terminal mode
mode_r db "r",0
raw_mode_on_cmd db "stty raw -echo",0
raw_mode_off_cmd db "stty -raw echo",0
; called by system() to clear/refresh the screen
clear_screen_cmd db "clear",0
; things the program will print
manual_help_str db 13,10,"Controls: ", \
NCHAR,"=NORTH / ", \
WCHAR,"=WEST / ", \
SCHAR,"=SOUTH / ", \
ECHAR,"=EAST / ", \
UCHAR,"=UP / ", \
DCHAR,"=DOWN / ", \
EXITCHAR,"=EXIT", \
13,10,10,0
auto_help_str db 13,10,"Controls: ", \
"'",SPEED_UP,"'"," = SPEED UP / ", \
"'",SPEED_DOWN,"'"," = SLOW DOWN / ", \
"'",EXITCHAR,"'"," = EXIT", \
13,10,10,0
; Format strings for printing / scanning
tput_fmt_str db "tput cup %d %d",0
int_scan_str db "%d",0
char_scan_str db "%c",0
layer_fmt_str db "Layer %d/%d",13,10,0
speed_fmt_str db "Speed %d/%d",13,10,0
; Rendering definitions
hor_wall db HOR_WALL,0
ver_wall db VER_WALL,0
player db PLAYER,0
hole db HOLE,0
ladder db LADDER,0
flag db FLAG,0
space db SPACE,0
half_space db HALF_SPACE,0
crumb db CRUMB,0
path_marker db PATH,0
segment .bss
; Presence of walls in (E)ast, (S)outh, and (D)own directions
; 0 indicates a wall, 1 the absence of a wall
; Cell position in buffer: ((H*W*L)+(R*W)+C)*3
;|---|---|---|---
;| E | S | D |...
;|---|---|---|---
maze resd 1
; Nodes in the graph which have been visited
; |---|---|---|---
; | x | y | l | ...
; |---|---|---|---
visited resd 1
visited_len resd 1
path resd 1
path_len resd 1
; these variables store the dimensions of the maze
width resd 1
height resd 1
layers resd 1
; these variables store the current player position
xpos resd 1
ypos resd 1
layer resd 1
segment .text
global asm_main
global raw_mode_on
global raw_mode_off
global init_board
global render
extern system
extern putchar
extern getchar
extern fcntl
extern printf
extern sprintf
extern fopen
extern fread
extern fgetc
extern scanf
extern fclose
extern malloc
extern calloc
extern free
extern usleep
asm_main:
enter 0,0
pusha
;***************CODE STARTS HERE***************************
push ebp
mov ebp, esp
sub esp, 8
asm_main_beginning:
; show the start screen, and get maze config
call start_screen
mov DWORD [ebp-4], eax ; solve mode
; put the terminal in raw mode so the game works nicely
call raw_mode_on
; Set up some data structures for visited/path tracking
call num_edges
mov ebx, 4
mul ebx
mov DWORD [ebp-8], eax
push DWORD [ebp-8]
call malloc
add esp, 4
mov DWORD [visited], eax
push DWORD [ebp-8]
call malloc
add esp, 4
mov DWORD [path], eax
; read the game board file into the global variable
call gen_maze
; set the player at the proper start position
mov DWORD [xpos], STARTX
mov DWORD [ypos], STARTY
mov DWORD [layer], START_LAYER
; Determine solve mode
cmp DWORD [ebp-4], 'm'
je run_manual
cmp DWORD [ebp-4], 'd'
je run_dfs
cmp DWORD [ebp-4], 'b'
je run_bfs
run_manual:
call manual_mode
jmp game_end
run_dfs:
push 0
call auto_mode
add esp, 4
jmp game_end
run_bfs:
push 1
call auto_mode
add esp, 4
game_end:
; restore old terminal functionality
call raw_mode_off
; Show the end screen. Ask the user if they want to play again.
call at_flag
cmp eax, 1
jne asm_main_end
call end_screen
cmp eax, 'y'
je asm_main_beginning
asm_main_end:
; User answered 'no'. Clear the screen, and exit
push clear_screen_cmd
call system
add esp, 4
mov esp, ebp
pop ebp
;***************CODE ENDS HERE*****************************
popa
mov eax, 0
leave
ret
; ==================== Start and End Screen Functions ====================
; === FUNCTION ===
; Print the start screen and gather configuration parameters from the user
; Returns: mode ['m', 'd', 'b'] (eax)
start_screen:
push ebp
mov ebp, esp
start_screen_beginning:
; clear the screen
push clear_screen_cmd
call system
add esp, 4
; Print the start screen
push start_scrn_file
call print_file
add esp, 4
call flush_input_buffer
; Read width
push 21
push 24
push int_scan_str
call read_input
add esp, 12
cmp eax, 0
je start_screen_beginning
cmp ebx, 0
jl start_screen_beginning
mov DWORD [width], ebx
call flush_input_buffer
; Read height
push 22
push 25
push int_scan_str
call read_input
add esp, 12
cmp eax, 0
je start_screen_beginning
cmp ebx, 0
jl start_screen_beginning
mov DWORD [height], ebx
call flush_input_buffer
; Read layers
push 22
push 26
push int_scan_str
call read_input
add esp, 12
cmp eax, 0
je start_screen_beginning
cmp ebx, 0
jl start_screen_beginning
mov DWORD [layers], ebx
call flush_input_buffer
; Read solve mode
push 26
push 27
push char_scan_str
call read_input
add esp, 12
cmp eax, 0
je start_screen_beginning
movzx eax, bl
cmp eax, 'm'
je start_screen_end
cmp eax, 'd'
je start_screen_end
cmp eax, 'b'
jne start_screen_beginning
start_screen_end:
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Show the end screen, and ask the user if they want to play again
; Returns: play_again ['y','n'] (eax)
end_screen:
push ebp
mov ebp, esp
end_screen_beginning:
; clear the screen
push clear_screen_cmd
call system
add esp, 4
; Print the start screen
push end_scrn_file
call print_file
add esp, 4
call flush_input_buffer
; Read width
push 33
push 23
push char_scan_str
call read_input
add esp, 12
cmp eax, 0
je end_screen_beginning
movzx eax, bl
cmp eax, 'y'
je end_screen_end
cmp eax, 'n'
jne end_screen_beginning
end_screen_end:
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Places the cursor at the given coordinates on the screen, and reads the given type of input
; Args: col, row, scan_str
; Returns: success (eax), input (ebx)
read_input:
push ebp
mov ebp, esp
sub esp, 4
push DWORD [ebp+16]
push DWORD [ebp+12]
call tput
add esp, 8
lea eax, [ebp-4]
push eax
push DWORD [ebp+8]
call scanf
add esp, 8
mov ebx, DWORD [ebp-4]
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Clears the input buffer
flush_input_buffer:
push ebp
mov ebp, esp
flush_input_buffer_start:
call nonblocking_getchar
movsx eax, al
cmp eax, -1
je flush_input_buffer_end
jmp flush_input_buffer_start
flush_input_buffer_end:
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Calls tput with the given arguments
; Args: row, col
tput:
push ebp
mov ebp, esp
sub esp, 4
; Allocate string buffer
push 50
call malloc
add esp, 4
mov DWORD [ebp-4], eax
; Format tput string
push DWORD [ebp+12]
push DWORD [ebp+8]
push tput_fmt_str
push DWORD [ebp-4]
call sprintf
add esp, 16
; Call tput
push DWORD [ebp-4]
call system
add esp, 4
; Free string buffer
push DWORD [ebp-4]
call free
add esp, 4
mov esp, ebp
pop ebp
ret
; Print the contents of the given file to the screen
; Args: file
print_file:
push ebp
mov ebp, esp
sub esp, 5
push mode_r
push DWORD [ebp+8]
call fopen
add esp, 8
mov DWORD [ebp-4], eax ; FILE*
print_file_loop_start:
; Read one byte
lea eax, [ebp-5]
push DWORD [ebp-4]
push 1
push 1
push eax
call fread
add esp, 16
cmp eax, 1 ; If fread didn't read a byte, EOF -> end
jne print_file_loop_end
movzx eax, BYTE [ebp-5]
push eax
call putchar
add esp, 4
jmp print_file_loop_start
print_file_loop_end:
push DWORD [ebp-4]
call fclose
add esp, 4
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
raw_mode_on:
push ebp
mov ebp, esp
push raw_mode_on_cmd
call system
add esp, 4
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
raw_mode_off:
push ebp
mov ebp, esp
push raw_mode_off_cmd
call system
add esp, 4
mov esp, ebp
pop ebp
ret
; ==================== Maze Generation Functions ====================
; === FUNCTION ===
; Generate a random maze, and stores the pointer to it in [maze]
gen_maze:
push ebp
mov ebp, esp
call rand_graph
push eax
call kruskal
add esp, 4
mov DWORD [maze], eax
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Generates a spanning tree using Kruskal's algorithm
; Args: graph_ptr
; Returns: maze_ptr
kruskal:
push ebp
mov ebp, esp
sub esp, 28
; Tree tracker
call num_vertices
mov DWORD [ebp-4], eax ; num vertices
push eax
push 4
call calloc
add esp, 8
mov DWORD [ebp-8], eax ; tree_tracker_ptr
mov DWORD [ebp-12], 0 ; next tree idx
; Output maze
call num_edges
mov DWORD [ebp-16], eax ; num possible edges
push eax
push 1
call calloc
add esp, 8
mov DWORD [ebp-20], eax ; maze_ptr
; Build spanning tree
mov DWORD [ebp-24], 0
kruskal_loop_start:
mov eax, DWORD [ebp-4]
dec eax
cmp DWORD [ebp-24], eax ; stop after |V|-1 edges
jge kruskal_loop_end
; Get a ptr to the edge with the largest weight
kruskal_get_max:
push DWORD [ebp+8]
push DWORD [ebp-16]
call list_max
add esp, 8
; Set value to zero (so we know it's used)
mov DWORD [eax], 0
; Find offset
sub eax, DWORD [ebp+8]
; offset / 4 / 3 * 4 = vertex offset. (offset / 4) % 3 = E or S or D
mov ebx, 4
cdq
div ebx
; Save offset/4 for edge addition later
mov DWORD [ebp-28], eax
mov ebx, 3
cdq
div ebx
mov ecx, edx
mov ebx, 4
mul ebx
mov edx, ecx
; ebx = vertex_1, ecx = vertex_2
mov ebx, eax
add ebx, DWORD [ebp-8]
mov ecx, ebx
cmp edx, 0
je kruskal_east_edge
cmp edx, 1
je kruskal_south_edge
jmp kruskal_down_edge
kruskal_east_edge:
add ecx, 4
jmp kruskal_modify_forest
kruskal_south_edge:
mov eax, DWORD [width]
mov edx, 4
mul edx
add ecx, eax
jmp kruskal_modify_forest
kruskal_down_edge:
mov eax, DWORD [width]
mov edx, DWORD [height]
mul edx
mov edx, 4
mul edx
add ecx, eax
kruskal_modify_forest:
; Both 0 -> neither in a forest, make new tree
mov eax, DWORD [ecx]
or eax, DWORD [ebx] ; 0 if both 0
cmp eax, 0
je kruskal_new_tree
; Both same -> edge would create a cycle, skip
mov eax, DWORD [ebx]
cmp eax, DWORD [ecx]
je kruskal_get_max
; One zero -> put new one in the other's forest
cmp DWORD [ebx], 0
je kruskal_add_to_tree
cmp DWORD [ecx], 0
je kruskal_add_to_tree
; Different -> combine trees
jmp kruskal_combine_trees
kruskal_new_tree:
inc DWORD [ebp-12]
mov eax, DWORD [ebp-12]
mov DWORD [ebx], eax
mov DWORD [ecx], eax
jmp kruskal_add_edge
kruskal_add_to_tree:
cmp DWORD [ebx], 0
jne kruskal_add_to_tree_other
mov eax, DWORD [ecx]
mov DWORD [ebx], eax
jmp kruskal_add_edge
kruskal_add_to_tree_other:
mov eax, DWORD [ebx]
mov DWORD [ecx], eax
jmp kruskal_add_edge
kruskal_combine_trees:
push DWORD [ebp-8]
push DWORD [ebp-4]
push DWORD [ebx]
push DWORD [ecx]
call kruskal_combine
add esp, 16
kruskal_add_edge:
mov eax, DWORD [ebp-28]
add eax, DWORD [ebp-20] ; add maze_ptr
mov BYTE [eax], 1
inc DWORD [ebp-24]
jmp kruskal_loop_start
kruskal_loop_end:
mov eax, DWORD [ebp-20]
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Combines two trees in a kruskal tree tracker
; Args: tree_tracker_ptr, tree_tracker_len, num1, num2
kruskal_combine:
push ebp
mov ebp, esp
; Find smaller of num1, num2
mov eax, DWORD [ebp+8]
mov ebx, DWORD [ebp+12]
mov DWORD [ebp-4], eax ; Smaller num
mov DWORD [ebp-8], ebx ; Larger num
mov eax, DWORD [ebp+8]
cmp eax, DWORD [ebp+12]
jl kruskal_combine_find_smaller_end
mov DWORD [ebp-4], ebx ; Smaller num
mov DWORD [ebp-8], eax ; Larger num
kruskal_combine_find_smaller_end:
; Overwrite larger of the two nums w/ the smaller one
mov DWORD [ebp-12], 0
kruskal_combine_loop_start:
mov eax, DWORD [ebp-12]
cmp eax, DWORD [ebp+16] ; while < tree_tracker_len
jge kruskal_combine_loop_end
; If tree_tracker[i] == larger num, set to smaller num
mov ecx, DWORD [ebp+20]
mov eax, DWORD [ebp-12]
mov ebx, 4
mul ebx
add ecx, eax ; ptr to tree_tracker[i]
mov eax, DWORD [ebp-8]
cmp eax, DWORD [ecx] ; if tree_tracker[i] == larger num
jne kruskal_combine_loop_continue
; Equal so overwrite
mov eax, DWORD [ebp-4]
mov DWORD [ecx], eax
kruskal_combine_loop_continue:
inc DWORD [ebp-12]
jmp kruskal_combine_loop_start
kruskal_combine_loop_end:
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Generates a 3-D grid graph with random edge weights
; Returns: graph_ptr (eax)
rand_graph:
push ebp
mov ebp, esp
sub esp, 28
; Malloc space for graph
call num_edges
mov ebx, 4
mul ebx
push eax
call malloc
add esp, 4
mov DWORD [ebp-4], eax ; graph_ptr
; Open source of randomness
push mode_r
push rand_file_str
call fopen
add esp, 8
mov DWORD [ebp-8], eax ; fp
mov DWORD [ebp-12], 0
rand_graph_layer_loop_start:
mov eax, DWORD [ebp-12]
cmp eax, DWORD [layers]
jge rand_graph_layer_loop_end
mov DWORD [ebp-16], 0
rand_graph_height_loop_start:
mov eax, DWORD [ebp-16]
cmp eax, DWORD [height]
jge rand_graph_height_loop_end
mov DWORD [ebp-20], 0
rand_graph_width_loop_start:
mov eax, DWORD [ebp-20]
cmp eax, DWORD [width]
jge rand_graph_width_loop_end
; L*WIDTH*HEIGHT + R*WIDTH + C
mov eax, DWORD [ebp-12]
mov ebx, DWORD [height]
mul ebx
mov ebx, DWORD [width]
mul ebx
mov ecx, eax
mov eax, DWORD [ebp-16]
mov ebx, DWORD [width]
mul ebx
add eax, ecx
add eax, DWORD [ebp-20]
mov ebx, 12
mul ebx
mov DWORD [ebp-28], eax
mov DWORD [ebp-24], 0
rand_graph_edges_loop_start:
cmp DWORD [ebp-24], 3
jge rand_graph_edges_loop_end
cmp DWORD [ebp-24], 1
je rand_graph_test_south_edge
cmp DWORD [ebp-24], 2
je rand_graph_test_down_edge
mov eax, DWORD [width]
dec eax
cmp DWORD [ebp-20], eax
jne rand_graph_do_random
jmp rand_graph_do_zero
rand_graph_test_south_edge:
mov eax, DWORD [height]
dec eax
cmp DWORD [ebp-16], eax
jne rand_graph_do_random
jmp rand_graph_do_zero
rand_graph_test_down_edge:
mov eax, DWORD [layers]
dec eax
cmp DWORD [ebp-12], eax
jne rand_graph_do_random
rand_graph_do_zero:
mov ebx, 0
jmp rand_graph_add_to_buffer
rand_graph_do_random:
; Get randint
push DWORD [ebp-8]
call randint
add esp, 4
cmp DWORD [ebp-24], 2 ; if down, reduce to 1/2
jne rand_graph_do_random_cont
mov ebx, 2
mov edx, 0
div ebx
rand_graph_do_random_cont:
cmp eax, 0 ; if zero, retry
je rand_graph_do_random
mov ebx, eax
rand_graph_add_to_buffer:
; Add to buffer
mov eax, DWORD [ebp-24]
mov ecx, 4
mul ecx
add eax, DWORD [ebp-4]
add eax, DWORD [ebp-28]
mov DWORD [eax], ebx
inc DWORD [ebp-24]
jmp rand_graph_edges_loop_start
rand_graph_edges_loop_end:
inc DWORD [ebp-20]
jmp rand_graph_width_loop_start
rand_graph_width_loop_end:
inc DWORD [ebp-16]
jmp rand_graph_height_loop_start
rand_graph_height_loop_end:
inc DWORD [ebp-12]
jmp rand_graph_layer_loop_start
rand_graph_layer_loop_end:
; Close source of randomness
push DWORD [ebp-8]
call fclose
add esp, 4
mov eax, DWORD [ebp-4] ; Return value
mov esp, ebp
pop ebp
ret
; === FUNCTION ===
; Reads 4 bytes of data from the given file as an integer
; Returns: random_int (eax)
randint:
push ebp
mov ebp, esp
sub esp, 8 ; counter, buffer
; Read one byte at a time from the file, and
; add it to the 4 byte (DWORD) buffer
mov DWORD [ebp-4], 0
randint_loop_start:
cmp DWORD [ebp-4], 4
jge randint_loop_end
push DWORD [ebp+8]
call fgetc
add esp, 4
mov ebx, DWORD [ebp-4]
mov BYTE [ebp-8+ebx], al
inc DWORD [ebp-4]
jmp randint_loop_start
randint_loop_end:
mov eax, DWORD [ebp-8]
mov esp, ebp
pop ebp
ret
; ==================== Maze Solving Functions ====================
; === FUNCTION ===
; Run the game in manual mode
manual_mode:
push ebp
mov ebp, esp
sub esp, 12 ; tmp next_x, next_y, next_layer
; Initial movement
push DWORD [xpos]
push DWORD [ypos]
push DWORD [layer]
call add_visited
add esp, 12
manual_loop_start:
; draw the game board
push 0
call render
add esp, 4
; Prep to move
mov eax, DWORD [xpos]
mov ebx, DWORD [ypos]
mov ecx, DWORD [layer]
mov DWORD [ebp-4], eax
mov DWORD [ebp-8], ebx
mov DWORD [ebp-12], ecx
; get an action from the user
call getchar
; choose what to do
cmp eax, EXITCHAR
je manual_loop_end
cmp eax, NCHAR
je move_north
cmp eax, WCHAR
je move_west
cmp eax, SCHAR
je move_south
cmp eax, ECHAR
je move_east
cmp eax, UCHAR
je move_up
cmp eax, DCHAR
je move_down
jmp input_end ; or just do nothing
; move the player according to the input character
move_north:
dec DWORD [ebp-8]
jmp input_end
move_south:
inc DWORD [ebp-8]
jmp input_end
move_west:
dec DWORD [ebp-4]
jmp input_end
move_east:
inc DWORD [ebp-4]
jmp input_end
move_up:
dec DWORD [ebp-12]
jmp input_end
move_down:
inc DWORD [ebp-12]
input_end:
push DWORD [xpos]
push DWORD [ypos]
push DWORD [layer]
push DWORD [ebp-4]
push DWORD [ebp-8]