-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuusi.asm
719 lines (557 loc) · 12.7 KB
/
uusi.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
BITS 16
;;;;;;;;;;;;;;
; CONSTANTS
;;;;;;;;;;;;;;
stacksize EQU 0200h ;define constant DEC 512
; starting address of video memory
videobase EQU 0a000h
; some colors
black EQU 0
green EQU 00110000b
blue EQU 00001001b
red EQU 00000100b
white EQU 00001111b
grey EQU 00000111b
; screen width in pixels, in the graphics mode 320x200
scrwidth EQU 320
;;;;;;;;;;;;;;
; DATA AND STACK SEGMENTS
;;;;;;;;;;;;;;
segment videoseg data
resb 320*200
segment memscreen data ;segment places the data here also in the compiled file
resb 320*200 ;reserve a byte(or 64000 of them)
segment background data
resb 320*200
segment mystack stack
resb stacksize
stacktop:
segment mydata data
foo resw 1 ;reserve a word aka 2 bytes, 1 word
pressesc dw 0 ;reserves a double word
oldintseg resw 1 ;reserve word
oldintoff resw 1 ;reserve word
delay dw 01h ;double word for default delay
gmode db 0
plocation dw 05A0h ;players starting location
slocation dw 037A0h ;styx starting location
move dw 0141h ;direction
moveselector dw 0 ;help determining direction
smove dw 01h ;movement for styx
playerColor db white ;define players starting color(changes constantly due to rotation for blinking)
lineColor EQU grey ;define line color for borders and trail
;;;;;;;;;;;;;;
; The code segment
;;;;;;;;;;;;;;
segment mycode code
; Subroutines here
copyBG: ;copy background routine
push ds
pusha
;Pointers
mov word si, 0
mov word di, 0
mov cx, 64000
;Segment registers to correct locations
mov ax, background;memscreen
mov es, ax
mov ax, videobase;background
mov ds, ax
;REPEAT COPY!
rep movsb
popa
pop ds
ret
copyMS: ;copy memscreen routine
push ds
pusha
;Pointers
mov word si, 0
mov word di, 0
mov cx, 64000
;Segment registers to correct locations
mov ax, videobase;memscreen
mov es, ax
mov ax, background;background
mov ds, ax
;REPEAT COPY!
rep movsb
popa
pop ds
ret
drawPlayer:
pusha
mov ax, videobase
mov es, ax ; move videobase to es
mov di, [plocation] ;player starting coord
mov dl, [playerColor] ;get players color
rol dl, 3 ;rotate left by 3 to cause blinking(CAUTION: might cause eye cancer)
mov [playerColor], dl ;store new color
;player is built of 5 pixels
mov byte [es:di], dl
sub di, 0140h
mov byte [es:di], dl
add di, 013fh
mov byte [es:di], dl
add di, 02h
mov byte [es:di], dl
add di, 013fh
mov byte [es:di], dl
;draw trail
mov ax, background
mov es, ax ;move (copy of)videobase to es
mov di, [plocation] ;player coord
mov byte [es:di], lineColor ;color of trail
popa
ret
drawStyx:
pusha
mov ax, videobase
mov es, ax
mov di, [slocation]
mov byte [es:di], green
add di, 01h
mov byte [es:di], green
add di, 01h
mov byte [es:di], green
sub di, 03h
mov byte [es:di], green
sub di, 01h
mov byte [es:di], green
sub di, 013Dh
mov byte [es:di], green
sub di, 01h
mov byte [es:di], green
sub di, 01h
mov byte [es:di], green
sub di, 013Fh
mov byte [es:di], green
add di, 03C0h
mov byte [es:di], green
sub di, 01h
mov byte [es:di], green
add di, 02h
mov byte [es:di], green
add di, 013Fh
mov byte [es:di], green
popa
ret
moveStyx:
pusha
mov cx, background
mov es, cx ; move bg to es
mov di, 0h
mov ax, [smove]
cmp ax, 01h
jz .sRight
jnz .sLeft
.sRight
mov bx, [slocation]
add bx, 03h
mov di, bx
mov dl, [es:di]
cmp dl, blue
jz .changeLeft
jnz .sEnd
.changeLeft
mov bx, 0FFFFh
mov [smove], bx
jmp .sEnd
.sLeft
mov ax, [smove]
cmp ax, 0FFFFh
jz .sEnd
mov bx, [slocation]
sub bx, 03h
mov di, bx
mov dl, [es:di]
cmp dl, blue
jz .changeRight
jnz .sEnd
.changeRight
mov bx, 01h
mov [smove], bx
jmp .sEnd
.sEnd
mov bx, [slocation]
add bx, 0FFFFh
mov [slocation], bx
popa
ret
;this routine checks(and moves) which location is valid for next location of player
movePlayer:
pusha
mov ax, [move]
mov [moveselector], ax
mov ax, [moveselector]
sub ax, 0140h
cmp ax, 01h ;cmp ax, 063Ch ;if we are at right top corner
jz .rightDir ; if we are at right corner jump to right top
jnz .continue1
.rightDir ;if going right
call .routineRight
.continue1
cmp ax, 0FFFFh
jz .leftDir
jnz .continue2
.leftDir ;if going left
call .routineLeft
.continue2
cmp ax, 0FEC0h
jz .upDir
jnz .continue3
.upDir ;if going up
call .routineUp
.continue3
cmp ax, 0140h
jz .downDir
jnz .leave
.downDir ;if going down
call .routineDown
.leave
popa
ret
.routineRight:
pusha
mov cx, background
mov es, cx ; move bg to es
mov di, 0h
mov bx, [plocation] ;store players location in bx
add bx, 0140h ;increment by 320
mov di, bx ;store result in di
mov dl, [es:di] ;store stuff starting from es pointer by di
cmp dl, lineColor ;compare it to line color
jz .rgoDown
jnz .isrgoUp
.rgoDown
mov word [move], 0280h
jmp .leaveRight
.isrgoUp
sub bx, 0280h
mov di, bx
mov dx, [es:di]
cmp dx, lineColor ;compare to line color
jz .rgoUp
jnz .leaveRight
.rgoUp
mov word [move], 0h
.leaveRight
mov ax, [plocation]
add ax, [move]
sub ax, 0140h
mov [plocation], ax
popa
ret
.routineLeft:
pusha
; below we fetch the background data
mov cx, background
mov es, cx ; move bg to es
mov di, 0h ;draw some shit here
mov bx, [plocation]
sub bx, 0140h
mov di, bx
mov dl, [es:di]
cmp dl, lineColor ;compare to line color
jz .lgoUp
jnz .islgoDown
.lgoUp
mov ax, 0h
mov word [move], ax
jmp .leaveLeft
.islgoDown
add bx, 0280h
mov di, bx
mov dx, [es:di]
cmp dx, lineColor ;compare to line color
jz .lgoDown
jnz .leaveLeft
.lgoDown
mov word [move], 0280h
.leaveLeft
mov ax, [plocation]
add ax, [move]
sub ax, 0140h
mov [plocation], ax
popa
ret
.routineUp:
pusha
mov cx, background
mov es, cx ; move bg to es
mov bx, [plocation]
add bx, 01h
mov di, bx
mov dl, [es:di] ; es:di is only one byte so instead of dx we put it to dl
cmp dl, lineColor ;compare to line color
jz .ugoRight
jnz .isugoLeft
.ugoRight
mov word [move], 0141h
jmp .leaveDown
.isugoLeft
sub bx, 02h
mov di, bx
mov dl, [es:di]
cmp dl, lineColor ;compare to line color
jz .ugoLeft
jnz .leaveUp
.ugoLeft
mov word [move], 013Fh
.leaveUp
mov ax, [plocation]
add ax, [move]
sub ax, 140h
mov [plocation], ax
popa
ret
.routineDown:
pusha
; below we fetch the background data
mov cx, background
mov es, cx ; move bg to es
mov bx, [plocation]
sub bx, 01h
mov di, bx
mov dl, [es:di] ; es:di is only one byte so instead of dx we put it to dl
cmp dl, lineColor ;compare to line color
jz .dgoLeft
jnz .isdgoRight
.dgoLeft
mov word [move], 013Fh
jmp .leaveDown
.isdgoRight
add bx, 02h
mov di, bx
mov dl, [es:di]
cmp dl, lineColor ;compare to line color
jz .dgoRight
;mov word [move], 013Fh
jnz .leaveDown
.dgoRight
mov word [move], 0141h
.leaveDown
mov ax, [plocation]
add ax, [move]
sub ax, 0140h
mov [plocation], ax
popa
ret
KeybInt:
push ds ; put the value of ds,ax to safety
push ax
mov ax,mydata ; Re-initialisation of
mov ds,ax ; the data segment
cli ; Disable other interrupts
; during this one
.getstatus:
in al, 64h
test al, 02h
loopnz .getstatus ; wait until the port is ready
in al,60h ; Get the scan code of
; the pressed/released key
; scan codes can be found in helppc
; interrupt services ->
; keyboard interrupt -> make codes
; here begins the actual key scanning
cmp al, 01h ; 1 is the 'make code' for ESC
jne .leftArrow ; if ESC was not pressed, continue
mov word [pressesc], 1
jmp .kbread
.leftArrow:
cmp al, 0004Bh
jne .rightArrow
mov word ax, [move]
cmp word ax, 0140h ; check if movement is zero
jz .moveLeft ; if zero, move to left
mov ax, 013Fh
mov [move], ax
jmp .kbread
.moveLeft
mov word ax, [plocation]
sub word ax, 01h
mov word [plocation], ax
mov ax, 013Fh
mov [move], ax
jmp .kbread
.rightArrow:
cmp al, 0004Dh
jne .upArrow
mov ax, [move]
cmp ax, 0140h ; check if movement is zero
jz .moveRight ; if zero, move to left
mov ax, 0141h
mov [move], ax
jmp .kbread
.moveRight
mov ax, [plocation]
add ax, 01h
mov [plocation], ax
mov ax, 0141h
mov [move], ax
jmp .kbread
.upArrow:
cmp al, 048h
jne .downArrow
mov ax, [move]
cmp ax, 0140h ; check if movement is zero
jz .moveUp ; if zero, move to left
mov ax, 0h
mov [move], ax
jmp .kbread
.moveUp
mov ax, [plocation]
sub ax, 0140h
mov [plocation], ax
mov ax, 0h
mov [move], ax
jmp .kbread
.downArrow:
cmp al, 050h
jne .stopMovement
mov ax, [move]
cmp ax, 0140h ; check if movement is zero
jz .moveDown ; if zero, move to left
mov ax, 0280h
mov [move], ax
jmp .kbread
.moveDown
mov ax, [plocation]
add ax, 0140h
mov [plocation], ax
mov ax, 0280h
mov [move], ax
jmp .kbread
.stopMovement:
cmp al, 02Ah
jne .cplus
mov ax, [move]
cmp ax, 0140h ; check if movement is zero
jz .startMovement ; if zero, move to left
mov ax, 0140h
mov [move], ax
jmp .kbread
.startMovement
mov ax, 0141h
mov [move], ax
jmp .kbread
.cplus:
cmp al, 04Eh ; 4E is the 'make code' for keypad plus
jne .cminus
mov ax, [delay]
inc ax
mov [delay], ax
jmp .kbread
.cminus:
cmp al, 04Ah ; 4A is the 'make code' for keypad minus
jne .kbread
mov ax, [delay]
dec ax
mov [delay], ax
.kbread:
in al,61h ; Send acknowledgment without
or al,10000000b ; modifying the other bits.
out 61h,al ;
and al,01111111b ;
out 61h,al ;
mov al,20h ; Send End-of-Interrupt signal
out 20h,al ;
sti ; Enable interrupts again
pop ax
pop ds ; Regain the ds,ax from stack
iret ; Return from interrupt
..start:
mov ax, mydata
mov ds, ax
mov ax, mystack
mov ss, ax
mov sp, stacktop
mov ah, 35h ;Note use of HEXADECIMAL!
mov al, 9 ;No need for h, because 9h = 9
;Alternatively, both at once: MOV AX, 3509h
int 21h ;Call interrupt 21,35 - note again the use of HEXADECIMAL values
mov [oldintseg], es
mov [oldintoff], bx
;after this line we will create our own interrupt handler :D:DDD:D
mov dx, KeybInt
mov ax, mycode
push ds ;to later get the data segment address back to DS we temp store it to stack
mov ds, ax
mov ah, 25h
mov al, 9
int 21h
pop ds ; here we get it back
call .graphmode
call .initbackground
call .mainloop
call .dosexit
;a routine for initializing the background
.initbackground:
mov ax, videobase
mov es, ax ; move videobase to es
mov di, 0504h
.loop: ;draw upper line
mov byte [es:di], lineColor
inc di
cmp di, 063Ch
jnz .loop
mov di, 0504h
mov cx, 0beh
.loop2: ;draw columns
mov byte [es:di], lineColor
add di, 0138h
mov byte [es:di], lineColor
add di, 08h
dec cx
jnz .loop2
.loop3: ;draw bottom line
mov byte [es:di], lineColor
inc di
cmp di, 0f3bdh
jnz .loop3
call copyBG ;copy the resulted background to memory
ret
.graphmode:
mov ah, 0Fh
int 10h
mov [gmode], al ;save the old mode
mov ah, 0
mov al, 13h ;set new mode
int 10h
ret
.mainloop:
mov word dx, [delay]
.pause1:
mov cx, 65535
.pause2:
dec cx
jne .pause2
dec dx
jne .pause1
call .draw
call movePlayer
call moveStyx
;below we check for esc
mov ax, [pressesc]
cmp ax, 0 ; 0-0 -> ZF = 0 | 1-0 -> ZF = 1
jz .mainloop
ret
.draw
call copyMS ;copy background to screen buffer
call drawPlayer ;draw player in next location
call drawStyx ;draw styx
ret
.dosexit:
mov ah, 0
mov al, [gmode]
int 10h
mov word dx, [oldintoff] ; Move VALUE at address oldintoff to DX (not the address!)
mov ax, [oldintseg] ; Move VALUE at address oldintseg to DS, step 1 (remember you cannot move directly to segment registers)
mov ds, ax ; Move VALUE at address oldintseg to DS, step 2
mov ah, 25h ; Move parameters to AH and AL (or both together by moving to AX). Interrupt
mov al, 9 ; number is still 9 and AH should be 25h (note hexadecimal)
INT 21H ; Finally, set the interrupt vector by calling int 21H
mov al, 0 ; 0 to AL
mov ah, 4ch ; 76 to AH
int 21h ;interrupt 21h = close program exit(0)
.end