-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.asm
1347 lines (1147 loc) · 46.5 KB
/
main.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
INCLUDE Irvine32.inc
.DATA
;================================================================================================================================================================================================================================================
fileName BYTE "lines.txt", 0 ;doesn't have to be a path if it's in the project's folder
maxSize equ 400 ;won't be changed
actualSize DWORD ? ;actual number of bytes/coordinates (X's + Y's), will be determined during run-time (the converting PROC)
numOfPoints BYTE ? ;actualSize / 2
numOfLines BYTE ? ;numOfPoints / 2
inputLines BYTE maxSize DUP(?) ; input buffer containing the characters in the file
pointsList BYTE maxSize DUP(?) ; contains the converted integers
xList BYTE maxSize DUP(?) ; x coordinates {startL1, endL1, startL2, endL2, startL3, ...}
yList BYTE maxSize DUP(?) ; y coordinates {startL1, endL1, startL2, endL2, startL3, ...}
numOfBytesTriangles DWORD ? ;used as an index with the offset of pointsTrianglesList
;to know where to insert the next element
numOfPointsTriangles DWORD ? ;actual number of points
numOfTriangles DWORD ?
pointsTrianglesList DWORD maxSize DUP(?) ;indices of the points of the detected triangles in xList/yList
xRectanglesList BYTE 45 DUP(?) ; Carries the x-coordinates of the found Rectangles {x1_1, x1_2, x1_3, x1_4, x2_1, x2_2, x2_3, x2_4, ...}
yRectanglesList BYTE 45 DUP(?) ; Carries the y-coordinates of the found Rectangles {y1_1, y1_2, y1_3, y1_4, y2_1, y2_2, y2_3, y2_4, ...}
rectanglesNumber DWORD 0 ; Carries the number of the found Rectangles
xSquaresList BYTE 45 DUP(?) ; Carries the x-coordinates of the found Squares {x1_1, x1_2, x1_3, x1_4, x2_1, x2_2, x2_3, x2_4, ...}
ySquaresList BYTE 45 DUP(?) ; Carries the y-coordinates of the found Squares {y1_1, y1_2, y1_3, y1_4, y2_1, y2_2, y2_3, y2_4, ...}
squaresNumber DWORD 0 ; Carries the number of the found Squares
intersectionsNumber DWORD 0 ; The Number of found intersections
xIntersections BYTE 400 DUP(?) ; Carries the x-coordinates of the found Intersections {x1, x2, x3, x4,....}
yIntersections BYTE 400 DUP(?) ; Carries the y-coordinates of the found Intersections {x1, x2, x3, x4,....}
isDistinctIntersection BYTE 0 ; To validate wether the found intersection was found before or not
validRec BYTE 0 ; To validate that there actually is lines connecting the found 4 poinst and forming a rectangle/square
xFirstPointOffset DWORD ? ;Used to store the offset of the x-coordinate first point in the nested loops used in the search for the 4 points of the rectangles/squares and in the nested loop used to find intersections
yFirstPointOffset DWORD ? ;Used to store the offset of the y-coordinate first point in the nested loops used in the search for the 4 points of the rectangles/squares
xSecondPointOffset DWORD ? ;Used to store the offset of the x-coordinate second point in the nested loops used in the search for the 4 points of the rectangles/squares
ySecondPointOffset DWORD ? ;Used to store the offset of the y-coordinate second point in the nested loops used in the search for the 4 points of the rectangles/squares
xThirdPointOffset DWORD ? ;Used to store the offset of the x-coordinate third point in the nested loops used in the search for the 4 points of the rectangles/squares
yThirdPointOffset DWORD ? ;Used to store the offset of the y-coordinate third point in the nested loops used in the search for the 4 points of the rectangles/squares
xFourthPointOffset DWORD ? ;Used to store the offset of the x-coordinate fourth point in the nested loops used in the search for the 4 points of the rectangles/squares
yFourthPointOffset DWORD ? ;Used to store the offset of the y-coordinate fourth point in the nested loops used in the search for the 4 points of the rectangles/squares
xStCurPoint BYTE ? ; The x-coordinate of the start of the current line used in the search for intersections
xEnCurPoint BYTE ? ; The x-coordinate of the end of the current line used in the search for intersections
yStCurPoint BYTE ? ; The y-coordinate of the start of the current line used in the search for intersections
yEnCurPoint BYTE ? ; The y-coordinate of the end of the current line used in the search for intersections
xCurOffset1 DWORD ? ; Used to save the cur offset of the xList in the nested loops used for the search for th intersections
yCurOffset1 DWORD ? ; Used to save the cur offset of the yList in the nested loops used for the search for th intersections
xSquaresOffset DWORD ? ; Used to store the offset of the last cell in the xSquaresList to store the new values of the new square in
ySquaresOffset DWORD ? ; Used to store the offset of the last cell in the ySquaresList to store the new values of the new square in
xRectanglesOffset DWORD ? ; Used to store the offset of the last cell in the xRectanglesList to store the new values of the new rectangle in
yRectanglesOffset DWORD ? ; Used to store the offset of the last cell in the yRectanglesList to store the new values of the new rectangle in
tmp1stPointECX DWORD ? ;
tmp2ndPointECX DWORD ? ;
tmp3rdPointECX DWORD ? ; ALL 4 are used as a temp containers for the ECX register values in the nested 4 loops used in the search for rectangles/squares
tmp4thPointECX DWORD ? ;
tmpValidatePointECX DWORD ? ; Used to store the values of the ECX register in the validation of the found intersection point
tmpValidateRecECX DWORD ? ; Used to store the values of the ECX register in the validation of the found rectangle/square
x BYTE ? ; Used as the x-coordinate of the found intersection point in the validation of it
y BYTE ? ; Used as the y-coordinate of the found intersection point in the validation of it
x5 BYTE ? ; Used as the x-coordinate in the validation of the found rectangle/square
y5 BYTE ? ; Used as the y-coordinate in the validation of the found rectangle/square
x1 BYTE ? ; Used as the x-coordinate of the found First point in the validation of the rectangle points
y1 BYTE ? ; Used as the y-coordinate of the found First point in the validation of the rectangle points
x2 BYTE ? ; Used as the x-coordinate of the found Second point in the validation of the rectangle points
y2 BYTE ? ; Used as the y-coordinate of the found Second point in the validation of the rectangle points
x3 BYTE ? ; Used as the x-coordinate of the found Third point in the validation of the rectangle points
y3 BYTE ? ; Used as the y-coordinate of the found Third point in the validation of the rectangle points
x4 BYTE ? ; Used as the x-coordinate of the found Fourth point in the validation of the rectangle points
y4 BYTE ? ; Used as the y-coordinate of the found Fourth point in the validation of the rectangle points
line BYTE ? ; Used to indicate the line that we're check whether it exists in the input or not
;================================================================================================================================================================================================================================================
.CODE
;================================================================================================================================================================================================================================================
main PROC
CALL ReadLines
mov ESI, OFFSET inputLines ; list of characters
mov EDI, OFFSET pointsList ; list to be filled with numbers
CALL ConvertCharsToNumbers
;call writeDec
mov actualSize, EAX ; EAX has the number of converted integers
mov EDX, OFFSET pointsList
mov ECX, actualSize
mov ESI, OFFSET xList
mov EDI, OFFSET yList
;mov xNumberElement, 0
;mov yNumberElement, 0
CALL DivideIntoXY
mov EAX, actualSize
CALL DivideByTwo ;getting # points = # of bytes (actualSize) / 2
mov numOfPoints, AL ;EAX contains actualSize / 2
CALL DivideByTwo ;getting # of lines = # of points (EAX) / 2
mov numOfLines, AL ;EAX contains numOfPoints / 2
;movzx ECX, numOfPoints
;mov EDX, offset xList
;coutXs:
;movzx EAX, BYTE ptr [EDX]
;CALL WriteInt
;CALL CRLF
;inc EDX
;LOOP coutXs
;triangles + example of the output (to understand what my PROCs do)
CALL FindTriangles
mov EAX, numOfTriangles
CALL WriteDec ;number of triangles
CALL CRLF
CALL CRLF
cmp numOfTriangles, 0
JE noTrianglesFound
mov ECX, numOfPointsTriangles
mov EDX, offset pointsTrianglesList
mov ESI, 0 ;points counter
outputTriangles:
mov AL, '('
CALL WriteChar
mov EBX, [EDX] ;index of the point in xList and yList
mov EDI, offset xList
add EDI, EBX
movzx EAX, BYTE ptr [EDI]
CALL WriteInt ;x coordinate of the point
mov AL, ','
CALL WriteChar
mov AL, ' '
CALL WriteChar
mov EDI, offset yList
add EDI, EBX
movzx EAX, BYTE ptr [EDI] ;y coordinate of the point
CALL WriteInt
mov AL, ')'
CALL WriteChar
CALL CRLF
add EDX, type pointsTrianglesList
inc ESI
cmp ESI, 3
JNE triangleNotDone
mov ESI, 0
CALL CRLF
triangleNotDone:
loop outputTriangles
noTrianglesFound:
CALL findRectanglesAndSquares
exit
main ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ReadLines PROC
mov EDX, offset fileName
CALL OpenFile ;returns fileHandle in EAX
mov ECX, maxSize
mov EDX, offset inputLines ;the buffer
CALL ReadFromFile
;CALL WriteInt ;view number of bytes read (EAX)
;CALL CRLF
;mov EDX, offset inputLines
;mov ECX, EAX ;EAX = number of bytes read
;LoopWriteLines:
;mov AL, [EDX]
;CALL WriteChar
;;CALL CRLF
;add EDX, type inputLines
;loop LoopWriteLines
ret
ReadLines ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; Opens a new text file and opens for input.
; Receives: EDX points to the filename.
; Returns: If the file was opened successfully, EAX
; contains a valid file handle. Otherwise, EAX equals
; INVALID_HANDLE_VALUE.
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
OpenFile PROC
INVOKE CreateFile,
edx, GENERIC_READ, DO_NOT_SHARE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
ret
OpenFile ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; Converts a list of characters representing integer into their integer values.
; Receives: ESI points to the array of characters, EDI points to
; the array of numbers to fill.
; Returns: EAX containing the total number of the converted numbers.
; Requirements: All the lists must be lists of BYTES.
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ConvertCharsToNumbers PROC USES EBX ECX EDX ESI EDI
mov ECX, 0 ; temporary counter of the integer elements
mov BL, 0 ; temporary value to update with the integer number
mov EDX, 0 ; counter spaces
while_convertingChars:
mov AL, [ESI]
cmp AL, '*'
JE endwhile_convertingChars ; EOF character
call IsDigit
JZ if_updateESIvalue ; contains a valid number
cmp EDX, 0 ; first space after a number
JNE endif_updateESIvalue
; AL has the first space or enter, then move the previous integer value in the list
mov [EDI], BL
mov BL, 0 ; reset the temporary value
inc ECX ; count an integer
inc EDI ; go to the next index
inc EDX ; count a space
JMP endif_updateESIvalue
; BL = (BL * 10) + AL = (result * 10) + digit
if_updateESIvalue:
sub AL, '0' ; convert the ASCII character to its digit value
mov BH, AL ; save the digit
mov AL, BL ; needed for MUL
mov BL, 10 ; needed for MUL
mul BL ; (BL * 10) = (result * 10)
add AL, BH ; (BL * 10) + BH --having value of AL--
mov BL, AL ; update the original container
mov EDX, 0 ; always update the spaces counter to zero
endif_updateESIvalue:
inc ESI ; go to the next character
JMP while_convertingChars
endwhile_convertingChars:
mov EAX, ECX
ret
ConvertCharsToNumbers ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; Divides a list of points stored as {x, y,x , y, ...} into 2 lists
; Receives: EDX points to array of points, ESI points to
; array of x-coordinates, EDI points to array of y-coordinates
; ECX containing number of elements in the array of points
; Returns: EAX containing the total number of the converted numbers.
; Requirements: All the lists must be lists of BYTES.
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DivideIntoXY PROC USES EAX EDX ECX ESI EDI
loop_xyDivision:
; Line_i Start_x
mov AL, [EDX]
mov [ESI], AL
inc ESI
inc EDX
; Line_i Start_y
mov AL, [EDX]
mov [EDI], AL
inc EDI
inc EDX
; Line_i End_x
mov AL, [EDX]
mov [ESI], AL
inc ESI
inc EDX
; Line_i End_y
mov AL, [EDX]
mov [EDI], AL
inc EDI
inc EDX
;add xNumberElement, 2
;add yNumberElement, 2
sub ECX, 3
LOOP loop_xyDivision
ret
DivideIntoXY ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;Divides a number by two
;Receives: EAX: the number to divide (N)
;Returns: EAX: N/2
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DivideByTwo PROC USES EBX EDX
mov EDX, 0
mov EBX, 2
div EBX
ret
DivideByTwo ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FindTriangles PROC
mov numOfTriangles, 0
mov numOfBytesTriangles, 0
mov numOfPointsTriangles, 0
movzx ECX, numOfLines ;iterating over all lines,
sub ECX, 2 ;except last 2 lines (because we need 3 lines to form a trianlge)
;1st iteration: EAX (index of line 1) = 0
;2nd iteration: EAX = 2 (because each line us represented with start and end, so 2 bytes)
;so for each iteration: EAX += 2
mov EAX, 0 ;index of line1 (1st iteration of 1st loop, gets incremented by 2 after each iteration)
loop_firstLineTri:
push ECX
mov EBX, EAX
add EBX, 2 ;index of line2 = index of line1 + 2
loop_secondLineTri:
;check start1 and start2
;check x's
mov EDI, offset xList
add EDI, EAX ;EDI = beginning + index of line1 (EAX)
mov ESI, offset xList
add ESI, EBX ;ESI = beginning + index of line2 (EBX)
cmpsb ;check if both lines have the same start
JNE checkEnds_FindTri
;same x, check y's
mov EDI, offset yList
add EDI, EAX ;y coordinate of the start of line1, EAX = index of line1
mov ESI, offset yList
add ESI, EBX ;y coordiante of the start of line2, EBX = index of line2
cmpsb
JNE checkEnds_FindTri
;the 2 lines have the same start point, so we need to find a line that has the other two end points
;for the FindThirdLine PROC:
;EDX = index of line to start checking from
mov EDX, EBX
add EDX, 2
;EAX = index of pt1 (end pt. of line1)
push EAX ;start pt. of line1
inc EAX
;EBX = index of pt2 (end pt. of line2)
push EBX ;start pt. of line2
inc EBX ;index of end of pt2
CALL FindThirdLine_FindTri
pop EBX
pop EAX
;result is returned in EDX
cmp EDX, -1
JE nextLine_FindTri
mov EDI, offset pointsTrianglesList
add EDI, numOfBytesTriangles
mov [EDI], EAX ;start of line1 (intersection pt.)
add EDI, type pointsTrianglesList
mov [EDI], EAX
add DWORD ptr [EDI], 1 ;end of line1 (2nd pt.)
add EDI, type pointsTrianglesList
mov [EDI], EBX
add DWORD ptr [EDI], 1 ;end of line2 (3rd pt.)
add numOfBytesTriangles, type pointsTrianglesList * 3
add numOfPointsTriangles, 3
inc numOfTriangles
JMP nextLine_FindTri
checkEnds_FindTri:
mov EDI, offset xList
add EDI, EAX ;x coordinate of the start point of line1
inc EDI ;x coordinate of end point of line1
mov ESI, offset xList
add ESI, EBX ;x coordinate of the start point of line2
inc ESI ;x coordinate of the end point of line2
cmpsb
JNE checkCrossStartEnd_FindTri
mov EDI, offset yList
add EDI, EAX
inc EDI ;y coordinate of the end pt. of line1
mov ESI, offset yList
add ESI, EBX
inc ESI ;y coordinate of the end pt. of line2
cmpsb
JNE checkCrossStartEnd_FindTri
;EAX = line1 index
;EBX = line2 index
;EDX = EBX + 2 (line3 index, start searching from there)
push EAX
push EBX
mov EDX, EBX
add EDX, 2
CALL FindThirdLine_FindTri
pop EBX
pop EAX
cmp EDX, -1
JE nextLine_FindTri
mov EDI, offset pointsTrianglesList
add EDI, numOfBytesTriangles
mov [EDI], EAX ;start of line1 (1st pt.)
add EDI, type pointsTrianglesList
mov [EDI], EAX
add DWORD ptr [EDI], 1 ;end of line1 (intersection pt.)
add EDI, type pointsTrianglesList
mov [EDI], EBX ;start of line2 (3rd pt.)
add numOfBytesTriangles, type pointsTrianglesList * 3
add numOfPointsTriangles, 3
inc numOfTriangles
JMP nextLine_FindTri
checkCrossStartEnd_FindTri: ;check if start of line1 = end of line2
mov EDI, offset xList
add EDI, EAX ;x coordinate of the start point of line1
mov ESI, offset xList
add ESI, EBX
inc ESI ;x coordinate of the end point of line2
cmpsb
JNE checkCrossEndStart_FindTri
;same x's, check y's
mov EDI, offset yList
add EDI, EAX ;y coordinate of the start point of line1
mov ESI, offset yList
add ESI, EBX
inc ESI ;y coordinate of the end point of line2
cmpsb
JNE checkCrossEndStart_FindTri
;same x, same y (start of line1 = end of line2)
;for FindThirdLine PROC:
;EAX = end of line1
;EBX = start of line2
;EDX = start of line to start checking from (start of line2 + 2)
push EAX
push EBX
inc EAX
mov EDX, EBX
add EDX, 2
CALL FindThirdLine_FindTri
pop EBX
pop EAX
cmp EDX, -1
JE nextLine_FindTri
mov EDI, offset pointsTrianglesList
add EDI, numOfBytesTriangles
mov [EDI], EAX ;start of line1 (intersection pt.)
add EDI, type pointsTrianglesList
mov [EDI], EAX
add DWORD ptr [EDI], 1 ;end pt. of line1 (2nd pt.)
add EDI, type pointsTrianglesList
mov [EDI], EBX ;start of line2 (3rd pt.)
add numOfBytesTriangles, type pointsTrianglesList * 3
add numOfPointsTriangles, 3
inc numOfTriangles
JMP nextLine_FindTri
checkCrossEndStart_FindTri:
mov EDI, offset xList
add EDI, EAX ;x coordinate of the start point of line1
inc EDI ;x coordinate of the end point of line1
mov ESI, offset xList
add ESI, EBX ;x coordinate of the start point of line2
cmpsb
JNE nextLine_FindTri
mov EDI, offset yList
add EDI, EAX ;y coordinate of the start point of line1
inc EDI ;y coordinate of the end point of line1
mov ESI, offset yList
add ESI, EBX ;y coordinate of the start point of line2
cmpsb
JNE nextLine_FindTri
;end of line1 = start of line2
;for FindThirdLine PROC:
;EAX = start of line1
;EBX = end of line2
;EDX = start of line to start checking from (starts of line2 + 2)
push EAX
push EBX
mov EDX, EBX
add EDX, 2
inc EBX
CALL FindThirdLine_FindTri
pop EBX
pop EAX
cmp EDX, -1
JE nextLine_FindTri
mov EDI, offset pointsTrianglesList
add EDI, numOfBytesTriangles
mov [EDI], EBX ;start of line2 (intersection pt.)
add EDI, type pointsTrianglesList
mov [EDI], EAX ;start of line1 (2nd pt.)
add EDI, type pointsTrianglesList
mov [EDI], EBX
add DWORD ptr [EDI], 1 ;end of line2 (3rd pt.)
add numOfBytesTriangles, type pointsTrianglesList * 3
add numOfPointsTriangles, 3
inc numOfTriangles
nextLine_FindTri:
add EBX, 2 ;x coordinate of the start of the next line in 2nd loop
;LOOP loop_secondLineTri: JUMP TOO FAR, HAS TO BE WRITTEN EXPLICITLY
dec ECX
JNZ loop_secondLineTri
add EAX, 2 ;next line in 1st loop
pop ECX ;retrieve the original counter of 1st loop from stack
;LOOP loop_firstLineTri: JUMP TOO FAR, HAS TO BE WRITTEN EXPLICITLY
dec ECX
JNZ loop_firstLineTri
done_FindTri:
ret
FindTriangles ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;After finding two lines intersecting at one point,
;this function searches for the third line that completes the triangle, if it exists,
;i.e. a line whose 2 points are equal to the 2 points in the parameters, order doesn't matter.
;Receives: EAX contains the index of the 1st point
; EBX contains the index of the 2nd point
; EDX contains the index of the line to start checking from
;Returns: EDX contains the index of the third line if found, else it contains -1
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FindThirdLine_FindTri PROC USES ECX
push EAX ;EAX is an input parameter, we need to keep its value
mov EAX, EDX ;we need to divide the index of the line by 2
CALL DivideByTwo ;EAX = EDX/2 = index/2
movzx ECX, numOfLines
sub ECX, EAX ;loopCounter = numOfLines - # of line to start checking from
;# of the line = index of the line divided by two,
;if index = 8, then it's the 4th line (because each line takes up 2 indices (start and end)
pop EAX
loop_FindThirdLine:
;check start of current line with 1st pt (EDI)
mov EDI, offset xList
add EDI, EAX ;x coordinate of 1st pt.
mov ESI, offset xList
add ESI, EDX ;x coordinate of current line
cmpsb
JNE checkCurrEndWithFirstPt_FindThirdLine
;same x, check y's
mov EDI, offset yList
add EDI, EAX ;y coordinate of 1st pt.
mov ESI, offset yList
add ESI, EDX ;y coordinate of current line
cmpsb
JNE checkCurrEndWithFirstPt_FindThirdLine
;same x, same y, check end pt. of current line with 2nd pt.
JMP checkCurrEndWithSecPt_FindThirdLine
checkCurrEndWithFirstPt_FindThirdLine:
;check x's
mov EDI, offset xList
add EDI, EAX ;x of 1st pt.
mov ESI, offset xList
add ESI, EDX ;x of curr start
inc ESI ;x of curr end
cmpsb
JNE nextLine_FindThirdLine
;same x, check y's
mov EDI, offset yList
add EDI, EAX ;y of 1st pt.
mov ESI, offset yList
add ESI, EDX ;y of curr start
inc ESI ;y of curr end
cmpsb
JNE nextLine_FindThirdLine
;current end = 1st point, then we need to check if current start = 2nd point
JMP checkCurrStartWithSecPt_FindThirdLine
checkCurrEndWithSecPt_FindThirdLine:
mov ESI, offset xList
add ESI, EBX ;x of 2nd pt.
mov EDI, offset xList
add EDI, EDX ;x of curr start
inc EDI ;x of curr end
cmpsb
JNE nextLine_FindThirdLine
;same x, check y's
mov ESI, offset yList
add ESI, EBX ;y of 2nd pt.
mov EDI, offset yList
add EDI, EDX ;y of curr start
inc EDI ;y of curr end
cmpsb
JNE nextLine_FindThirdLine
;FOUND THE LINE (curr start = 1st pt, curr end = 2nd pt)
JMP foundThirdLine_FindThirdLine
checkCurrStartWithSecPt_FindThirdLine:
;check x's
mov ESI, offset xList
add ESI, EBX ;x of 2nd pt.
mov EDI, offset xList
add EDI, EDX ;x of curr start
cmpsb
JNE nextLine_FindThirdLine
;same x, check y's
mov ESI, offset yList
add ESI, EBX ;y of 2nd pt.
mov EDI, offset yList
add EDI, EDX ;y of curr start
cmpsb
JNE nextLine_FindThirdLine
;FOUND THE LINE (curr start = 2nd pt, curr end = 1st pt)
JMP foundThirdLine_FindThirdLine
nextLine_FindThirdLine:
add EDX, 2
;LOOP loop_FindThirdLine: JUMP TOO FAR, HAS TO BE WRITTEN EXPLICITLY
dec ECX
JNZ loop_FindThirdLine
mov EDX, -1 ;third line not found
foundThirdLine_FindThirdLine:
;return, EDX contains the index of the found third line (hopefully :'D)
ret
FindThirdLine_FindTri ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; findRectanglesAndSquares PROC only calls for the other 2 functions:
; 1- findIntersections which calculates all the intersections
; 2- findRectangles which finds all the rectangles and squares found and returns :
; 1) The Number of Rectangles found in rectanglesNumber
; 2) The Number of Squares found in squaresNumber
; 3) A list with the x-coordinates of the 4 points of each of the found rectangles in the form of
; (if the rectangle is named ABCD clockwise then it's stored in the form of ADCB { A_x1, D_x1 , C_x1, B_x1, A_x2, D_x2 , C_x2, B_x2, ....}
; 4) A list with the x-coordinates of the 4 points of each of the found Squares in the form of
; (if the square is named ABCD clockwise it's stored in the form of ADCB { A_x1, D_x1 , C_x1, B_x1, A_x2, D_x2 , C_x2, B_x2, ....}
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
findRectanglesAndSquares PROC
CALL findIntersections
CALL findRectangles
mov EAX, rectanglesNumber
call writedec
call crlf
mov EAX, squaresNumber
call writedec
call crlf
ret
findRectanglesAndSquares ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------;
; distinctPoint PROC validates whether or not the found intersection point is distinct "not found and stored before" to prevent having any duplicates in the intersections list. ;
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------;
distinctPoint PROC
mov isDistinctIntersection, 0
mov EDX, offset xIntersections
mov EBX, offset yIntersections
mov ECX, intersectionsNumber
cmp ECX, 0
je if_noIntersections
loop_traverse: ; this loops through the already saved intersections and compare the new point with the existing ones.
mov AL, [EDX] ; the x-coordinate of the existing points.
cmp AL, x
je if_equal ; if the current and the new point has the same x-coordinate value.
jmp endif_equal
if_equal:
mov AL, [EBX] ; the y-coordinate of the existing points.
cmp AL, y ; compare there y-coordinates.
je if_equall
jmp endif_equal
if_equall:
mov isDistinctIntersection, 1 ; if they're equal in both of their x and y coordinates then the new point is not distinct dont store it.
endif_equall:
endif_equal:
inc EDX
inc EBX
LOOP loop_traverse
if_noIntersections:
RET
distinctPoint ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; findIntersections PROC traverses through the xList an yList of the given lines to find the intersections and stores them.
; It returns the number of the found intersections in intersectionsNumber and its values in 2 lists:
; 1) xIntersections carrying the x-coordinate values of the intersection points {x1, x2, x3, x4, ....}.
; 2) yIntersections carrying the y-coordinate values of the intersection points {y1, y2, y3, y4, ....}.
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
findIntersections PROC
mov EDX, offset xList
mov EBX, offset yList
mov EDI, offset xIntersections
mov ESI, offset yIntersections
movzx ECX, numOfPoints
loop_firstLine: ; selects a line an to find its intersections with the other lines
mov xCurOffset1, EDX ;as we use EDX and EBX in the second loop too
mov yCurOffset1, EBX
mov tmp1stPointECX, ECX
; store the current line's values in the variables to use in the second loop
mov Al, [EDX]
mov xStCurPoint, AL
mov Al, [EDX + 1]
mov xEnCurPoint, AL
mov Al, [EBX]
mov yStCurPoint, AL
mov Al, [EBX + 1]
mov yEnCurPoint, AL
add EDX, 2
add EBX, 2
dec ECX
cmp ECX, 0
je endloop_findIntersectionWCur
loop_findIntersectionWCur: ; second loop finds the lines that intersects with the line from the 1st loop and stores the intersection points
; check if they have the same start x-coordinate.
mov AL, [EDX]
cmp AL, xStCurPoint
je if_sameXSt
chkEn: ; check if they have the same end x-coordinate.
mov AL, [EDX + 1]
cmp AL, xEnCurPoint
je if_sameXEn
chkStEn: ; check if the start x-coordinate of the second line is the same as the end x-coordinate of the first line.
mov AL, [EDX]
cmp AL, xEnCurPoint
je if_sameXStEn
chkEnSt: ; check if the end x-coordinate of the second line is the same as the start x-coordinate of the first line.
mov AL, [EDX + 1]
cmp AL, xStCurPoint
je if_sameXEnSt
jmp continueSearching
if_sameXSt: ; if same start x-coordinate check the y-coordinates for both.
mov AL, [EBX]
cmp AL, yStCurPoint
je if_sameYSt
jmp chkEn ;else check the rest of the options.
endif_sameXSt:
if_sameXEn: ; if same end x-coordinate check the y-coordinates for both.
mov AL, [EBX + 1]
cmp AL, yEnCurPoint
je if_sameYEn
jmp chkStEn ;else check the rest of the options.
endis_sameXEn:
if_sameXStEn: ; if the start x-coordinate of the second line is the same as the end x-coordinate of the first line.
mov AL, [EBX]
cmp AL, yEnCurPoint
je if_sameYSt
jmp chkEnSt ;else check the rest of the options.
endif_sameXStEn:
if_sameXEnSt: ;if the end x-coordinate of the second line is the same as the start x-coordinate of the first line.
mov AL, [EBX + 1]
cmp AL, yStCurPoint
je if_sameYEn
jmp continueSearching ;else continue searching through the rest of the lines.
endif_sameXEnSt:
if_sameYSt: ;if they have the same start y-coordinates store the offsets
mov xFirstPointOffset, EDX
mov yFirstPointOffset, EBX
mov tmpValidatePointECX, ECX
;store the values of the point in the x and y variables
mov AL, [EDX]
mov x, AL
mov AL, [EBX]
mov y, AL
; check if the point is distinct
CALL distinctPoint
;restore the original offsets
mov ECX, tmpValidatePointECX
mov EDX, xFirstPointOffset
mov EBX, yFirstPointOffset
mov Al, isDistinctIntersection
cmp AL, 0
je if_validPointSt ; the pont is distinct if the variable equales 0 and not if it equals 1
jmp continueSearching
if_validPointSt: ; if the point is distinct then it's valid and we should store it
mov AL, [EDX]
mov [EDI], AL
mov AL, [EBX]
mov [ESI], AL
; increment EDI and ESI for thenext point found, increment the count of intersections
inc EDI
inc ESI
inc intersectionsNumber
jmp continueSearching
endif_validPointSt:
endif_sameYSt:
if_sameYEn: ; same operations as if_sameYSt with the diffrence that we store the end point's value not the start
mov xFirstPointOffset, EDX
mov yFirstPointOffset, EBX
mov tmpValidatePointECX, ECX
mov AL, [EDX + 1]
mov x, AL
mov AL, [EBX + 1]
mov y, AL
CALL distinctPoint
mov ECX, tmpValidatePointECX
mov EDX, xFirstPointOffset
mov EBX, yFirstPointOffset
mov Al, isDistinctIntersection
cmp AL, 0
je if_validPointEn
jmp continueSearching
if_validPointEn:
mov AL, [EDX + 1]
mov [EDI], AL
mov AL, [EBX + 1]
mov [ESI], AL
inc EDI
inc ESI
inc intersectionsNumber
endif_validPointEn:
endif_sameYEn:
continueSearching: ; dec EDX and EDI and ECX and continue searching for other intersections with the first line.
add EDX, 2
add EBX, 2
dec ECX
cmp ECX, 0
je endloop_findIntersectionWCur
JMP loop_findIntersectionWCur
endloop_findIntersectionWCur:
; restore the first loop's original registers values, dec EDX and EDI and ECX and repeat the operation for the other lines.
mov EDX, xCurOffset1
mov EBX, yCurOffset1
add EDX, 2
add EBX, 2
mov ECX, tmp1stPointECX
dec ECX
cmp ECX, 0
jbe endloop_firstLine
JMP loop_firstLine
endloop_firstLine:
ret
findIntersections ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; checkSquare PROC checks if the found 4 points form a rectangle or a square and stores them in there respective Lists.
; the 4 points are in the anti-clockwise order like this A = ( x1 , y1 ) -> D = ( x1 , y2 ) -> C = ( x3 , y2 ) -> B = ( x3 , y1 ).
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
checkSquare PROC
; if abs( y2 - y1 ) == abs( x3 - x1 ) then the four points form a square
; abs ( y2 - y1 )
mov AL, y2
cmp AL, y1
ja if_neg1 ; if y2 > y1
mov AL, y1
sub AL, y2
jmp endif_neg1 ; if y1 > y2
if_neg1:
sub AL, y1
endif_neg1:
; abs ( x3 - x1 )
mov x, AL
mov AL, x1
cmp AL, x3
ja if_neg2 ; if x1 > x3
mov Al, x3
sub AL, x1
jmp endif_neg2 ; if x3 > x1
if_neg2:
sub AL, x3
endif_neg2:
cmp Al, x
je if_square
jmp if_Rectangle
if_Square: ; if the 4 points form a Square fitch the xSquaresList and the ySquaresList offsets and store the points in the mentioned order.
mov EDI, xSquaresOffset
mov ESI, ySquaresOffset
mov Al, x1
mov [EDI], AL
mov AL, y1
mov [ESI], AL
inc EDI
inc ESI
mov Al, x2
mov [EDI], AL
mov AL, y2
mov [ESI], AL
inc EDI
inc ESI
mov Al, x3
mov [EDI], AL
mov AL, y3
mov [ESI], AL
inc EDI
inc ESI
mov Al, x4
mov [EDI], AL
mov AL, y4
mov [ESI], AL
inc EDI
inc ESI
mov xSquaresOffset, EDI
mov ySquaresOffset, ESI
inc SquaresNumber
jmp endif_Rectangle
endif_Square:
if_Rectangle:; if the 4 points form a rectangle fitch the xRectanglesList and the yRectanglesList offsets and store the points in the mentioned order.
mov EDI, xRectanglesOffset
mov ESI, yRectanglesOffset
mov Al, x1
mov [EDI], AL
mov AL, y1
mov [ESI], AL
inc EDI
inc ESI
mov Al, x2
mov [EDI], AL
mov AL, y2
mov [ESI], AL
inc EDI
inc ESI
mov Al, x3
mov [EDI], AL
mov AL, y3
mov [ESI], AL
inc EDI
inc ESI
mov Al, x4
mov [EDI], AL
mov AL, y4
mov [ESI], AL
inc EDI
inc ESI
mov xRectanglesOffset, EDI
mov yRectanglesOffset, ESI
inc rectanglesNumber
endif_Rectangle:
RET
checkSquare ENDP
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;findRectangles PROC uses 4 nested points to find all possible 4 points that can form a rectangle or a square
; by insureing that every 2 points are in fact the end and start of an already existing line.
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
findRectangles PROC
mov EDX, offset xIntersections
mov EBX, offset yIntersections
mov xRectanglesOffset, offset xRectanglesList
mov yRectanglesOffset, offset yRectanglesList
mov xSquaresOffset, offset xSquaresList
mov ySquaresOffset, offset ySquaresList
mov ECX, intersectionsNumber
loop_firstPoint: ; choses a point to stand as point A in the rectangle.
; store the point's values in the respecting variables.
mov AL, [EDX]
mov x1, AL
mov AL, [EBX]
mov y1, AL
; temproarly store the values of the registers in variables to reuse them in the second loop.