-
Notifications
You must be signed in to change notification settings - Fork 1
/
simon.asm
1020 lines (830 loc) · 15.6 KB
/
simon.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
;This is my simon game!!! enjoy :)
IDEAL
MODEL small
STACK 100h
DATASEG
;Array:
ColorsPlayed db 100 dup (5)
index dw 0
;Counters and checkers:
checkerForPlay dw 0
checkerForPress dw 0
points dw 0
;Messages:
levelMessage db 'Please select the wanted level:$'
scoreMessage db 'Your Score:$'
ErrorMsg db 'Error$'
;Timer
clock equ es:6Ch
timePeriode dw 18
timeSubstracter dw 0
limit dw 0
;Locations and sizes:
x dw 0
y dw 0
minX dw 0
maxX dw 0
minY dw 0
maxY dw 0
horLength dw 0
verLength dw 0
;Colors:
redColor dw 12
blueColor dw 6
yellowColor dw 29
greenColor dw 2
redButtonColor dw 3577
blueButtonColor dw 3580
yellowButtonColor dw 3579
greenButtonColor dw 3578
;Sounds:
redSound dw 7240h
blueSound dw 5424h
yellowSound dw 4305h
greenSound dw 3620h
currentSound dw 0h
;BMP files related
filename db ?
backgroundPic db 'SimonPic.bmp',0
gameOverPic db 'gameOver.bmp',0
levelsPic db 'levels.bmp',0
filehandle dw ?
Header db 54 dup (0)
Palette db 256*4 dup (0)
ScrLine db 320 dup (0)
CODESEG
proc OpenFile
; Opens file
mov ah, 3Dh
xor al, al
int 21h
jc openerror
mov [filehandle], ax
ret
;printing error message
openerror:
mov dx, offset ErrorMsg
mov ah, 9h
int 21h
ret
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
shl di,8
add di,cx
; Read one line
mov ah,3fh
mov cx,320
mov dx,offset ScrLine
int 21h
; Copy one line into video memory
cld ; Clear direction flag, for movsb
mov cx,320
mov si,offset ScrLine
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
pop cx
loop PrintBMPLoop
ret
endp CopyBitmap
;draws the game background (simon picture)
proc DrawBackground
; Process BMP file
mov dx, offset backgroundPic
call OpenFile
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
ret
endp DrawBackground
;draws the gameover background (gameover picture)
proc DrawGameOver
; Process BMP file
mov dx, offset gameOverPic
call OpenFile
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
ret
endp DrawGameOver
;draws the levels selection background (levels picture)
proc DrawLevelMenu
; Process BMP file
mov dx, offset levelsPic
call OpenFile
call ReadHeader
call ReadPalette
call CopyPal
call CopyBitmap
ret
endp DrawLevelMenu
proc Timer
push[timePeriode]
push bp
mov bp, sp
timePeriodee equ [bp+2]
; wait for first change in timer
mov ax, 40h
mov es, ax
mov ax, [clock]
firstTick:
cmp ax, [clock]
je firstTick
; counts 'x' sec
mov cx, timePeriodee ; 18x0.055sec = ~1sec
delayLoop:
mov ax, [clock]
tick:
cmp ax, [clock]
je tick
loop delayLoop
pop bp
pop [timePeriode]
ret
endp Timer
;making the timer count less time
proc GoFaster
mov dx, [timeSubstracter]
sub [timePeriode], dx
cmp [timePeriode], 3
jg donee
mov [timePeriode], 3
donee:
ret
endp GoFaster
proc PlaySound
; opens speaker
in al, 61h
or al, 00000011b
out 61h, al
; sends control word to change frequency
mov al, 0B6h
out 43h, al
; plays requessted frequency
out 42h, al ; Sending lower byte
mov al, ah
out 42h, al ; Sending upper byte
call Timer
; close the speaker
in al, 61h
and al, 11111100b
out 61h, al
ret
endp PlaySound
proc InitializeMouse
; Initializes the mouse
mov ax,0h
int 33h
; Shows mouse
mov ax,1h
int 33h
ret
endp InitializeMouse
proc DisableMouse
; Hides mouse
mov ax,02
int 33h
ret
endp DisableMouse
;checking mouse left click input from the user on a spesific color, if pressed: continues the game, if not: gameover
proc IsOnColor
push ax
push bx
push cx
push dx
; Loop until mouse left click
MouseLP:
mov ax,3h
int 33h
cmp bx, 01h ; check left mouse click
jne MouseLP
; Checks if the mouse location is on a specific button
cmp cx, [minX]
jg continue1
call DisableMouse
call DrawGameOver
jmp exit
;----------------
continue1:
cmp cx, [maxX]
jl continue2
call DisableMouse
call DrawGameOver
jmp exit
;----------------
continue2:
cmp dx, [maxY]
jl continue3
call DisableMouse
call DrawGameOver
jmp exit
;----------------
continue3:
cmp dx, [minY]
jg done
call DisableMouse
call DrawGameOver
jmp exit
done:
pop dx
pop cx
pop bx
pop ax
ret
endp IsOnColor
;checking mouse left click input from the user untill he presses on the start button, then starts the game
proc IsOnStart
push ax
push bx
push cx
push dx
;start button coordinates
mov [minX], 00DAh
mov [maxX], 01A6h
mov [maxY], 007Dh
mov [minY], 004Ah
; Loop until mouse left click
MouseLPP:
mov ax,3h
int 33h
cmp bx, 01h ; check left mouse click
jne MouseLPP
; Checks if the mouse location is on a specific button
cmp cx, [minX]
jg continuee1
jmp MouseLPP
;----------------
continuee1:
cmp cx, [maxX]
jl continue2
jmp MouseLPP
;----------------
continuee2:
cmp dx, [maxY]
jl continue3
jmp MouseLPP
;----------------
continuee3:
cmp dx, [minY]
jg done
jmp MouseLPP
doneee:
pop dx
pop cx
pop bx
pop ax
ret
endp IsOnStart
;checking mouse left click input from the user untill he presses on one of the level buttons, changes features in the game according to the level
;that was selected
proc SelectLevel
push ax
push bx
push cx
push dx
; Loops until mouse left click
MouseLPPP:
mov ax,3h
int 33h
cmp bx, 01h ; checks left mouse click
jne MouseLPPP
Easy:
;easy button coordinates
mov [minX], 0018h
mov [maxX], 00D0h
mov [maxY], 00A0h
mov [minY], 0037h
cmp cx, [minX]
jl Normal
;----------------
continueee1:
cmp cx, [maxX]
jg Normal
;----------------
continueee2:
cmp dx, [maxY]
jg Normal
;----------------
continueee3:
cmp dx, [minY]
jl Normal
;assining the substracter and the speed limit of the timer
mov [timeSubstracter], 1
mov [limit],5
jmp doneeee
Normal:
mov [minX], 00E4h
mov [maxX], 019Ah
mov [maxY], 00A1h
mov [minY], 0039h
cmp cx, [minX]
jl Hard
;----------------
continueeee1:
cmp cx, [maxX]
jg Hard
;----------------
continueeee2:
cmp dx, [maxY]
jg Hard
;----------------
continueeee3:
cmp dx, [minY]
jl Hard
mov [timeSubstracter], 2
mov [limit],4
jmp doneeee
Hard:
mov [minX], 01AEh
mov [maxX], 0268h
mov [maxY], 00A0h
mov [minY], 0037h
cmp cx, [minX]
jl NoButtonDetected
;----------------
continueeeee1:
cmp cx, [maxX]
jg NoButtonDetected
;----------------
continueeeee2:
cmp dx, [maxY]
jg NoButtonDetected
;----------------
continueeeee3:
cmp dx, [minY]
jl NoButtonDetected
mov [timeSubstracter], 5
mov [limit],3
jmp doneeee
;----------------
NoButtonDetected:
jmp MouseLPPP
doneeee:
pop dx
pop cx
pop bx
pop ax
ret
endp SelectLevel
;checking if the user pressed on the red button
proc IsOnRed
mov [minX], 0150h
mov [maxX], 0258h
mov [maxY], 005Fh
mov [minY], 000Eh
call IsOnColor
mov ax, [redSound]
call PlaySound
ret
endp IsOnRed
proc IsOnBlue
mov [minX], 0024h
mov [maxX], 012Ah
mov [maxY], 005Fh
mov [minY], 000Eh
call IsOnColor
mov ax, [blueSound]
call PlaySound
ret
endp IsOnBlue
proc IsOnYellow
mov [minX], 0024h
mov [maxX], 012Ah
mov [maxY], 00BAh
mov [minY], 006Ah
call IsOnColor
mov ax, [yellowSound]
call PlaySound
ret
endp IsOnYellow
proc IsOnGreen
mov [minX], 0150h
mov [maxX], 0258h
mov [maxY], 00BAh
mov [minY], 006Ah
call IsOnColor
mov ax, [greenSound]
call PlaySound
ret
endp IsOnGreen
;creates a pixel according to the x,y coordinates
proc createPixel
push ax
push bx
push cx
push dx
mov bh,0h
mov cx, [x]
mov dx, [y]
mov ah,0ch
int 10h
pop dx
pop cx
pop bx
pop ax
ret
endp createPixel
proc createLine
push [x]
push ax
push bx
push cx
push dx
mov cx, [horLength]
formX :
call createPixel
add [x],1
loop formX
pop dx
pop cx
pop bx
pop ax
pop [x]
ret
endp createLine
proc createRec
push[y]
push ax
push bx
push cx
push dx
mov cx, [verLength]
formRec:
call createLine
add [y],1
loop formRec
pop dx
pop cx
pop bx
pop ax
pop[y]
ret
endp createRec
proc printMessage
;--------------------
mov dl, 5 ;Column
mov dh, 2 ;Row
mov bh, 0 ;Display page
mov ah, 02h ;SetCursorPosition
int 10h
mov dx, offset levelMessage
mov ah,9
int 21h
ret
endp printMessage
proc printScore
;--------------------
mov dl, 110 ;Column
mov dh, 87 ;Row
mov bh, 0 ;Display page
mov ah, 02h ;SetCursorPosition
int 10h
mov dx, offset scoreMessage
mov ah,9
int 21h
;displaying number of points
mov ax, [points]
mov bx, 10 ;initializes divisor
mov dx, 0000h ;clears dx
mov cx, 0000h ;clears cx
;splitting process starts here
dloop1:
mov dx, 0000h ;clears dx during jump
div bx ;divides ax by bx
push dx ;pushes dx(remainder) to stack
inc cx ;increments counter to track the number of digits
cmp ax, 0 ;checks if there is still something in ax to divide
jne dloop1 ;jumps if ax is not zero
dloop2:
pop dx ;pops from stack to dx
add dx, 30h ;converts to it's ascii equivalent
mov ax,dx
mov bl, 254 ;Color is cyan
mov bh, 0 ;Display page
mov ah, 0Eh ;Teletype
int 10h
loop dloop2 ;loops till cx equals zero
inc [points]
ret ;returns control
endp printScore
;randomly generates a number from 0-3 and picking which color will be played now according to the number
proc PickAColor
mov ax, 40h
mov es, ax
mov ax, [es:6Ch]
and al, 00000011b
comparison1:
cmp al, 00
jne comparison2
mov bx, [index]
mov dl, 00
mov[ColorsPlayed + bx],dl
inc [index]
jmp finish
;-----------
comparison2:
cmp al, 01
jne comparison3
mov bx, [index]
mov dl, 01
mov[ColorsPlayed + bx],dl
inc [index]
jmp finish
;-----------
comparison3:
cmp al, 02
jne comparison4
mov bx, [index]
mov dl, 02
mov[ColorsPlayed + bx],dl
inc [index]
jmp finish
;-----------
comparison4:
cmp al, 03
jne comparison1
mov bx, [index]
mov dl, 03
mov[ColorsPlayed + bx],dl
inc [index]
jmp finish
finish:
ret
endp PickAColor
;playing and painting all the previos buttons that are stored in the array and the new picked button
proc PlayColors
checkColorsToPlay:
mov bx, [checkerForPlay]
compareToNull:
cmp [ColorsPlayed + bx], 5
je return
;-----------
compareToRed:
cmp [ColorsPlayed + bx], 00
jne compareToBlue
call GoRed
inc [checkerForPlay]
jmp checkColorsToPlay
;------------
compareToBlue:
cmp [ColorsPlayed + bx], 01
jne compareToYellow
call GoBlue
inc [checkerForPlay]
jmp checkColorsToPlay
;-------------
compareToYellow:
cmp [ColorsPlayed + bx], 02
jne compareToGreen
call GoYellow
inc [checkerForPlay]
jmp checkColorsToPlay
;-------------
compareToGreen:
cmp [ColorsPlayed + bx], 03
jne return
call GoGreen
inc [checkerForPlay]
jmp checkColorsToPlay
return:
mov [checkerForPlay], 0
ret
endp PlayColors
;checking the user's left click mouse input to match all the previos buttons that need to be clicked (array) and the new picked button
proc PressColors
checkColorsToPress:
mov bx, [checkerForPress]
compareToNulll:
cmp [ColorsPlayed + bx], 5
je returnn
;-----------
compareToRedd:
cmp [ColorsPlayed + bx], 00
jne compareToBluee
call IsOnRed
inc [checkerForPress]
jmp checkColorsToPress
;------------
compareToBluee:
cmp [ColorsPlayed + bx], 01
jne compareToYelloww
call IsOnBlue
inc [checkerForPress]
jmp checkColorsToPress
;-------------
compareToYelloww:
cmp [ColorsPlayed + bx], 02
jne compareToGreenn
call IsOnYellow
inc [checkerForPress]
jmp checkColorsToPress
;-------------
compareToGreenn:
cmp [ColorsPlayed + bx], 03
jne returnn
call IsOnGreen
inc [checkerForPress]
jmp checkColorsToPress
returnn:
mov [checkerForPress], 0
mov cx, 18
call Timer
ret
endp PressColors
;red button animation && sound
proc GoRed
push [redColor]
push [redSound]
push [redButtonColor]
push bp
mov bp, sp
redColorr equ [bp + 6]
redSoundd equ [bp + 4]
redButtonColorr equ [bp + 2]
mov ax, redColorr
mov [x], 210
mov [y], 40
mov [horLength], 50
mov [verLength], 30
call createRec
mov ax, redSoundd
call PlaySound
mov ax, redButtonColorr
call createRec
pop bp
pop [redButtonColor]
pop [redSound]
pop [redColor]
ret
endp GoRed
;blue button animation && sound
proc GoBlue
push [blueColor]
push [blueSound]
push [blueButtonColor]
push bp
mov bp, sp
blueColorr equ [bp + 6]
blueSoundd equ [bp + 4]
blueButtonColorr equ [bp + 2]
mov ax, blueColorr
mov [x], 60
mov [y], 40
mov [horLength], 50
mov [verLength] ,30
call createRec
mov ax, blueSoundd
call PlaySound
mov ax, blueButtonColorr
call createRec
pop bp
pop [blueButtonColor]
pop [blueSound]
pop [blueColor]
ret
endp GoBlue
;yellow button animation && sound
proc GoYellow
push [yellowColor]
push [yellowSound]
push [yellowButtonColor]
push bp
mov bp, sp
yellowColorr equ [bp + 6]
yellowSoundd equ [bp + 4]
yellowButtonColorr equ [bp + 2]
mov ax, yellowColorr
mov [x], 57
mov [y], 130
mov [horLength], 50
mov [verLength] ,30
call createRec
mov ax, yellowSoundd
call PlaySound
mov ax, yellowButtonColorr
call createRec
pop bp
pop [yellowButtonColor]
pop [yellowSound]
pop [yellowColor]
ret
endp GoYellow
;green button animation && sound
proc GoGreen
push [greenColor]
push [greenSound]
push [greenButtonColor]
push bp
mov bp, sp
greenColorr equ [bp + 6]
greenSoundd equ [bp + 4]
greenButtonColorr equ [bp + 2]
mov ax, greenColorr
mov [x], 213
mov [y], 130
mov [horLength], 50
mov [verLength] ,30
call createRec
mov ax, greenSoundd
call PlaySound
mov ax, greenButtonColorr
call createRec
pop bp
pop [greenButtonColor]
pop [greenSound]
pop [greenColor]
ret
endp GoGreen
start:
mov ax, @data
mov ds, ax
;Graphic mode
mov ax, 13h
int 10h
;THE GAME!!!
;lets the user select the wanted level
call DrawLevelMenu
call printMessage
call InitializeMouse
call SelectLevel
;we do this so the mouse won't mess up the pcture printing
call DisableMouse
;opens the bmp file and draws it on the screen
call DrawBackground