-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonachi_validator.ys
395 lines (359 loc) · 9.99 KB
/
fibonachi_validator.ys
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
irmovq stack, %rsp
irmovq stack, %rbp
main:
call Memory_reader # Read number from array
rrmovq %rax, %rbx
rrmovq %rax, %rsi # Set values for multiplication
call square_number
rrmovq %rdx, %rbx # Move number to be multiplied
call multiply_by_5
# Save number + 4 to the stack
irmovq $4, %rdi
addq %rdi, %rdx
pushq %rdx
# Check if (number - 4) is a fibonachi sequence number
irmovq $8, %rdi
subq %rdi, %rdx
call Fibonachi_test # Check if the number is a fibonachi number
call clear_registers # Clear registers
popq %rdx # Pop (number + 4) if the previous number was not a fibonachi number
call Fibonachi_test
# At this point neither of the numbers were a number of the fibonachi sequence
rrmovq %r9, %rax # Moves the input number to %rax as per specifications by the assignment
halt
Fibonachi_test:
# clear registers
irmovq $0, %rax
irmovq $0, %rbx
irmovq $0, %rdi
rrmovq %rdx, %r12
call Squareroot
rrmovq %rdx, %r8
irmovq $0, %rdx
# RCX = squareroot result
rrmovq %rcx, %rbx # Set multiplication values
rrmovq %rcx, %rsi #
call square_number
subq %r8, %rdx # Test if 0
je fibonacci_number_found
ret
fibonacci_number_found:
call clear_registers
jmp main
clear_registers:
irmovq $0, %rcx
irmovq $0, %rdx
irmovq $0, %rbx
irmovq $0, %rdi
irmovq $0, %r14
ret
Memory_reader:
irmovq $0, %r8 # Set constant 0
irmovq $8, %r14 # Set constant 8
mrmovq (%r11),%rax # Copies number from array
subq %r8, %rax # End program if the number is zero, as per assignment specifications
je the_input_number_is_zero
addq %r14, %r11 # Add 8 to the memory reader register, so it reads the next number.
rrmovq %rax, %r9
ret
the_input_number_is_zero:
halt
multiply_by_5:
call multiply_by_5_prep
ret
square_number:
call multiplication_prep
ret
multiplication_prep:
irmovq $0, %rdx
irmovq $1, %rdi
rrmovq %rbx, %rbx # Number to be multiplied
rrmovq %rsi, %rsi # Number that is multiplied with
jmp multiplication_loop
multiplication_loop:
addq %rbx, %rdx
subq %rdi, %rsi
jle return_function
jmp multiplication_loop
multiply_by_5_prep:
irmovq $0, %rdx
irmovq $1, %rdi
irmovq $5, %rsi
jmp multiplication_loop
division:
call Jakolasku_1048576_prep
ret
Squareroot:
irmovq $16777216, %r8 #bit numero 2^24
call Squareroot_start
ret
Squareroot_start:
call set_bit_to_be_divided
call Bit_shift_right2
rrmovq %rax, %r8
# "Bit" is of correct size
# Continue to the second loop of the C algorhythm
jmp Sqrt_jne_bit0
Sqrt_jne_bit0:
# Empty r10
irmovq $0, %r10
#if bit is 0, then it ends the program
subq %r13, %r8
je return_function
# bit is non zero at this point
# if num >= res + bit
# r10 = res + bit
addq %rcx, %r10
addq %r8, %r10
# test >=
subq %r10, %r12
jge Sqrt_if_num_jge_sum_of_res_and_bit
addq %r10, %r12 # Add back after test
call set_result_to_be_divided
call Bit_shift_right1
# Save result
rrmovq %rax, %rcx
call set_bit_to_be_divided
call Bit_shift_right2
# save result
rrmovq %rax, %r8
jmp Sqrt_jne_bit0
Sqrt_if_num_jge_sum_of_res_and_bit:
# Add back after test
addq %r10, %r12
# num -= res + bit;
subq %rcx, %r12 # numero -= result
subq %r8, %r12 # numero -= bit
# res = (res >> 1) + bit
call set_result_to_be_divided
call Bit_shift_right1
rrmovq %rax, %rcx
addq %r8, %rcx
call set_bit_to_be_divided
call Bit_shift_right2
rrmovq %rax, %r8
jmp Sqrt_jne_bit0
set_bit_to_be_divided:
# Set number to correct register for division
rrmovq %r8, %rbx
ret
set_result_to_be_divided:
# Set number to correct register for division
rrmovq %rcx, %rbx
ret
Bit_shift_right2:
# Divide number by 2, 2 times
irmovq $0, %rax # result
call Jakolasku
# Save result temporarily
rrmovq %rax, %rbx
irmovq $0, %rax # result
call Jakolasku
ret
Bit_shift_right1:
irmovq $0, %rax # result
call Jakolasku
ret
return_function:
ret
###
# Division, that starts by dividing with 2^20 -> 2^16 -> ... -> 2
# Prep functions prepare the correct values for dividing by a smaller number
###
Division_1048576_prep:
#irmovq $65536, %r14
#addq %r14, %rbx
irmovq $1048576, %rsi
irmovq $524288, %rdi
jmp Division_1048576
Division_1048576:
subq %rsi, %rbx # Test if 0 or negative
jl Division_65536_prep
addq %rdi, %rax # incerement counter
jmp Division_1048576
Division_65536_prep:
irmovq $1048576, %r14
addq %r14, %rbx
irmovq $65536, %rsi
irmovq $32768, %rdi
jmp Division_65536
Division_65536:
subq %rsi, %rbx # test if 0 or negative
jl Division_32768_prep
addq %rdi, %rax # increment counter
jmp Division_65536
Division_32768_prep:
irmovq $65536, %r14
addq %r14, %rbx
irmovq $32768, %rsi
irmovq $16384, %rdi
jmp Division_32768
Division_32768:
subq %rsi, %rbx # test if 0 or negative
jl Division_4096_prep
addq %rdi, %rax # increment counter
jmp Division_32768
Division_4096_prep:
irmovq $32768, %r14
addq %r14, %rbx
irmovq $4096, %rsi
irmovq $2048, %rdi
jmp Division_4096
Division_4096:
subq %rsi, %rbx # test if 0 or negative
jl Division_2048_prep
addq %rdi, %rax #lisää 2148 counteriin
jmp Division_4096
Division_2048_prep:
irmovq $4096, %r14
addq %r14, %rbx
#tarkistetaan onko jaettava 0
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $2048, %rsi
irmovq $1024, %rdi
jmp Division_2048
Division_2048:
subq %rsi, %rbx # test if 0 or negative
jl Division_1024_prep
addq %rdi, %rax #lisää 1024 counteriin
jmp Division_2048
Division_1024_prep:
irmovq $2048, %r14
addq %r14, %rbx
#tarkistetaan onko jaettava 0
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $1024, %rsi
irmovq $512, %rdi
jmp Division_1024
Division_1024:
subq %rsi, %rbx # test if 0 or negative
jl Division_512_prep
addq %rdi, %rax #lisää 512 counteriin
jmp Division_1024
Division_512_prep:
irmovq $1024, %r14
addq %r14, %rbx
#tarkistetaan onko jaettava 0
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $512, %rsi
irmovq $256, %rdi
jmp Division_512
Division_512:
subq %rsi, %rbx # test if 0 or negative
jl Division_256_prep
addq %rdi, %rax #lisää 256 counteriin
jmp Division_512
Division_256_prep:
irmovq $512, %r14
addq %r14, %rbx
#tarkistetaan onko jaettava 0
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $256, %rsi
irmovq $128, %rdi
jmp Division_256
Division_256:
subq %rsi, %rbx # test if 0 or negative
jl Division_128_prep
addq %rdi, %rax #lisää 128 counteriin
jmp Division_256
Division_128_prep:
irmovq $256, %r14
addq %r14, %rbx
#tarkistetaan onko jaettava 0
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $128, %rsi
irmovq $64, %rdi
jmp Division_128
Division_128:
subq %rsi, %rbx # test if 0 or negative
jl Division_64_prep
addq %rdi, %rax #lisää 64 counteriin
jmp Division_128
Division_64_prep:
irmovq $128, %r14
addq %r14, %rbx
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $64, %rsi
irmovq $32, %rdi
jmp Division_64
Division_64:
subq %rsi, %rbx # test if 0 or negative
jl Division_32_prep
addq %rdi, %rax #lisää 32 counteriin
jmp Division_64
Division_32_prep:
irmovq $64, %r14
addq %r14, %rbx
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $32, %rsi
irmovq $16, %rdi
jmp Division_32
Division_32:
subq %rsi, %rbx # test if 0 or negative
jl Division_16_prep
addq %rdi, %rax #lisää 16 counteriin
jmp Division_32
Division_16_prep:
irmovq $32, %r14
addq %r14, %rbx
#tarkistetaan onko jaettava 0
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $16, %rsi
irmovq $8, %rdi
jmp Division_16
Division_16:
subq %rsi, %rbx # test if 0 or negative
jl Division_8_prep
addq %rdi, %rax #lisää 8 counteriin
jmp Division_16
Division_8_prep:
irmovq $16, %r14
addq %r14, %rbx
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $8, %rsi
irmovq $4, %rdi
jmp Division_8
Division_8:
subq %rsi, %rbx # test if 0 or negative
jl Division_4_prep
addq %rdi, %rax #lisää 4 counteriin
jmp Division_8
Division_4_prep:
irmovq $8, %r14
addq %r14, %rbx
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $4, %rsi
irmovq $2, %rdi
jmp Division_4
Division_4:
subq %rsi, %rbx # test if 0 or negative
jl Division_2_prep
addq %rdi, %rax #lisää 2 counteriin
jmp Division_4
Division_2_prep:
irmovq $4, %r14
addq %r14, %rbx
subq %r13, %rbx # test if 0 or negative
jle return_function
irmovq $2, %rsi
irmovq $1, %rdi
jmp Division_2
Division_2:
subq %rsi, %rbx # test if 0 or negative
jl return_function
addq %rdi, %rax #lisää 1 counteriin
jmp Division_2
.pos 0x800
stack:
.pos 0x900
array: