-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.asm
409 lines (328 loc) · 5.98 KB
/
file.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
.data
// long
ok: .long 1
i: .long 0
j: .long 0
// asciz
nul: .asciz "\n"
formatscanf: .asciz "%[^\n]"
formatprintf: .asciz "%d "
formatnegativ: .asciz "%d\n"
formatnul: .asciz "%s"
delim: .asciz " "
// input
input: .space 500
// array
v: .space 500
poz: .space 500
b: .space 500
f: .space 500
// variables
a: .space 4
el: .space 4
k: .space 4
n: .space 4
n3: .space 4
m: .space 4
pozitie: .space 4
frecventa: .space 4
x: .space 4
.text
verif:
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %esi
pushl %edi
movl 8(%ebp), %ebx
cmp $0, %ebx
je cont
pregparametrii:
movl 8(%ebp), %eax
movl %eax, a
subl m, %eax
forst:
movl a, %ebx
cmp %ebx, %eax
jg dreapta
cmp $0, %ebx
jl dreapta
movl (%edi, %ebx, 4), %edx
cmp %edx, 12(%ebp)
je rau
jmp decrementarea
dreapta:
movl 8(%ebp), %eax
movl %eax, a
addl m, %eax
fordr:
movl a, %ebx
cmp %ebx, %eax
jl cont
cmp n3, %ebx
je cont
movl (%edi, %ebx, 4), %edx
cmp %edx, 12(%ebp)
je rau
jmp incrementarea
cont:
movl 8(%ebp), %ebx
movl 12(%ebp), %edx
movl %edx, (%edi, %ebx, 4)
movl 12(%ebp), %ebx
pushl %esi
lea f, %esi
movl (%esi, %ebx, 4), %edx
incl %edx
movl %edx, (%esi, %ebx, 4)
popl %esi
jmp bun
incrementarea:
movl a, %ebx
incl %ebx
movl %ebx, a
jmp fordr
decrementarea:
movl a, %ebx
decl %ebx
movl %ebx, a
jmp forst
rau:
xorl %eax, %eax
jmp retverif
bun:
movl $1, %eax
retverif:
popl %edi
popl %esi
popl %ebx
popl %ebp
ret
//BACKTRACKING
bkt:
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %esi
pushl %edi
// Assign to x the value of the position in the vector for which we generate an element
movl 8(%ebp), %edx
movl %edx, x
// We initialize the counter for 'for'
movl $1, %ebx
movl %ebx, k
forpermutare:
// If we have reached n, the for ends
movl k, %ecx
cmp n, %ecx
jg returnarerea
// If ok is 0, it means that we have found a correct permutation
movl ok, %ebx
cmp $0, %ebx
je returnarerea
// We save the position in the vector where we have to load the found value
movl 8(%ebp), %ebx
movl (%esi, %ebx, 4), %edx
movl %edx, pozitie
pushl %esi
lea b, %esi
// We put the found value in the permutation vector
movl k, %edx
movl %edx, (%esi, %ebx, 4)
// We check if the number appeared less than 3 times in a row
lea f, %esi
movl k, %ebx
movl (%esi, %ebx, 4), %edx
movl %edx, frecventa
movl frecventa, %ebx
popl %esi
cmp $3, %ebx
je incrementarek
aici:
// We check the found element
pushl k
pushl pozitie
call verif
popl %ebx
popl %ebx
cmp $0, %eax
je incrementarek
salut2:
// If we found j - 1 elements, it means that we have to display the permutation
movl 8(%ebp), %ebx
movl j, %edx
decl %edx
cmp %ebx, %edx
je returnarebuna
// If not, we call the bkt function
pushl k
incl %ebx
pushl %ebx
call bkt
popl %ebx
popl k
lea v, %edi
lea b, %esi
// When we return from bkt, we decrease the frequency of the element and
// remove it from the vector, if we have NOT already found the permutation
movl ok, %ebx
cmp $1, %ebx
je stergere
jmp incrementarek
stergere:
pushl %esi
lea f, %esi
movl k, %ebx
movl (%esi, %ebx, 4), %edx
decl %edx
movl %edx, (%esi, %ebx, 4)
popl %esi
movl 8(%ebp), %ebx
movl (%esi, %ebx, 4), %edx
movl %edx, pozitie
movl pozitie, %ebx
xorl %edx, %edx
movl %edx, (%edi, %ebx, 4)
incrementarek:
movl k, %ecx
incl %ecx
movl %ecx, k
jmp forpermutare
returnarebuna:
movl $0, %ebx
movl %ebx, ok
returnarerea:
popl %edi
popl %esi
popl %ebx
popl %ebp
ret
.global main
main:
// Read the string
pushl $input
pushl $formatscanf
call scanf
popl %ebx
popl %ebx
// We initialise n
pushl $delim
pushl $input
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
movl %eax, n
// We initialise m
pushl $delim
pushl $0
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
movl %eax, m
// We prepare the vector pos
lea poz, %esi
// We prepare the vector v
lea v, %edi
movl $3, %eax
xorl %edx, %edx
mull n
movl %eax, n3
// We read the vector elements
forv:
movl i, %ecx
cmp n3, %ecx
je pasul1
pushl $delim
pushl $0
call strtok
popl %ebx
popl %ebx
pushl %eax
call atoi
popl %ebx
movl i, %ecx
movl %eax, (%edi, %ecx, 4)
// We check if the read element is 0. If so, it means that I have memorize its position
// for backtracking
cmp $0, %eax
je pozitii
continuare1:
pushl %esi
lea f, %esi
movl (%esi, %eax, 4), %ebx
incl %ebx
movl %ebx, (%esi, %eax, 4)
popl %esi
jmp incrementare
// We memorize the position where element 0 appeared
pozitii:
movl j, %ebx
movl %ecx, (%esi, %ebx, 4)
incl %ebx
movl %ebx, j
jmp continuare1
// We increment the contor
incrementare:
incl %ecx
movl %ecx, i
jmp forv
// We check if at least one 0 appeared
pasul1:
movl j, %ebx
cmp $0, %ebx
je condamnat
jmp pasul2
// We call the backtracking function
pasul2:
pushl $0
call bkt
popl %ebx
lea v, %edi
salut:
// We check if a permutation could be generated, and if not, we jump to the 'condamnat' tah where we display -1
movl ok, %ebx
cmp $1, %ebx
je condamnat
movl $0, %ecx
movl %ecx, i
jmp permutare
permutare:
movl i, %ecx
cmp n3, %ecx
je newline
forb:
movl (%edi, %ecx, 4), %eax
pushl %eax
pushl $formatprintf
call printf
popl %ebx
popl %ebx
movl i, %ecx
incl %ecx
movl %ecx, i
jmp permutare
newline:
pushl $nul
pushl $formatnul
call printf
popl %ebx
popl %ebx
jmp exit
// We display -1 if a valid permutation can't be generated.
condamnat:
pushl $-1
pushl $formatnegativ
call printf
popl %ebx
popl %ebx
jmp exit
exit:
movl $1, %eax
xorl %ebx, %ebx
int $0x80