-
Notifications
You must be signed in to change notification settings - Fork 0
/
027.asm
85 lines (73 loc) · 2.94 KB
/
027.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
primes times 2000 db 1 ;array for the prime sieve
section .text
extern printf
global main
main:
mov byte [primes], 0 ;set primes[0] = 0
mov byte [primes + 1], 0 ;set primes[1] = 0
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, 2000 ;check if square is > limit
jg reset ;if it is, jump to reset
cmp byte [primes + ebx], 0 ;check if ebx is no prime
je sieve_outer ;if no prime, try next number
sieve_inner:
mov byte [primes + eax], 0 ;set multiple to not prime
add eax, ebx ;next multiple
cmp eax, 2000 ;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: ;reset registers for next operation
mov ecx, 1 ;b
xor edi, edi ;max n
xor esi, esi ;max a * b
incB:
inc ecx ;next b
cmp byte [primes + ecx], 1 ;is b prime?
jne incB ;if not, try next
cmp ecx, 1000 ;limit reached?
jge print ;if yes, print result
mov ebx, 1001 ;set a to -1001
neg ebx
incA:
add ebx, 2 ;next a
cmp ebx, 1000 ;a > 1000
jg incB ;if yes, try next b
xor eax, eax ;reset n
countprimes:
push rax ;n on the stack
push rax ;twice
mul eax ;n^2
add eax, ecx ;n^2 + b
mov r8d, eax ;copy result
pop rax ;n back from the stack
mul ebx ;n * a
add eax, r8d ;n^2 + (a * n) + b
cmp byte [primes + eax], 0 ;is result prime?
pop rax ;get n back
je updatemax ;if not, updatemax
inc eax ;else increase n
jmp countprimes ;and continue
updatemax:
cmp eax, edi ;n > max n
jl incA ;if not, back to next A
mov edi, eax ;else update max
mov eax, ebx ;a in eax
mul ecx ;a * b
mov esi, eax ;result in esi
jmp incA ;and back to incA
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
section .note.GNU-stack ;just for gcc