-
Notifications
You must be signed in to change notification settings - Fork 0
/
049.asm
112 lines (98 loc) · 3.87 KB
/
049.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
section .data
msg db "%lld", 10, 0 ;return string for printf (just the result)
primes times 1250 db 255 ;array for the prime sieve
digits times 30 db 0 ;digits for permutation check
section .text
extern printf
global main
main:
mov ebx, 1 ;array index for outer loop
sieve_outer:
inc ebx ;increase index
mov eax, ebx ;copy to eax for squaring
mul ebx ;square
cmp eax, 10000 ;check if square is > limit
jg reset ;if it is, jump to reset
bt [primes], ebx ;check if ebx is no prime
jnc sieve_outer ;if no prime, try next number
sieve_inner:
btr [primes], eax ;set multiple to not prime
add eax, ebx ;next multiple
cmp eax, 10000 ;check if multiple is <= limit
jl sieve_inner ;if it is, continue with inner loop
jmp sieve_outer ;if not, continue with outer loop
reset:
mov eax, 1487
btr [primes], eax ;primes[1487] = 0 to exclude the example
mov rax, 1000 ;init rax
xor rbx, rbx ;and clear registers
xor rcx, rcx
xor rdi, rdi
nextstarter:
inc eax ;next starting number
bt [primes], eax ;is number prime?
jnc nextstarter ;if not, try next
mov ebx, eax ;else set ebx
nextfollowers:
inc ebx ;next second number
bt [primes], ebx ;is it prime?
jnc nextfollowers ;if not, try next
mov ecx, ebx ;else put it in ecx
sub ecx, eax ;and subtract eax
add ecx, ebx ;add ebx to difference
cmp ecx, 10000 ;result > 9999?
jge nextstarter ;if yes, try next starter
bt [primes], ecx ;is third number prime?
jnc nextfollowers ;if not, try next second number
mov r8d, eax ;copy of eax to save it
xor esi, esi ;reset esi
getdigits:
mov byte [digits + esi], 0 ;reset digits[edi]
inc esi ;next index
cmp esi, 30 ;end of array?
jl getdigits ;if not, continue
mov esi, 10 ;10 in esi for division in divide
mov r9d, digits ;put digits' adress in r9d
call divide ;call divide function
mov eax, ebx ;put ebx in eax for divide
lea r9d, [digits + 10] ;load address of digits[10] in r9d
call divide ;call divide again
mov eax, ecx ;repeat for edi
lea r9d, [digits + 20]
call divide
mov eax, r8d ;restore eax
xor esi, esi ;reset esi and ecx for permcheck
xor edi, edi
permcheck:
mov dil, [digits + esi] ;get digits[esi]
cmp byte [digits + esi + 10], dil ;must be same as digits[esi + 10]
jne nextfollowers ;if not, try next followers
cmp byte [digits + esi + 20], dil ;same for digits[esi + 20]
jne nextfollowers
inc esi ;next index
cmp esi, 10 ;finished?
jl permcheck ;if not, repeat
result:
xor rsi, rsi ;build the 12-digit concatenated number
mov rsi, rax
imul rsi, 100000000
imul rbx, 10000
add rsi, rbx
add rsi, rcx
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
divide:
xor edx, edx
div esi
mov byte [r9d + edx], 1
test eax, eax
jnz divide
ret
section .note.GNU-stack ;just for gcc