-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path035.asm
81 lines (68 loc) · 2.62 KB
/
035.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
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:
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, 1000000 ;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, 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: ;set up registers for next operation
mov edi, 1 ;index for prime
mov esi, 10 ;for divisions
xor ecx, ecx ;counter
nextprime:
inc edi ;next number
cmp edi, 1000000 ;finished?
jge print ;if yes, print result
bt [primes], edi ;is edi prime?
jnc nextprime ;if not, try next number
cmp edi, 11 ;prime <= 11
jle circular ;if yes, go to circular
mov ebx, 1 ;1 in ebx for the number of digits
mov eax, edi ;prime in eax for division
getlength:
xor edx, edx ;reset remainder
div esi ;divide by 10
test eax, eax ;reduced to 0?
jz prepare ;if yes, jump to prepare
imul ebx, esi ;else multiply esi by 10
jmp getlength ;and repeat
prepare:
mov eax, edi ;prime in eax
rotate:
xor edx, edx ;reset remainder
div esi ;divide by 10
imul edx, ebx ;multiply remainder by ebx
add eax, edx ;add edx --> rotation
bt [primes], eax ;is rotation prime?
jnc nextprime ;if not, jump to nextprime
cmp eax, edi ;rotation = original?
jne rotate ;if not, continue rotating
circular:
inc ecx
jmp nextprime
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