-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path037.asm
92 lines (80 loc) · 3.17 KB
/
037.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
primes times 125000 db 255 ;array for the prime sieve
section .text
extern printf
global main
main:
xor ebx, ebx ;array index for outer loop
btr [primes], ebx ;set 0 and 1 to not prime
inc ebx
btr [primes], ebx
sieve_outer:
inc ebx ;increase index
mov eax, ebx ;copy to eax for squaring
mul ebx ;square
cmp eax, 1000000 ;check if square is > limit
jg reset ;if it is, jump to reset
bt [primes], ebx ;check if ebx is not prime
jnc sieve_outer ;if not prime, try next number
sieve_inner:
btr [primes], eax ;set multiple to not prime
add eax, ebx ;next multiple
cmp eax, 1000000 ;check if square is <= limit
jl sieve_inner ;if it is, continue with inner loop
jmp sieve_outer ;if not, continue with outer loop
reset:
mov ebx, 10 ;10 in ebx (primes < 10 not considered)
mov edi, 10 ;10 in edi for divisions
xor ecx, ecx ;sum
xor r8d, r8d ;counter
nextprime:
inc ebx ;next number
bt [primes], ebx ;is it prime?
jnc nextprime ;if not, find next prime
mov eax, ebx ;prime in eax for next step
trunc_right:
xor edx, edx ;reset remainder
div edi ;divide by 10
bt [primes], eax ;is truncated number prime?
jnc nextprime ;if not, try next prime
cmp eax, 10 ;number still >= 10?
jge trunc_right ;if yes, repeat
mov esi, 1000000 ;1000000 in esi for division
mov eax, esi ;copy to eax for next step
setdivisor:
xor edx, edx ;reset remainder
div edi ;divide by 10
cmp eax, ebx ;eax < number?
jg setdivisor ;if not, repeat
mov esi, eax ;else set esi
mov eax, ebx ;prime in eax
trunc_left:
xor edx, edx ;reset remainder
div esi ;divide by esi
mov eax, edx ;remainder in eax
bt [primes], eax ;is remainder prime?
jnc nextprime ;if not try next prime
push rax ;result on the stack
mov eax, esi ;esi in eax
xor edx, edx ;reset remainder
div edi ;divide by 10
mov esi, eax ;result back in esi
pop rax ;number back from the stack
cmp eax, 10 ;number still >= 10?
jge trunc_left ;if yes, repeat
add ecx, ebx ;else add prime to sum
inc r8d ;increase counter
cmp r8d, 11 ;are all 11 candidates found?
jl nextprime ;if not, try next prime
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
mov esi, ecx
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc