-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeAppleGame.asm
1343 lines (1192 loc) · 32.3 KB
/
SnakeAppleGame.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
[org 0x0100]
jmp start
;for snake speed
delay1:
push cx
push dx
mov cx,[delay1var1] ;
loop1:
; Inner loop for 10000 cycles
mov dx,[delay1var2]
nestedloop:
dec dx
jnz nestedloop
dec cx
jnz loop1
pop dx
pop cx
ret
;for word snake game and instructions
delay2:
push cx
push dx
mov cx,100 ;
loop2:
; Inner loop for 10000 cycles
mov dx,200
nestedloop2:
dec dx
jnz nestedloop2
dec cx
jnz loop2
pop dx
pop cx
ret
delay3:
push cx
push dx
mov cx,500 ;
loop3:
; Inner loop for 10000 cycles
mov dx,500
nestedloop3:
dec dx
jnz nestedloop3
dec cx
jnz loop3
pop dx
pop cx
ret
play_sound:
push bp
mov bp, sp
push ax
push bx
push cx
mov al, 182
out 43h, al
mov ax,3560 ;[bp+4] move the passed frequency
out 42h, al
mov al, ah
out 42h, al
in al, 61h
or al, 00000011b
out 61h, al
mov bx, 25
pause1:
mov cx,2000 ; [bp+6]
pause2:
dec cx
jne pause2
dec bx
jne pause1
in al, 61h
and al, 11111100b
out 61h, al
pop cx
pop bx
pop ax
pop bp
ret
play_sound1:
push bp
mov bp, sp
push ax
push bx
push cx
mov al, 182
out 43h, al
mov ax,3560 ;[bp+4] move the passed frequency
out 42h, al
mov al, ah
out 42h, al
in al, 61h
or al, 00000011b
out 61h, al
mov bx, 25
pause3:
mov cx,100 ; [bp+6]
pause4:
dec cx
jne pause4
dec bx
jne pause3
in al, 61h
and al, 11111100b
out 61h, al
pop cx
pop bx
pop ax
pop bp
ret
; subroutine to clear the screen for frontpage and backpage
clrscr1:
push es
push ax
push cx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
xor di, di ; point di to top left column
mov ax,0x0020 ; space char in normal attribute
mov cx, 2000 ; number of screen locations
cld ; auto increment mode
rep stosw ; clear the whole screen
pop di
pop cx
pop ax
pop es
ret
; subroutine to clear the screen for game
clrscr2:
push es
push ax
push cx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
xor di, di ; point di to top left column
mov ax,0x7020 ; space char in normal attribute
mov cx, 2000 ; number of screen locations
cld ; auto increment mode
rep stosw ; clear the whole screen
pop di
pop cx
pop ax
pop es
ret
printhor: ;Subroutine for printing horizontal borders
push bp
mov bp, sp
push es
push ax
push cx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov al, 80 ; load al with columns per row
mul byte [bp+10] ; multiply with y position
add ax, [bp+12] ; add x position
shl ax, 1 ; turn into byte offset
mov di, ax ; point di to required location
mov al, [bp+6] ; mov space in al
mov cx, [bp+4] ; load length of string in cx
mov ah, [bp+8] ; load attribute in ah
next:
mov [es:di], ax ; show this char on screen
add di, 2 ; move to next screen location
loop next ; repeat the operation cx times
pop di
pop cx
pop ax
pop es
pop bp
ret 10
printver: ;subroutine for Printing Vertical Borders
push bp
mov bp, sp
push es
push ax
push cx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov al, 80 ; load al with columns per row
mul byte [bp+10] ; multiply with y position
add ax, [bp+12] ; add x position
shl ax, 1 ; turn into byte offset
mov di, ax ; point di to required location
mov al, [bp+6] ; mov space in al
mov cx, [bp+4] ; load length of string in cx
mov ah, [bp+8] ; load attribute in ah
next1:
mov [es:di], ax ; show this char on screen
add di, 160 ; move to next screen location
loop next1 ; repeat the operation cx times
pop di
pop cx
pop ax
pop es
pop bp
ret 10
printstr:
push bp
mov bp, sp
push es
push ax
push cx
push si
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov al, 80 ; load al with columns per row
mul byte [bp+10] ; multiply with y position
add ax, [bp+12] ; add x position
shl ax, 1 ; turn into byte offset
mov di,ax ; point di to required location
mov si, [bp+6] ; point si to string
mov cx, [bp+4] ; load length of string in cx
mov ah, [bp+8] ; load attribute in ah
nextstrchar:
mov al, [si] ; load next char of string
mov [es:di], ax ; show this char on screen
add di, 2
add si, 1 ; move to next char in string
loop nextstrchar ; repeat the operation cx times
pop di
pop si
pop cx
pop ax
pop es
pop bp
ret 10
printstr1:
push bp
mov bp, sp
push es
push ax
push cx
push si
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov al, 80 ; load al with columns per row
mul byte [bp+10] ; multiply with y position
add ax, [bp+12] ; add x position
shl ax, 1 ; turn into byte offset
mov di,ax ; point di to required location
mov si, [bp+6] ; point si to string
mov cx, [bp+4] ; load length of string in cx
mov ah, [bp+8] ; load attribute in ah
nextmsgch:
mov al, [si] ; load next char of string
mov [es:di], ax ; show this char on screen
add di, 2
add si, 1 ; move to next char in string
call delay2
loop nextmsgch ; repeat the operation cx times
pop di
pop si
pop cx
pop ax
pop es
pop bp
ret 10
print_score:
push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov ax, [Score] ; load number in ax
mov bx, 10 ; use base 10 for division
mov cx, 0 ; initialize count of digits
nextdigit:
mov dx, 0 ; zero upper half of dividend
div bx ; divide by 10
add dl, 0x30 ; convert digit into ascii value
push dx ; save ascii value on stack
inc cx ; increment count of values
cmp ax, 0 ; is the quotient zero
jnz nextdigit
mov di,248
nextpos: pop dx ; remove a digit from the stack
mov dh, 0x09 ; use normal attribute
mov [es:di], dx ; print char on screen
add di, 2 ; move to next screen location
loop nextpos ; repeat for all digits on stack
pop di
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret
print_score1:
push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov ax, [Score] ; load number in ax
mov bx, 10 ; use base 10 for division
mov cx, 0 ; initialize count of digits
nextdigit1:
mov dx, 0 ; zero upper half of dividend
div bx ; divide by 10
add dl, 0x30 ; convert digit into ascii value
push dx ; save ascii value on stack
inc cx ; increment count of values
cmp ax, 0 ; is the quotient zero
jnz nextdigit1
mov di,1848
nextpos1:
pop dx ; remove a digit from the stack
mov dh, 0x09 ; use normal attribute
mov [es:di], dx ; print char on screen
add di, 2 ; move to next screen location
loop nextpos1 ; repeat for all digits on stack
pop di
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret
snake_grow:
push ax
push bx
push si
mov si,[snakelength]
shl si,1 ;multiply by 2 as array of snake is of 2 bytes
sub si,2
mov ax,[snakepos+si]
sub si,2
mov bx,[snakepos+si]
add si,6
sub bx,ax
add ax,bx
mov [snakepos+si],ax
mov [tailpos],ax
mov bx,[snakelength]
add bx,1
mov [snakelength],bx
pop si
pop bx
pop ax
ret
print_snake:
mov ax,0xb800
mov es,ax
mov ax,0x7020
mov di,[tailpos]
mov [es:di],ax
mov si,0
mov ax,[head] ;storing attribute and character of head
mov cx,[snakelength]
mov di,[snakepos+si]
mov [es:di],ax ;prints head
sub cx,1
add si,2
mov ax,[body] ;prints body
body_loop:
mov di,[snakepos+si]
mov [es:di],ax
add si,2
loop body_loop
mov [tailpos],di
ret
change_pos_left: ;swap indexes and changes head
mov si,[snakelength]
add si,[snakelength]
sub si,2
mov cx,[snakelength]
sub cx,1
loop_pos:
mov ax,[snakepos+si-2]
mov [snakepos+si],ax
sub si,2
loop loop_pos
mov ax,[snakepos+0]
sub ax,2
mov [snakepos+0],ax
ret
change_pos_right: ;swap indexes and changes head
mov si,[snakelength]
add si,[snakelength]
sub si,2
mov cx,[snakelength]
sub cx,1
loop_pos1:
mov ax,[snakepos+si-2]
mov [snakepos+si],ax
sub si,2
loop loop_pos1
mov ax,[snakepos+0]
add ax,2
mov [snakepos+0],ax
ret
change_pos_up: ;swap indexes and changes head
mov si,[snakelength]
add si,[snakelength]
sub si,2
mov cx,[snakelength]
sub cx,1
loop_pos2:
mov ax,[snakepos+si-2]
mov [snakepos+si],ax
sub si,2
loop loop_pos2
mov ax,[snakepos+0]
sub ax,160
mov [snakepos+0],ax
ret
change_pos_down: ;swap indexes and changes head
mov si,[snakelength]
add si,[snakelength]
sub si,2
mov cx,[snakelength]
sub cx,1
loop_pos3:
mov ax,[snakepos+si-2]
mov [snakepos+si],ax
sub si,2
loop loop_pos3
mov ax,[snakepos+0]
add ax,160
mov [snakepos+0],ax
ret
move_up:
push ax
push cx
push di
push si
call change_pos_up ;changing positions 1 step per call
call delay1
call print_snake
call collision ;checks for collision with borders
pop si
pop di
pop cx
pop ax
ret
move_down:
push ax
push cx
push di
push si
call change_pos_down ;changing positions 1 step per call
call delay1
call print_snake
call collision ;checks for collision with borders
pop si
pop di
pop cx
pop ax
ret
move_left:
push ax
push cx
push di
push si
call change_pos_left ;changing positions 1 step per call
call delay1
call print_snake
call collision ;checks for collision with borders
pop si
pop di
pop cx
pop ax
ret
move_right:
push ax
push cx
push di
push si
call change_pos_right ;changing positions 1 step per call
call delay1
call print_snake
call collision ;checks for collision with borders and body
pop si
pop di
pop cx
pop ax
ret
collision:
push ax
push bx
push cx
push si
mov ax,[snakepos+0] ;storing head position
mov cx,[borderlength] ;storing length of border for loop
mov si,0
loop_bordercollision:
mov bx,[border+si]
add si,2
cmp ax,bx ;cmp each border posotion with head
je terminate_program
loop loop_bordercollision
mov cx,[snakelength] ;storing length of snake for loop
sub cx,1
mov si,2
loop_bodycollision:
mov bx,[snakepos+si] ;storing position of second part of snake
add si,2
cmp ax,bx
je terminate_program
loop loop_bodycollision
pop si
pop cx
pop bx
pop ax
ret
terminate_program:
pop si
pop cx
pop bx
pop ax
call clrscr1
mov ax,15
push ax ; push x position
mov ax,5
push ax ; push y position
mov ax, 0x0C ; blue on black attribute
push ax ; push attribute
mov ax,end1
push ax ; push address of message
mov ax,[endlen]
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax,15
push ax ; push x position
mov ax,6
push ax ; push y position
mov ax, 0x0C ; blue on black attribute
push ax ; push attribute
mov ax,end2
push ax ; push address of message
mov ax,[endlen]
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax,15
push ax ; push x position
mov ax,7
push ax ; push y position
mov ax, 0x0C ; blue on black attribute
push ax ; push attribute
mov ax,end3
push ax ; push address of message
mov ax,[endlen]
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax,15
push ax ; push x position
mov ax,8
push ax ; push y position
mov ax, 0x0C ; blue on black attribute
push ax ; push attribute
mov ax,end4
push ax ; push address of message
mov ax,[endlen]
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 1
push ax ; push x position
mov ax, 11
push ax ; push y position
mov ax, 0x09 ; blue on black attribute
push ax ; push attribute
mov ax,scoreword
push ax ; push address of message
push word [scorewlen] ; push message length
call printstr1 ; call the printstr subroutine
call print_score1
loop_sound:
mov cx,500
call play_sound1
loop loop_sound
mov ax, 0x4c00 ; terminate program
int 0x21
check_valid:
push ax
push bx
push cx
push si
mov al,80 ;storing apple position
mul byte[appleY]
add ax,[appleX]
shl ax,1
mov cx,[borderlength] ;storing length of border for loop
mov si,0
loop_checkborder:
mov bx,[border+si]
add si,2
cmp ax,bx ;cmp each border posotion with head
je GenNextApple
loop loop_checkborder
xor bx,bx
mov cx,[snakelength] ;storing length of snake for loop
mov si,0
loop_checksnake:
mov bx,[snakepos+si] ;storing position of second part of snake
add si,2
cmp ax,bx
je GenNextApple
loop loop_checksnake
xor bx,bx
mov bx,[appleY]
cmp bx,0
je GenNextApple
cmp bx,1
je GenNextApple
cmp bx,2
je GenNextApple
pop si
pop cx
pop bx
pop ax
ret
;Generate next apple position
GenNextApple:
push bp
mov bp,sp
push ax
push cx
push dx
generate_loop: ; Generate random X and Y positions within the board dimensions
; Generate X position
xor ah,ah ; Set function code for random number generation (e.g., using BIOS)
int 1Ah ; Call BIOS function to get current time
mov ax,dx ; Store current time in BX
xor dx,dx ; Clear AX
mov cx,160 ; Load board width
div cx ; Divide by board width
mov word[appleX], dx ; Store the X position in appleX
; Generate Y position
xor ah,ah ; Set function code for random number generation (e.g., using BIOS)
int 0x1A ; Call BIOS function to get current time
mov ax,dx ; Store current time in BX
xor dx,dx ; Clear AX
mov cx,25 ; Load board height
div cx ; Divide by board height
mov [appleY], dx ; Store the Y position in appleY
call check_valid
pop dx
pop cx
pop ax
pop bp
ret
;Print apple on screen
print_apple:
push bp
mov bp, sp
push es
push ax
push di
call GenNextApple
mov ax,0xb800
mov es,ax
mov al,80 ;storing apple position
mul byte[appleY]
add ax,[appleX]
shl ax,1
mov di,ax
mov ax,0x4041
mov [es:di],ax
pop di
pop ax
pop es
pop bp
ret
eat_apple:
push ax
push bx
push cx
push dx
mov al,80 ;storing apple position
mul byte[appleY]
add ax,[appleX]
shl ax,1
mov bx,[snakepos+0]
cmp ax,bx
jne return_eat
cmp word[delay1var1],0
je continue
sub word[delay1var1],50
continue:
call play_sound
add word[Score],5
call print_score
call snake_grow
call print_apple
return_eat:
pop dx
pop cx
pop bx
pop ax
ret
kbisr: ;Takes input(move snake),using interrupts
push ax
push es
mov ax, 0xb800
mov es, ax
in al, 0x60
left_compare:
mov ah,1
cmp [right],ah
je right_compare
cmp al, 30
jne right_compare
mov al,1
mov byte [left],al
mov al,0
mov byte[right],al
mov byte[up],al
mov byte[down],al
jmp nomatch
right_compare:
cmp [left],ah
je up_compare
cmp al, 32
jne up_compare
mov al,1
mov byte [right],al
mov al,0
mov byte[left],al
mov byte[up],al
mov byte[down],al
jmp nomatch
up_compare:
cmp [down],ah
je down_compare
cmp al, 17
jne down_compare
mov al,1
mov byte [up],al
mov al,0
mov byte[right],al
mov byte[left],al
mov byte[down],al
jmp nomatch
down_compare:
cmp [up],ah
je exitgame
cmp al, 31
jne exitgame
mov al,1
mov byte [down],al
mov al,0
mov byte[right],al
mov byte[up],al
mov byte[left],al
jmp nomatch
exitgame:
cmp al, 16
jne nomatch
or byte [close],1
nomatch:
mov al, 0x20
out 0x20, al ; send EOI to PIC
pop es
pop ax
iret
;///////////////////////Main Driver Code
start:
call clrscr1
mov ax, 2
push ax ; push x position
mov ax, 0
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes1
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 1
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes2
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 2
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes3
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 3
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes4
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 4
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes5
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 5
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes6
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 6
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes7
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
call delay3
mov ax, 2
push ax ; push x position
mov ax, 7
push ax ; push y position
mov ax, 0x0C ; red on black attribute
push ax ; push attribute
mov ax,Snakes8
push ax ; push address of message
mov ax,[Snakeslength] ; push message length
push ax
call printstr ; call the printstr subroutine
mov ax,0
push ax ; push x position
mov ax,10
push ax ; push y position
mov ax, 0x09 ; blue on black attribute
push ax ; push attribute
mov ax,msg1
push ax ; push address of message
mov ax,[msglen]
push ax
call printstr1 ; call the printstr subroutine
mov ax,0
push ax ; push x position
mov ax,11
push ax ; push y position
mov ax, 0x09 ; blue on black attribute
push ax ; push attribute
mov ax,msg2
push ax ; push address of message
mov ax,[msglen]
push ax
call printstr1 ; call the printstr subroutine