-
Notifications
You must be signed in to change notification settings - Fork 0
/
isosceles.ASM
303 lines (263 loc) · 9.65 KB
/
isosceles.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
; Daria Tarasova, 197 group
format PE console
entry start
include 'win32a.inc'
include 'macro.inc'
section '.data' data readable writable
; Tools for input user numbers
digit2In db '%d %d', 0
digitIn db '%d', 0
; Messages for user
fail1Str db 'These 3 vectors can not form a triangle.', 10, 0
fail2Str db 'These 3 vectors can not form an isocleles triangle.', 10, 0
successStr db 'These 3 vectors can form an isosceles triangle.', 10, 0
firstPoint db 'Enter coordinates of the first point', 10, 0
secondPoint db 'Enter coordinates of the second point', 10, 0
thirdPoint db 'Enter coordinates of the third point', 10, 0
inputCoord db 'Input X and Y coordinates separated by space: ', 10, 0
wrongCoord db 'Wrong input: X and Y should be in range [-65536; 65535].', 10, 0
endMessage db 'Press any key to exit...', 10, 0
; Tools for output of numbers
pointsStr db '{%d, %d} {%d, %d}', 10, 0
newLine db '', 10, 0
; Coordinates
p1x dd ?
p2x dd ?
p3x dd ?
p1y dd ?
p2y dd ?
p3y dd ?
; Temporary stack values
tmpStack dd ?
tmpCheckStack dd ?
s dd 0
a dd 0
b dd 0
c dd 0
; Procedures variables
x1 dd ?
x2 dd ?
y1 dd ?
y2 dd ?
l1 dd ?
l2 dd ?
temp dd ?
NULL = 0
;------------------------------------------------------------------------
section '.code' code readable executable
start:
; Points' coordinates input
call getFirst
call getSecond
call getThird
; Check if vectors can form a triangle
mov ebx, [p2y]
sub ebx, [p3y]
mov eax, [p1x]
push eax
push ebx
call mult
add [s], eax
mov ebx, [p3y]
sub ebx, [p1y]
mov eax, [p2x]
push eax
push ebx
call mult
add [s], eax
mov ebx, [p1y]
sub ebx, [p2y]
mov eax, [p3x]
push eax
push ebx
call mult
add [s], eax ; s = p1x * (p2y - p3y) + p2x * (p3y - p1y) + p3x * (p1y - p2y)
cmp [s], 0 ; If s = 0 then the vectors can form a triangle
jne checkIsosceles
; Can't form a triangle
push fail1Str
call [printf]
call endProg
;--------------------------------------------------------------------
; Check if value is in range [0; 65535]
checkValue:
; Check bottom border
mov [tmpCheckStack], esp
cmp eax, 65535
jle checkBottom
jmp wrongVal
; Print message about wrong input and exit program
wrongVal:
push wrongCoord
call [printf]
call endProg
; Check upper border
checkBottom:
cmp eax, -65536
jl wrongVal
mov esp, [tmpCheckStack]
ret
;--------------------------------------------------------------------
; Input of coordinates of the first point - the end of the segment
getFirst:
; Prompt the user to enter values
mov [tmpStack], esp ; memorizing the position in the stack
push firstPoint
call [printf]
push inputCoord
call [printf]
; Reading the entered values
push p1y
push p1x
push digit2In
call [scanf]
; Checking the entered value
mov eax, [p1x]
call checkValue
mov eax, [p1y]
call checkValue
mov esp, [tmpStack] ; restoring the position in the stack
ret
;--------------------------------------------------------------------
; Input of coordinates of the first point - the end of the segment
getSecond:
; Prompt the user to enter values
mov [tmpStack], esp ; memorizing the position in the stack
push secondPoint
call [printf]
push inputCoord
call [printf]
; Reading the entered values
push p2y
push p2x
push digit2In
call [scanf]
; Checking the entered value
mov eax, [p2x]
call checkValue
mov eax, [p2y]
call checkValue
mov esp, [tmpStack] ; restoring the position in the stack
ret
;--------------------------------------------------------------------
; Input of coordinates of the first point - the end of the segment
getThird:
; Prompt the user to enter values
mov [tmpStack], esp ; memorizing the position in the stack
push thirdPoint
call [printf]
push inputCoord
call [printf]
; Reading the entered values
push p3y
push p3x
push digit2In
call [scanf]
; Checking the entered value
mov eax, [p3x]
call checkValue
mov eax, [p3y]
call checkValue
mov esp, [tmpStack] ; restoring the position in the stack
ret
;--------------------------------------------------------------------
checkIsosceles:
; Calculate square roots of the sides
lengthRoot p1x, p1y, p2x, p2y
mov [a], eax ; a = (p1x - p2x) ^ 2 + (p1y - p2y)
lengthRoot p1x, p1y, p3x, p3y
mov [b], eax ; b = (p1x - p3x) ^ 2 + (p1y - p3y)
lengthRoot p2x, p2y, p3x, p3y
mov [c], eax ; c = (p2x - p3x) ^ 2 + (p2y - p3y)
push [a]
push [b]
call areEqual
cmp eax, 1 ; if a = b then the vectors can form an isosceles triangles
je foundEqual
push [a]
push [c]
call areEqual
cmp eax, 1 ; if a = c then the vectors can form an isosceles triangles
je foundEqual
push [b]
push [c]
call areEqual
cmp eax, 1 ; if b = c then the vectors can form an isosceles triangles
je foundEqual
; Equal sides were not found
push fail2Str
call [printf]
call endProg
foundEqual:
push successStr
call [printf]
call endProg
;--------------------------------------------------------------------
; Ends the program
endProg:
push endMessage
call [printf]
call [getch]
push NULL
call [ExitProcess]
;---------------------------------------------------------------------
; The multiplication result is stored in eax
mult:
mov [tmpStack], esp
pop eax
pop eax
pop ebx
xor ecx, ecx
xor edx, edx
multLoop:
cmp ecx, ebx
jge endMultLoop
add edx, eax
inc ecx
jmp multLoop
endMultLoop:
mov eax, edx
mov esp, [tmpStack]
ret
;-----------------------------------------------------------------------
; Stores in eax the absolute value of the top value on the stack
abs:
mov [tmpStack], esp
pop eax
pop eax
cmp eax, 0
jl removeMinus
mov esp, [tmpStack]
ret
removeMinus:
xor ebx, ebx ; ebx = 0
sub ebx, eax
mov eax, ebx
mov esp, [tmpStack]
ret
;------------------------------------------------------------------------
; Checks if the top 2 values on the stack are equal, if true eax = 1, else eax = 0
areEqual:
mov [tmpStack], esp
pop eax
pop eax
pop ebx
cmp eax, ebx
je areEqualRes
mov eax, 0
mov esp, [tmpStack]
ret
areEqualRes:
mov eax, 1
mov esp, [tmpStack]
ret
;------------------------------------------------------------------------
section 'idata' import data readable
library kernel, 'kernel32.dll', \
msvcrt, 'msvcrt.dll'
import kernel, \
ExitProcess, 'ExitProcess'
import msvcrt, \
printf, 'printf', \
scanf, 'scanf', \
getch, '_getch'