-
Notifications
You must be signed in to change notification settings - Fork 0
/
007.asm
49 lines (41 loc) · 1.58 KB
/
007.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
primes times 14375 db 255 ;N * (ln N + ln(ln N)) bits, rounded up
section .text
extern printf
global main
main:
mov ebx, 1 ;array index for outer loop
xor ecx, ecx ;counter
sieve_outer:
inc ebx ;increase index
mov eax, ebx ;copy to eax for squaring
mul ebx ;square
cmp eax, 115000 ;check if square is > limit
jg find10001st ;if it is, sieve is done
bt [primes], ebx ;check if ebx is no prime
jnc sieve_outer ;if no prime, try next number
inc ecx ;else increase counter
sieve_inner:
btr [primes], eax ;set multiple to not prime
add eax, ebx ;next multiple
cmp eax, 115000 ;check if multiple is <= limit
jl sieve_inner ;if it is, continue with inner loop
jmp sieve_outer ;if not, continue with outer loop
find10001st:
inc ebx ;increase array index
bt [primes], ebx ;is it prime?
adc ecx,0 ;if yes, increase ecx
cmp ecx, 10001 ;check if counter arrived at 10001
jl find10001st ;if not, continue
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
mov esi, ebx
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc