-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive-backtracking.asm
373 lines (318 loc) · 4.9 KB
/
recursive-backtracking.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
.data
str: .space 2000
x: .space 2000
n: .long 0
m: .long 0
nr: .long 0
i: .long 0
trei: .long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
inapoi: .long 0
in: .long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
chdelim: .asciz " "
formatscanf: .asciz "%[^\n]s"
formatprintf: .asciz "%d "
final: .asciz "\n"
k: .long 0
ind: .long 0
nul: .asciz "-1\n"
ls: .long 0
li: .long 0
e: .long 1
.text
ok:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx
xorl %eax, %eax
movl (%esi, %ebx, 4), %ebx
cmp $3, (%edi, %ebx, 4)
je ok_exit
movl $1, %eax
ok_exit:
popl %ebp
ret
solutie:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx
xorl %eax, %eax
incl %ebx
cmp nr, %ebx
je solutie_exit
movl $1, %eax
solutie_exit:
popl %ebp
ret
distanta:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx
subl m, %ebx
movl $1, %eax
movl %ebx, li
addl m, %ebx
addl m, %ebx
addl $1, %ebx
movl %ebx, ls
movl li, %ebx
cmp $0, %ebx
jl for3
movl ls, %ebx
cmp nr, %ebx
jg for2
movl li, %ebx
for1:
cmp ls, %ebx
je distanta_exit
cmp k, %ebx
je for1cont
movl (%esi, %ebx, 4), %edx
pushl %ebx
movl k, %ebx
movl (%esi, %ebx, 4), %eax
popl %ebx
cmp %edx, %eax
je return0
for1cont:
incl %ebx
jmp for1
for2:
movl li, %ebx
loopfor2:
cmp nr, %ebx
je distanta_exit
cmp k, %ebx
je for2cont
movl (%esi, %ebx, 4), %edx
pushl %ebx
movl k, %ebx
movl (%esi, %ebx, 4), %eax
popl %ebx
cmp %edx, %eax
je return0
for2cont:
incl %ebx
jmp loopfor2
for3:
xorl %ebx, %ebx
loopfor3:
cmp ls, %ebx
je distanta_exit
cmp k, %ebx
je for3cont
movl (%esi, %ebx, 4), %edx
pushl %ebx
movl k, %ebx
movl (%esi, %ebx, 4), %eax
popl %ebx
cmp %edx, %eax
je return0
for3cont:
incl %ebx
jmp loopfor3
return0:
xorl %eax, %eax
distanta_exit:
popl %ebp
ret
backtr:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl %edx, k
movl 12(%ebp), %ecx
movl %ecx, ind
movl (%esi, %edx, 4), %eax
cmp $0, %eax
jg nenule
gol:
cmp n, %ecx
jg nenule
for:
movl %ecx, (%esi, %edx, 4)
pushl k
call ok
popl k
cmp $0, %eax
je maxim
dist:
pushl %edx
pushl k
call distanta
popl k
popl %edx
cmp $0, %eax
je maxim
movl $0, inapoi
addl $1, (%edi, %ecx, 4)
verificare:
pushl k
call solutie
popl k
cmp $0, %eax
je backtr_exit
callbacktr:
addl $1, %edx
pushl $1
pushl %edx
call backtr
popl %ebx
popl %ebx
jmp backtr_exit
maxim:
cmp n, %ecx
jl contfor
estemaxim:
movl $0, (%esi, %edx, 4)
movl $1, inapoi
cmp $0, k
jl imposibil
pasinapoi:
decl %edx
movl (%esi, %edx, 4), %ebx
movl %ebx, ind
addl $1, ind
movl $in, %edi
cmp $0, (%edi, %edx, 4)
jne pasinapoi
cmp n, %ebx
je resetare
movl $trei, %edi
decl (%edi, %ebx, 4)
movl $0, (%esi, %edx, 4)
jmp callbacktr1
resetare:
movl $trei, %edi
decl (%edi, %ebx, 4)
movl $0, (%esi, %edx, 4)
jmp pasinapoi
contfor:
movl $trei, %edi
incl %ecx
jmp for
nenule:
cmp $0, inapoi
je verificare
jmp pasinapoi
imposibil:
movl $0, e
jmp backtr_exit
callbacktr1:
pushl ind
pushl %edx
call backtr
popl %ebx
popl %ebx
backtr_exit:
popl %ebp
ret
.global main
main:
movl $x, %esi
movl $in, %edi
pushl $str
pushl $formatscanf
call scanf
popl %ebx
popl %ebx
pushl $chdelim
pushl $str
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
movl %eax, n
pushl $chdelim
pushl $0
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
movl %eax, m
xorl %edx, %edx
movl $3, %eax
movl n, %ebx
mull %ebx
movl %eax, nr
xorl %ebx, %ebx
strtoint:
cmp nr, %ebx
je cateori
pushl %ebx
pushl $chdelim
pushl $0
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
popl %ebx
adaugvector:
movl %eax, (%esi, %ebx, 4)
cmp $0, %eax
jne inin
incl %ebx
jmp strtoint
inin:
movl $1, (%edi, %ebx, 4)
incl %ebx
jmp strtoint
cateori:
movl $trei, %edi
xorl %ecx, %ecx
forcateori:
pushl %ecx
cmp nr, %ecx
je prelucrare
movl (%esi, %ecx, 4), %eax
addl $1, (%edi, %eax, 4)
incl %ecx
jmp forcateori
prelucrare:
pushl $1
pushl $0
call backtr
popl %ebx
popl %ebx
cmp $0, e
je nuexista
afis:
xorl %ecx, %ecx
forafis:
pushl %ecx
cmp nr, %ecx
je exit
pushl (%esi, %ecx, 4)
pushl $formatprintf
call printf
popl %ebx
popl %ebx
pushl $0
call fflush
popl %ebx
popl %ecx
incl %ecx
jmp forafis
nuexista:
movl $4, %eax
movl $1, %ebx
movl $nul, %ecx
movl $3, %edx
int $0x80
movl $1, %eax
xorl %ebx, %ebx
int $0x80
exit:
movl $4, %eax
movl $1, %ebx
movl $final, %ecx
movl $2, %edx
int $0x80
movl $1, %eax
xorl %ebx, %ebx
int $0x80