-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigraph.asm
More file actions
335 lines (277 loc) · 5.42 KB
/
Copy pathDigraph.asm
File metadata and controls
335 lines (277 loc) · 5.42 KB
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
section .data
fmt_info: db "Digraph: %d vertices, %d edges", 10, 0
fmt_edge: db " %d -> %d (w=%d)", 10, 0
fmt_has: db "Edge %d->%d exists: %d", 10, 0
fmt_outdeg: db "Out-degree of %d: %d", 10, 0
fmt_pass: db "All tests passed.", 10, 0
DIGRAPH_V equ 0
DIGRAPH_E equ 4
DIGRAPH_CAP equ 8
DIGRAPH_EDGES equ 16
DIGRAPH_SIZE equ 24
EDGE_SRC equ 0
EDGE_DST equ 4
EDGE_WEIGHT equ 8
EDGE_SIZE equ 12
section .text
global main, digraph_create, digraph_add_edge, digraph_has_edge, digraph_out_degree, digraph_free
extern malloc, realloc, free, printf
digraph_create:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
sub rsp, 8
mov r12d, edi
mov r13d, esi
mov edi, DIGRAPH_SIZE
call malloc
mov rbx, rax
mov dword [rbx + DIGRAPH_V], r12d
mov dword [rbx + DIGRAPH_E], 0
mov dword [rbx + DIGRAPH_CAP], r13d
mov eax, r13d
imul eax, EDGE_SIZE
movsxd rdi, eax
call malloc
mov [rbx + DIGRAPH_EDGES], rax
mov rax, rbx
add rsp, 8
pop r13
pop r12
pop rbx
pop rbp
ret
digraph_add_edge:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
push r14
push r15
sub rsp, 8
mov rbx, rdi
mov r12d, esi
mov r13d, edx
mov r14d, ecx
mov eax, [rbx + DIGRAPH_E]
cmp eax, [rbx + DIGRAPH_CAP]
jl .add_no_grow
mov eax, [rbx + DIGRAPH_CAP]
test eax, eax
jnz .cap_not_zero
mov eax, 1
jmp .cap_done
.cap_not_zero:
shl eax, 1
.cap_done:
mov [rbx + DIGRAPH_CAP], eax
imul eax, EDGE_SIZE
movsxd rsi, eax
mov rdi, [rbx + DIGRAPH_EDGES]
call realloc
mov [rbx + DIGRAPH_EDGES], rax
.add_no_grow:
mov eax, [rbx + DIGRAPH_E]
imul eax, EDGE_SIZE
movsxd rcx, eax
mov rdi, [rbx + DIGRAPH_EDGES]
add rdi, rcx
mov dword [rdi + EDGE_SRC], r12d
mov dword [rdi + EDGE_DST], r13d
mov dword [rdi + EDGE_WEIGHT], r14d
inc dword [rbx + DIGRAPH_E]
add rsp, 8
pop r15
pop r14
pop r13
pop r12
pop rbx
pop rbp
ret
digraph_has_edge:
push rbp
mov rbp, rsp
mov rcx, [rdi + DIGRAPH_EDGES]
mov r8d, [rdi + DIGRAPH_E]
xor eax, eax
.has_loop:
cmp eax, r8d
jge .has_not_found
mov r9d, eax
imul r9d, EDGE_SIZE
movsxd r9, r9d
cmp dword [rcx + r9 + EDGE_SRC], esi
jne .has_next
cmp dword [rcx + r9 + EDGE_DST], edx
jne .has_next
mov eax, 1
pop rbp
ret
.has_next:
inc eax
jmp .has_loop
.has_not_found:
xor eax, eax
pop rbp
ret
digraph_out_degree:
push rbp
mov rbp, rsp
mov rcx, [rdi + DIGRAPH_EDGES]
mov r8d, [rdi + DIGRAPH_E]
xor eax, eax
xor r9d, r9d
.outdeg_loop:
cmp r9d, r8d
jge .outdeg_done
mov r10d, r9d
imul r10d, EDGE_SIZE
movsxd r10, r10d
cmp dword [rcx + r10 + EDGE_SRC], esi
jne .outdeg_next
inc eax
.outdeg_next:
inc r9d
jmp .outdeg_loop
.outdeg_done:
pop rbp
ret
digraph_free:
push rbp
mov rbp, rsp
push rbx
mov rbx, rdi
mov rdi, [rbx + DIGRAPH_EDGES]
call free
mov rdi, rbx
call free
pop rbx
pop rbp
ret
digraph_print:
push rbp
mov rbp, rsp
push rbx
push r12
push r13
sub rsp, 8
mov rbx, rdi
lea rdi, [rel fmt_info]
mov esi, [rbx + DIGRAPH_V]
mov edx, [rbx + DIGRAPH_E]
xor eax, eax
call printf
xor r12d, r12d
mov r13d, [rbx + DIGRAPH_E]
.print_loop:
cmp r12d, r13d
jge .print_done
mov eax, r12d
imul eax, EDGE_SIZE
movsxd rcx, eax
mov rdi, [rbx + DIGRAPH_EDGES]
add rdi, rcx
push rdi
lea rdi, [rel fmt_edge]
mov esi, [rsp]
mov rsi, [rsp]
mov esi, dword [rsi + EDGE_SRC]
mov rax, [rsp]
mov edx, dword [rax + EDGE_DST]
mov rax, [rsp]
mov ecx, dword [rax + EDGE_WEIGHT]
xor eax, eax
call printf
add rsp, 8
inc r12d
jmp .print_loop
.print_done:
add rsp, 8
pop r13
pop r12
pop rbx
pop rbp
ret
main:
push rbp
mov rbp, rsp
push rbx
sub rsp, 8
mov edi, 6
mov esi, 4
call digraph_create
mov rbx, rax
mov rdi, rbx
mov esi, 5
mov edx, 2
mov ecx, 1
call digraph_add_edge
mov rdi, rbx
mov esi, 5
mov edx, 0
mov ecx, 1
call digraph_add_edge
mov rdi, rbx
mov esi, 4
mov edx, 0
mov ecx, 1
call digraph_add_edge
mov rdi, rbx
mov esi, 4
mov edx, 1
mov ecx, 1
call digraph_add_edge
mov rdi, rbx
mov esi, 2
mov edx, 3
mov ecx, 1
call digraph_add_edge
mov rdi, rbx
mov esi, 3
mov edx, 1
mov ecx, 1
call digraph_add_edge
mov rdi, rbx
call digraph_print
mov rdi, rbx
mov esi, 5
mov edx, 2
call digraph_has_edge
mov ecx, eax
lea rdi, [rel fmt_has]
mov esi, 5
mov edx, 2
xor eax, eax
call printf
mov rdi, rbx
mov esi, 2
mov edx, 5
call digraph_has_edge
mov ecx, eax
lea rdi, [rel fmt_has]
mov esi, 2
mov edx, 5
xor eax, eax
call printf
mov rdi, rbx
mov esi, 5
call digraph_out_degree
mov edx, eax
lea rdi, [rel fmt_outdeg]
mov esi, 5
xor eax, eax
call printf
lea rdi, [rel fmt_pass]
xor eax, eax
call printf
mov rdi, rbx
call digraph_free
xor eax, eax
add rsp, 8
pop rbx
pop rbp
ret
section .note.GNU-stack noalloc noexec nowrite progbits