-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.asm
727 lines (587 loc) · 33.6 KB
/
pong.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
.286
SetVideMode MACRO
mov ah,0
mov al,13h
int 10h
ENDM
ResetVideMode MACRO
mov ah,0
mov al,3h
int 10h
ENDM
PressAnyKey MACRO
mov ah,01h
waitKey: int 16h
jz waitKey
ENDM
GetSystemTime MACRO
mov ah,2ch ;get the system time
int 21h ; CH = hour, CL = minute, DH = seconds, DL = 1/100s
ENDM
setIntteruptHandle MACRO
;getting int09h interrupt vector handler
cli ;turn off interrupt flag
pusha
;get the interrupt 09 address
mov ax,3509h
int 21h
;save the original interrupt address
mov int9Off, bx
mov int9Seg, es
popa
;setting the new int 09h interrupt vector handler
push ds
mov ax,cs
mov ds,ax ;load the segment of the new interrupt
lea dx,checkKeyboard ;load the offset of the new interrupt
mov ax,2509h ; int 21/25h at interrupt 09
int 21h
pop ds
sti
ENDM
resetInterruptHandle MACRO
cli
mov ax,int9Seg
mov dx,int9Off
push ds
mov ds,ax
mov ax,2509h
int 21h
pop ds
sti
ENDM
.model small
.data
time db 0h ;track system time
WINDOW_WIDTH equ 320
WINDOW_HEIGHT equ 200
ball_x dw 155 ;x position (column) of ball
ball_y dw 96 ;y position (row) of ball
ball_home_x equ 155 ; x coord of the center of the screen
ball_home_y equ 96 ; y coord of the center of the screen
ball_width equ 5h ; width of the ball in pixels
ball_height equ 6h ; height of the ball in pixels
ball_velocity_x dw 5h ;ball velocity in the x direction
ball_velocity_y dw 2h ;ball velocity in the y direction
paddle_left_y dw 10 ;left paddle position for the top of the screen
paddle_right_y dw 10 ;right paddle position for the top of the screen
paddle_left_x equ 10 ;left paddle x coord
paddle_right_x equ 305 ;right paddle x coord
paddle_velocity equ 10 ;paddle move speed
paddle_height equ 40 ;paddle height
paddle_width equ 5 ;paddle width
int9Seg dw ? ; default interrupt 9 segment
int9Off dw ? ;default interrupt 9 offset
lpMovingUp dw 0 ;left paddle move state 0 = no movement, -paddle_velocity = move up
rpMovingUp dw 0 ;right paddle move state 0 = no movement, -paddle_velocity = move up
lpMovingDown dw 0 ;left paddle move state 0 = no movement, paddle_velocity = move down
rpMovingDown dw 0 ;right paddle move state 0 = no movement, paddle_velocity = move down
exitFlag db 0
left_score db 0
right_score db 0
game_active db 1
winner_text db "Game Over$"
left_txt db "Left Won$"
right_txt db "Right Won$"
r_restart db "Restart - R key$"
esc_exit db "Exit - ESC key$"
main_menu_txt db "Main Menu$"
start_txt db "Start - Enter key$"
.stack 100h
.code
checkKeyboard proc
pusha ;push all regs to the stack
pushf ;push flag register to the stack
in al,60h ;read the keyboard scancode
cmp al,11h ;check for W Keydown Scancode
jne skipMoveLeftUp
mov lpMovingUp,-1*paddle_velocity ;set left paddle move up flag to -1 to move up
jmp exitKeyboard
skipMoveLeftUp:
cmp al,11h + 80h ;check for W Keyup Scancode
jne skipStopLeftUp
mov lpMovingUp,0 ;set left paddle move up flag to zero
jmp exitKeyboard
skipStopLeftUp:
cmp al,1fh ;check for S Keydown Scancode
jne skipMoveLeftDown
mov lpMovingDown,1*paddle_velocity ;set left paddle move down flag to 1 to move down
jmp exitKeyboard
skipMoveLeftDown:
cmp al,1fh + 80h ;check for S Keyup Scancode
jne skipStopLeftDown
mov lpMovingDown,0 ;set left paddle move down flag to zero
jmp exitKeyboard
skipStopLeftDown:
;right paddle
cmp al,48h ;check for Up arrow Keydown Scancode
jne skipMoveRightUp
mov rpMovingUp,-1*paddle_velocity ;set right paddle move up flag to -1 to move up
jmp exitKeyboard
skipMoveRightUp:
cmp al,48h + 80h ;check for Up arrow Keyup Scancode
jne skipStopRightUp
mov rpMovingUp,0 ;set right paddle move up flag to zero
jmp exitKeyboard
skipStopRightUp:
cmp al,50h ;check for Down arrow Keydown Scancode
jne skipMoveRightDown
mov rpMovingDown,1*paddle_velocity ;set right paddle move down flag to 1 to move down
jmp exitKeyboard
skipMoveRightDown:
cmp al,50h + 80h ;check for Down arrow Keyup Scancode
jne skipStopRightDown
mov rpMovingDown,0 ;set right paddle move down flag to zero
jmp exitKeyboard
skipStopRightDown:
cmp al,01h ;check for the ESC key
jne exitKeyboard
mov exitFlag,1 ;set the exit flag
exitKeyboard:
popf ;retrive flag register form the stack
popa ;retrive all other flags form the stack
mov al, 20h ;The non specific EOI (End Of Interrupt)
out 20h, al
iret ;return from interrupt
checkKeyboard endp
main proc far
mov ax,@data
mov ds,ax
call MainMenu
initlize:
SetVideMode
setIntteruptHandle ;turn on interrupt flag
mov lpMovingDown,0
mov rpMovingDown,0
mov lpMovingUp,0
mov rpMovingUp,0
mov paddle_left_y,80
mov paddle_right_y,80
call DrawPaddles ;draw paddles for the first time
mainLoop:
cmp exitFlag,1
je exitApp
cmp game_active,0
je showGameOver
GetSystemTime ;dl = 1/100s
cmp dl,time ;check for the same frame
je mainLoop ;check for time again
mov time,dl
call moveLeftPaddle
call moveRightPaddle
call ClearBall
call MoveBall
call DrawBall
call printScore
call checkWin
jmp mainLoop
showGameOver:
resetInterruptHandle
call DrawGameOver
mov game_active,1
mov left_score,0
mov right_score,0
jmp initlize
exitApp:
ResetVideMode
resetInterruptHandle
mov al,'F'
mov ah,08H
mov bh,0
mov bl,0fh
mov cx,10
int 10h
mov ah,4ch
int 21h
main endp
DrawBall proc
mov cx, ball_x ;left ball position
mov dx, ball_y ;top ball posititon
horizontal:
mov ah, 0ch ; pixel color interrupt
mov al, 0Fh ;pixel color white
mov bh,0h
int 10h ;call interrupt
inc cx ;move the x pos to the right
mov ax,cx ;put current x pos in ax
sub ax,ball_x ;decrement by the defualt pos
cmp ax,ball_width ;check if ball WINDOW_WIDTH equal to predifiend size
jl horizontal ;if not jump to print the next pixel
mov cx,ball_x
inc dx
mov ax,dx
sub ax,ball_y
cmp ax,ball_height
jl horizontal
ret
DrawBall endp
ClearBall proc
mov cx, ball_x ;left ball position
mov dx, ball_y ;top ball posititon
clear_horizontal:
mov ah, 0ch ; pixel color interrupt
mov al, 00h ;pixel color white
mov bh,0h
int 10h ;call interrupt
inc cx ;move the x pos to the right
mov ax,cx ;put current x pos in ax
sub ax,ball_x ;decrement by the defualt pos
cmp ax,ball_width ;check if ball WINDOW_WIDTH equal to predifiend size
jl clear_horizontal ;if not jump to print the next pixel
mov cx,ball_x
inc dx
mov ax,dx
sub ax,ball_y
cmp ax,ball_height
jl clear_horizontal
ret
ClearBall endp
DrawPaddles proc
mov cx, paddle_left_x ;left ball position
mov dx, paddle_left_y ;top ball posititon
paddle1_horizontal:
mov ah, 0ch ; pixel color interrupt
mov al, 0Fh ;pixel color white
mov bh,0h
int 10h ;call interrupt
inc cx ;move the x pos to the right
mov ax,cx ;put current x pos in ax
sub ax,paddle_left_x ;decrement by the defualt pos
cmp ax,paddle_width ;check if ball WINDOW_WIDTH equal to predifiend size
jl paddle1_horizontal ;if not jump to print the next pixel
mov cx,paddle_left_x
inc dx
mov ax,dx
sub ax,paddle_left_y
cmp ax,paddle_height
jl paddle1_horizontal
; Drawing the right paddle
mov cx, paddle_right_x ;right ball position
mov dx, paddle_right_y ;top ball posititon
paddle2_horizontal:
mov ah, 0ch ; pixel color interrupt
mov al, 0Fh ;pixel color white
mov bh,0h
int 10h ;call interrupt
inc cx ;move the x pos to the right
mov ax,cx ;put current x pos in ax
sub ax,paddle_right_x ;decrement by the defualt pos
cmp ax,paddle_width ;check if ball WINDOW_WIDTH equal to predifiend size
jl paddle2_horizontal ;if not jump to print the next pixel
mov cx,paddle_right_x
inc dx
mov ax,dx
sub ax,paddle_right_y
cmp ax,paddle_height
jl paddle2_horizontal
ret
DrawPaddles endp
ClearRightPaddle PROC
mov cx, paddle_right_x ;x coord of right paddle
mov dx, paddle_right_y ;y coord of the right paddle
cpaddle2_horizontal:
mov ah, 0ch ; pixel color interrupt
mov al, 00h ;pixel color black
mov bh,0h
int 10h ;call interrupt
inc cx ;move the x pos to the right
mov ax,cx ;put current x pos in ax
sub ax,paddle_right_x ;decrement by the defualt pos
cmp ax,paddle_width ;check if paddle width equal to predifiend size
jl cpaddle2_horizontal ;if not jump to print the next pixel
mov cx,paddle_right_x
inc dx
mov ax,dx
sub ax,paddle_right_y
cmp ax,paddle_height
jl cpaddle2_horizontal
ret
ClearRightPaddle ENDP
ClearLeftPaddle proc
mov cx, paddle_left_x ;x coord of left paddle
mov dx, paddle_left_y ;y coord of left paddle
cpaddle1_horizontal:
mov ah, 0ch ; pixel color interrupt
mov al, 00h ;pixel color black
mov bh,0h
int 10h ;call interrupt
inc cx ;move the x pos to the right
mov ax,cx ;put current x pos in ax
sub ax,paddle_left_x ;decrement by the defualt pos
cmp ax,paddle_width ;check if paddle width equal to predifiend size
jl cpaddle1_horizontal ;if not jump to print the next pixel
mov cx,paddle_left_x
inc dx
mov ax,dx
sub ax,paddle_left_y
cmp ax,paddle_height
jl cpaddle1_horizontal
ret
ClearLeftPaddle endp
MoveBall proc
;check collision with the left pedal
;check if ball passed the left paddle
cmp ball_x,paddle_left_x + paddle_width
jg skipLeftCheck
cmp ball_x,paddle_left_x
jle skipLeftCheck
;check for ball below the top of the paddle
mov ax, ball_y
add ax,ball_height
cmp ax,paddle_left_y
jl skipLeftCheck
;check for ball above the bottom of the paddle
mov ax, paddle_left_y
add ax,paddle_height
cmp ball_y,ax
jg skipLeftCheck
jmp NegVelocityX
skipLeftCheck:
;check if ball passed the right paddle
cmp ball_x,paddle_right_x - ball_width
jl skipRightCheck
cmp ball_x,paddle_right_x
jge skipRightCheck
;check for ball below the top of the paddle
mov ax, ball_y
add ax,ball_height
cmp ax,paddle_right_y
jl skipRightCheck
;check for ball above the bottom of the paddle
mov ax, paddle_right_y
add ax,paddle_height
cmp ball_y,ax
jg skipRightCheck
jmp NegVelocityX
skipRightCheck:
;check if the ball passed the right wall
mov ax,ball_x
add ax,ball_width
cmp ax,WINDOW_WIDTH
jge ballOut
;check if the ball passed the left wall
cmp ball_x,0
jle ballOut
checkY:
mov ax,ball_y
add ax,ball_height
inc ax
cmp ax,WINDOW_HEIGHT
jge NegVelocityY
cmp ball_y,0000h
jle NegVelocityY
jmp moveB
moveB:
mov ax,ball_velocity_x
add ball_x,ax ;move the ball horizontally
mov ax,ball_velocity_y
add ball_y,ax ;move the ball vertically
ret
ballOut:
neg ball_velocity_x
cmp ball_x,0
jle rightPoint
inc left_score
call ResetBall
ret
rightPoint:
inc right_score
call ResetBall
ret
NegVelocityY:
neg ball_velocity_y
jmp moveB
NegVelocityX:
neg ball_velocity_x
jmp moveB
MoveBall endp
ResetBall proc
mov ball_x,ball_home_x
mov ball_y,ball_home_y
ret
ResetBall endp
moveLeftPaddle proc
mov ax,lpMovingUp
add ax,lpMovingDown
cmp ax,0
je exitMoveLeft
mov ax,paddle_left_y
add ax,lpMovingUp
add ax,lpMovingDown
cmp ax,0
jl exitMoveLeft
add ax,paddle_height
cmp ax,WINDOW_HEIGHT
jg exitMoveLeft
call ClearLeftPaddle
mov ax,paddle_left_y
add ax,lpMovingUp
add ax,lpMovingDown
mov paddle_left_y,ax
call DrawPaddles
jmp exitMoveLeft
exitMoveLeft:
ret
moveLeftPaddle endp
moveRightPaddle proc
mov ax,rpMovingUp
add ax,rpMovingDown
cmp ax,0
je exitMoveRight
mov ax,paddle_right_y
add ax,rpMovingUp
add ax,rpMovingDown
cmp ax,0
jl exitMoveRight
add ax,paddle_height
cmp ax,WINDOW_HEIGHT
jg exitMoveRight
call ClearRightPaddle
mov ax,paddle_right_y
add ax,rpMovingUp
add ax,rpMovingDown
mov paddle_right_y,ax
call DrawPaddles
jmp exitMoveRight
exitMoveRight:
ret
moveRightPaddle endp
printScore proc
mov ah,02h
mov bh,0H
mov dh,5
mov dl,5
int 10h
mov ah,09h
mov al,left_score
add al,'0'
mov bl,07h
mov cx,1
int 10h
mov ah,02h
mov bh,0H
mov dh,5
mov dl,34
int 10h
mov ah,09h
mov al,right_score
add al,'0'
mov bl,07h
mov cx,1
int 10h
ret
printScore endp
checkWin proc
cmp left_score,5
jge setWin
cmp right_score,5
jge setWin
ret
setWin: mov game_active,0
ret
checkWin endp
DrawGameOver PROC
SetVideMode
;set cursor postition to top left with a margin of 5
mov ah,02h
mov bh,0H
mov dh,5
mov dl,5
int 10h
;print the game over text
lea dx,winner_text
mov ah,9
int 21h
;move the cursor down
mov ah,02h
mov bh,0H
mov dh,6
mov dl,5
int 10h
;check which player won
mov al,left_score
cmp al,right_score
jl rightWinner
lea dx,left_txt
jmp leftWinner
rightWinner:
lea dx,right_txt
leftWinner: mov ah,9
int 21h ;print the winner
;move the cursor down
mov ah,02h
mov bh,0H
mov dh,7
mov dl,5
int 10h
;print the restart text
lea dx,r_restart
mov ah,9
int 21h
;move the cursor down
mov ah,02h
mov bh,0H
mov dh,8
mov dl,5
int 10h
;print the esc text
lea dx,esc_exit
mov ah,9
int 21h
;wait for ESC key or R key
waitKeyInput: mov ah, 0
int 16h
cmp ah,01h
jne skipExit1
mov exitFlag,1
ret
skipExit1:
cmp ah,13h
jne waitKeyInput
ret
DrawGameOver ENDP
MainMenu PROC
SetVideMode
;set cursor postition to top left with a margin of 5
mov ah,02h
mov bh,0H
mov dh,5
mov dl,5
int 10h
;print the main menu text
lea dx,main_menu_txt
mov ah,9
int 21h
;move the cursor down
mov ah,02h
mov bh,0H
mov dh,6
mov dl,5
int 10h
;print the Start text
lea dx,start_txt
mov ah,9
int 21h
;move the cursor down
mov ah,02h
mov bh,0H
mov dh,7
mov dl,5
int 10h
;print the esc text
lea dx,esc_exit
mov ah,9
int 21h
;wait for ESC key or Enter key
waitKeyInput2: mov ah, 0
int 16h
cmp ah,01h
jne skipExit2
mov exitFlag,1
ret
skipExit2:
cmp ah,1Ch
jne waitKeyInput2
ret
MainMenu ENDP
end main