-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe.asm
2897 lines (2435 loc) · 52.4 KB
/
pe.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
.386
IDEAL
MODEL small
STACK 100h
DATASEG
; -------------------------------------------------------------------------------------
; -------------------------------- DATA MANAGEMENT ------------------------------------
; -------------------------------------------------------------------------------------
; the dots array is sorted in the following order: x position, y position, color
dot_amount dw 0
dots dw 100h dup (?, ?, ?), 321
dots_prev dw 100h dup (?, ?, ?), 321
dots_wall_prev dw 100h dup (?, ?, ?), 321
real dw 100h dup (?, ?, ?), 321
; the sticks array is sorted in the following order: first dot, second dot, color
stick_amount dw 0
sticks dw 100h dup (?, ?, ?), 0
sticks_length dw 100h dup (?, ?, ?), 0
; mode: 0 -> sandbox simulation setup, 1 -> run simulation
mode dw 0
stick_handle dw 0
selected dw 0
saved_color dw 0
left dw 1
left_prev dw 1
right dw 0
right_prev dw 0
fpu dd ?
default_palette db 256*4 dup (0)
; button handling
button_not_pressed dw 0EBh
button_pressed dw 052h
button_nuclear_state dw 0
button_big_state dw 0
button_inverse_state dw 0
button_multi_state dw 0
button_color1_state dw 1
button_color2_state dw 0
button_color3_state dw 0
; button selected center, button selected size
button_nuclear dw 18, 55, 9
button_big dw 18, 85, 9
button_inverse dw 18, 115, 9
button_multi dw 18, 145, 9
button_color1 dw 206, 81, 4
button_color2 dw 206, 108, 4
button_color3 dw 206, 134, 4
; colors
dot_color dw 12
selected_color dw 29
locked_color dw 79
stick_color dw 231
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------
; --------------------------------- FILE SETTINGS -------------------------------------
; -------------------------------------------------------------------------------------
; bmp display
current_file dw 0
file_header db 54 dup (0)
file_palette db 256*4 dup (0)
file_line db 320 dup (0)
; bmp file names
file_title db 'pe_title.bmp', 0
file_how1 db 'pe_how1.bmp', 0
file_how2 db 'pe_how2.bmp', 0
file_settings db 'pe_opt.bmp', 0
file_end db 'pe_end.bmp', 0
; button borders
; x1, y1, x2, y2
title_start dw 112, 64, 206, 97
title_how dw 57, 102, 262, 134
title_exit dw 110, 141, 208, 169
how2_menu dw 85, 173, 233, 196
settings_exit dw 5, 179, 57, 194
settings_start dw 263, 177, 313, 194
settings_nuclear dw 6, 42, 30, 66
settings_big dw 6, 72, 30, 96
settings_inverse dw 6, 102, 30, 126
settings_multi dw 6, 132, 30, 156
settings_color1 dw 199, 73, 213, 87
settings_color2 dw 199, 100, 213, 114
settings_color3 dw 199, 126, 213, 140
end_menu dw 85, 150, 233, 173
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------
; ----------------------------- COSTUMIZABLE SETTINGS ---------------------------------
; -------------------------------------------------------------------------------------
; settings
nuclear dw 0
gravity dw 1
dot_size dw 1
search_sens dw 2
; colors
current_color dw 1
color_1 dw 9, 43, 98, 6
color_2 dw 49, 53, 34, 41
color_3 dw 12, 29, 79, 231
; dot color, selected color, locked color, stick color
; -------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------
CODESEG
; open a bmp
; input: file name in memory
; output: file handle
proc bmp_open
push bp
mov bp, sp
push ax dx
; open file
mov ax, 3D00h
mov dx, [bp+4]
int 21h
; file handle
mov [bp+4], ax
pop dx ax bp
ret
endp bmp_open
; read bmp header
; input: file handle, file header in memory
; output: file handle
proc bmp_header
push bp
mov bp, sp
push ax bx cx dx
; read header
mov ax, 3f00h
mov bx, [bp+6]
mov cx, 54
mov dx, [bp+4]
int 21h
pop dx cx bx ax bp
ret 2
endp bmp_header
; read bmp palette
; input: file handle, file palette in memory
; output: file handle
proc bmp_palette
push bp
mov bp, sp
push ax bx cx dx
; read palette
mov ax, 3f00h
mov bx, [bp+6]
mov cx, 400h
mov dx, [bp+4]
int 21h
pop dx cx bx ax bp
ret 2
endp bmp_palette
; display palette from memory
; input: file palette in memory
; output: none
proc palette
push bp
mov bp, sp
push ax cx dx si
mov si, [bp+4]
mov cx, 256
; copy starting index
mov dx, 3C8h
mov al, 0
out dx, al
; copy palette
inc dx
palette_loop:
; red
mov al, [si+2]
shr al, 2
out dx, al
; green
mov al, [si+1]
shr al, 2
out dx, al
; blue
mov al, [si]
shr al, 2
out dx, al
add si, 4
loop palette_loop
; insert last color
mov dx, 3C8h
mov al, 0fh
out dx, al
inc dx
mov al, 63
out dx, al
out dx, al
out dx, al
; change mouse color
pop si dx cx ax bp
ret 2
endp palette
; read computer's default palette
; input: palette storage in memory
; output: none
proc read_palette
push bp
mov bp, sp
push ax cx dx si
mov si, [bp+4]
mov cx, 256
; copy starting index
mov dx, 3C7h
mov al, 0
out dx, al
; copy palette
inc dx
inc dx
read_palette_loop:
; red
in al, dx
shl al, 2
mov [si+2], al
; green
in al, dx
shl al, 2
mov [si+1], al
; blue
in al, dx
shl al, 2
mov [si], al
add si, 4
loop read_palette_loop
pop si dx cx ax bp
ret 2
endp read_palette
; display bitmap from bmp
; input: file handle, file line in memory
; output: file handle
proc bmp_bitmap
push bp
mov bp, sp
push ax cx dx di si
mov cx, 200
bitmap_loop:
push cx
; di = cx*320
mov di,cx
shl cx, 6
shl di, 8
add di, cx
; read line
mov ax, 3f00h
mov cx, 320
mov bx, [bp+6]
mov dx, [bp+4]
int 21h
; copy line to screen
cld
mov cx, 320
mov si, [bp+4]
rep movsb
pop cx
loop bitmap_loop
pop si di dx cx ax bp
ret 2
endp bmp_bitmap
; close opened bmp file
; input: file handle
; output: none
proc bmp_close
push bp
mov bp, sp
push ax bx
; close file
mov ax, 3e00h
mov bx, [bp+4]
int 21h
pop bx ax bp
ret 2
endp bmp_close
; display a bmp file
; input: file name in memory, file header in memory, file palette in memory, file line in memory
; output: none
proc display_bmp
push bp
mov bp, sp
push [word ptr bp+10]
call bmp_open
push [word ptr bp+8]
call bmp_header
push [word ptr bp+6]
call bmp_palette
push [word ptr bp+6]
call palette
push [word ptr bp+4]
call bmp_bitmap
call bmp_close
pop bp
ret 8
endp display_bmp
; delay
; input: none
; output: none
proc delay
push cx
mov cx, 50
outer_loop:
push cx
mov cx, 0FFFFh
inner_loop:
loop inner_loop
pop cx
loop outer_loop
pop cx
ret
endp delay
; delay
; input: none
; output: none
proc delay_physics
push cx
mov cx, 40
outer_loop_physics:
push cx
mov cx, 0FFFFh
inner_loop_p:
loop inner_loop_p
pop cx
loop outer_loop_physics
pop cx
ret
endp delay_physics
; draw a pixel to the screen
; input: x position, y position, color
; output: none
proc draw_dot
push bp
mov bp, sp
push ax cx dx di
; get screen position - y*320+x
mov ax, [bp+6]
mov cx, 320
xor dx, dx
mul cx
add ax, [bp+8]
mov di, ax
; get color and display color
mov ax, [bp+4]
cmp [byte ptr es:di], al
je draw_dot_skip
mov [es:di], al
draw_dot_skip:
pop di dx cx ax bp
ret 6
endp draw_dot
; access an array in memory by index
; input: array start location, element in array
; output: beginning of element data in array
proc array_access
push bp
mov bp, sp
push ax cx
; get starting position of element information in array
mov ax, [bp+4]
dec ax
mov cx, 6
mul cl
add ax, [bp+6]
mov [bp+6], ax
pop cx ax bp
ret 2
endp array_access
; reset unselected buttons' color in settings screen
; input: first button state in memory, first button data in memory, second button state in memory, second button data in memory, button no pressed color in memory
; output: none
proc button_color_reset
push bp
mov bp, sp
push bx
mov bx, [bp+12]
mov [word ptr bx], 0
mov bx, [bp+10]
push bx
mov bx, [bp+4]
push [word ptr bx]
call display_square_button
mov bx, [bp+8]
mov [word ptr bx], 0
mov bx, [bp+6]
push bx
mov bx, [bp+4]
push [word ptr bx]
call display_square_button
pop bx bp
ret 10
endp button_color_reset
; select and deselect a button in settings screen
; input: button data in memory, color
; output: none
proc display_square_button
push bp
mov bp, sp
push ax bx cx dx si
; hide mouse
mov ax, 2
int 33h
; get data
mov bx, [bp+6]
; find top-left corner
mov ax, [bx]
mov dx, [bx+2]
sub ax, [bx+4]
sub dx, [bx+4]
mov si, [bp+4]
mov cx, [bx+4]
shl cx, 1
inc cx
button_column:
push ax cx
mov cx, [bx+4]
shl cx, 1
inc cx
button_row:
push ax dx si
call draw_dot
inc ax
loop button_row
pop cx ax
inc dx
loop button_column
; show mouse
mov ax, 1
int 33h
pop si dx cx bx ax bp
ret 4
endp display_square_button
; display a square
; input: blackout, dots start, dot position in array, size of square
; output: none
proc display_square
push bp
mov bp, sp
push ax bx cx dx di
push [word ptr bp+8] [word ptr bp+6]
call array_access
pop bx
; check if dot isnt too close to border
mov ax, [bx]
cmp ax, [bp+4]
jl cant_display
mov cx, 320
sub cx, [bp+4]
cmp ax, cx
jg cant_display
mov dx, [bx+2]
cmp dx, [bp+4]
mov cx, 320
sub cx, [bp+4]
cmp dx, cx
jg cant_display
; find top-left corner
sub ax, [bp+4]
sub dx, [bp+4]
mov cx, [bp+10]
cmp cx, 0
je square_skip_color_read
mov cx, [bx+4]
square_skip_color_read:
mov bx, cx
mov cx, [bp+4]
shl cx, 1
inc cx
column:
push ax cx
mov cx, [bp+4]
shl cx, 1
inc cx
row:
push ax dx bx
call draw_dot
inc ax
loop row
pop cx ax
inc dx
loop column
cant_display:
pop di dx cx bx ax bp
ret 8
endp display_square
; the naive line drawing algorithm
; input: dx, dy, y or x mode, x1, y1, x2, y2, color
; outupt: none
proc naive_algo
push bp
mov bp, sp
push ax bx cx dx di si
; get xy mode
mov bx, [bp+14]
; dx = x1 − x2
mov di, [bp+18]
; dy = y1 − y2
mov si, [bp+16]
; for x from x1 to x2 do
mov cx, [bp+12]
dec cx
naive_algo_loop:
inc cx
; y = yLow + dy × (x − xLow) / dx
mov ax, cx
sub ax, [bp+12]
xor dx, dx
mul si
div di
; compare y's
mov dx, [bp+10]
cmp dx, [bp+6]
jb naive_y_normal
mov dx, [bp+10]
sub dx, ax
mov ax, dx
jmp after_naive_y
naive_y_normal:
add ax, [bp+10]
after_naive_y:
; plot(x, y)
cmp bx, 0
jne yx
push cx
push ax
jmp after_mode
yx:
push ax
push cx
after_mode:
push [word ptr bp+4]
call draw_dot
cmp cx, [bp+8]
jne naive_algo_loop
pop si di dx cx bx ax bp
ret 16
endp naive_algo
; set up the inputs for the naive algorithm
; input: x1, y1, x2, y2, color
; output: none
proc naive_algo_setup
push bp
mov bp, sp
push ax di si
mov ax, [bp+12]
cmp ax, [bp+8]
jne not_equal
mov ax, [bp+10]
cmp ax, [bp+6]
jne not_equal
jmp naive_algo_setup_end
not_equal:
; dx
mov ax, [bp+12]
cmp ax, [bp+8]
jb dx_else
mov di, [bp+12]
sub di, [bp+8]
jmp dx_after
dx_else:
mov di, [bp+8]
sub di, [bp+12]
dx_after:
; dy
mov ax, [bp+10]
cmp ax, [bp+6]
jb dy_else
mov si, [bp+10]
sub si, [bp+6]
jmp dy_after
dy_else:
mov si, [bp+6]
sub si, [bp+10]
dy_after:
; ------------
; 12 - Ax
; 10 - Ay
; 8 - Bx
; 6 - By
; di - Dx
; si - Dy
; 4 - color
; ------------
; if di > si:
; if 12 > 8:
; 8, 6, 12, 10
; else:
; 12, 10, 8, 6
; else:
; if 10 > 6
; 6, 8, 10, 12
; else:
; 10, 12, 6, 8
cmp di, si
jb main_else
push di si
mov ax, 0
push ax
mov ax, [bp+12]
cmp ax, [bp+8]
jb main_x_else
push [word ptr bp+8] [word ptr bp+6] [word ptr bp+12] [word ptr bp+10] [word ptr bp+4]
call naive_algo
jmp naive_algo_setup_end
main_x_else:
push [word ptr bp+12] [word ptr bp+10] [word ptr bp+8] [word ptr bp+6] [word ptr bp+4]
call naive_algo
jmp naive_algo_setup_end
main_else:
push si di
mov ax, 1
push ax
mov ax, [bp+10]
cmp ax, [bp+6]
jb main_y_else
push [word ptr bp+6] [word ptr bp+8] [word ptr bp+10] [word ptr bp+12] [word ptr bp+4]
call naive_algo
jmp naive_algo_setup_end
main_y_else:
push [word ptr bp+10] [word ptr bp+12] [word ptr bp+6] [word ptr bp+8] [word ptr bp+4]
call naive_algo
naive_algo_setup_end:
pop si di ax bp
ret 10
endp naive_algo_setup
; display a line
; input: blackout, sticks start, stick position in array, dots start
; output: none
proc display_stick
push bp
mov bp, sp
push ax bx cx dx di si
push [word ptr bp+8]
push [word ptr bp+6]
call array_access
pop bx
; get first dot's x and y positions
push bx
mov bx, [bx]
push [word ptr bp+4] bx
call array_access
pop bx
mov cx, [bx]
mov dx, [bx+2]
pop bx
; get second dot's x and y positions
add bx, 2
push bx
mov bx, [bx]
push [word ptr bp+4] bx
call array_access
pop bx
mov di, [bx]
mov si, [bx+2]
pop bx
; get stick color
mov ax, [bp+10]
cmp ax, 0
je stick_skip_color_read
add bx, 2
mov ax, [bx]
stick_skip_color_read:
; ------------
; cx - Ax
; dx - Ay
; di - Bx
; si - By
; ax - color
; ------------
push cx dx di si ax
call naive_algo_setup
pop si di dx cx bx ax bp
ret 8
endp display_stick
; append element to the end of an array
; input: new element's first property, new element's second property, new element's color, array start, array amount location in memory
; output: output
proc array_add
push bp
mov bp, sp
push ax bx
mov bx, [bp+4]
mov ax, [bx]
inc ax
mov [bx], ax
push [word ptr bp+6] ax
call array_access
pop bx
mov ax, [bp+12]
mov [bx], ax
mov ax, [bp+10]
mov [bx+2], ax
mov ax, [bp+8]
mov [bx+4], ax
pop bx ax bp
ret 10
endp array_add
; check if mouse is over a dot
; input: search sensitivity, dot size, dot array start, x lookup, y lookup
; output: start of element in memory; 0 if no element is selected
proc search_dots_by_position
push bp
mov bp, sp
push ax bx cx dx di si
mov si, [bp+10]
add si, [bp+12]
mov bx, [bp+8]
sub bx, 6
search_loop:
add bx, 6
mov cx, [bp+6]
sub cx, si
search_x_loop:
inc cx
cmp cx, [bx]
jne search_x_loop_end
push cx
mov cx, [bp+4]
sub cx, si
search_y_loop:
inc cx
cmp cx, [bx+2]
jne search_y_loop_end
mov ax, bx
sub ax, [bp+8]
add ax, 6
xor dx, dx
mov di, 6
div di
mov dx, ax
pop cx
jmp end_search
search_y_loop_end:
mov dx, [bp+4]
add dx, si
cmp cx, dx
jbe search_y_loop
pop cx
search_x_loop_end:
mov dx, [bp+6]
add dx, si
cmp cx, dx
jbe search_x_loop
mov ax, 321
cmp [bx], ax
jne search_loop
mov dx, 0
end_search:
mov [bp+12], dx
pop si di dx cx bx ax bp
ret 8
endp search_dots_by_position
; clear the screen
; input: none
; output: none
proc clear
push ax cx di
mov ax, 0
mov cx, 320*200
clear_loop:
mov di, cx
cmp [byte ptr es:di], al
je clear_loop_skip
mov [es:di], al
clear_loop_skip:
loop clear_loop
pop di cx ax
ret
endp clear
; input: dots in memory, prev dots in memory, dot number in array
; output: cahnge or no change
proc check_change_dot
push bp
mov bp, sp
push ax bx cx dx si di
mov cx, 0
push [word ptr bp+8] [word ptr bp+4]
call array_access
pop si
push [word ptr bp+6] [word ptr bp+4]
call array_access
pop di
mov ax, [si]
mov dx, [di]
cmp ax, dx
jne check_dot_changed
mov ax, [si+2]
mov dx, [di+2]
cmp ax, dx
jne check_dot_changed
mov ax, [si+4]
mov dx, [di+4]
cmp ax, dx
jne check_dot_changed
jmp check_dots_not_changed
check_dot_changed:
mov cx, 1
check_dots_not_changed:
mov [bp+8], cx
pop di si dx cx bx ax bp
ret 4
endp check_change_dot
; input: dots in memory, prev dots in memory, sticks start in memory, dot number in array
; output: cahnge or no change
proc check_change_stick
push bp
mov bp, sp
push ax bx cx dx si di
mov cx, 0
push [word ptr bp+6] [word ptr bp+4]
call array_access
pop bx
push [word ptr bp+10] [word ptr bx]
call array_access
pop si
push [word ptr bp+8] [word ptr bx]
call array_access
pop di
mov ax, [si]
mov dx, [di]
cmp ax, dx
jne check_stick_changed
mov ax, [si+2]
mov dx, [di+2]
cmp ax, dx
jne check_stick_changed
mov ax, [si+4]
mov dx, [di+4]