-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_7.s
82 lines (69 loc) · 1.54 KB
/
euler_7.s
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
# https://projecteuler.net/problem=7
#
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
# What is the 10 001st prime number?
#
# Solution by Joseph Mullins
# Answer: 104743
#
# compile with gcc euler_7.s
.global main
.text
main:
movq $1, %rcx
movq $0, %rdx
prime_counter:
add $2, %rcx
pushq %rdx
pushq %rcx
call check_power
popq %rcx
popq %rdx
cmp $0, %rax
je prime_counter
incq %rdx
cmp $10000, %rdx
je print
jmp prime_counter
# PURPOSE: Checks prime
# 0 is not a prime, anything else is
.type check_power, @function
check_power:
pushq %rbp
movq %rsp, %rbp
movq 16(%rbp), %rcx # get argument
movq $2, %rbx # Find half of the factor
movq $0, %rdx
movq %rcx, %rax
idivq %rbx
cmpq $0, %rdx # Though if divisible by 2, not a prime
je not_prime
cmpq $2, %rax
jle end_func
decq %rax
movq %rax, %r9
movq $2, %rbx
loop:
movq %rcx, %rax
movq $0, %rdx
idivq %rbx
cmpq $0, %rdx
je not_prime
cmpq %r9, %rbx # if we have got to half of it, then we know it must be prime
je end_func
incq %rbx
jmp loop
not_prime:
movq $0, %rax
end_func: # end of a function, %rax must hold return value before here
movq %rbp, %rsp
popq %rbp
ret
print:
movq $format, %rdi # set first paramter of printf, formatting
movq %rcx, %rsi # set 2nd paramter (the total value)
xorq %rax, %rax # printf is varags
call printf
ret
format:
.asciz "%200d\n"