-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler_3_brute.s
66 lines (55 loc) · 1.69 KB
/
euler_3_brute.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
#
# https://projecteuler.net/problem=3
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
#
# Solution by Joseph Mullins
# Answer: 6857
#
# Compile with gcc euler_3.s
.global main
.text
main:
mov $600851475143, %rcx # The number
mov $0, %rdx
mov %rcx, %rax
mov $2, %rbx
div %rbx # Half is the max the prime factor can be
cmp $0, %rdx # If it was divisible by 2 then we need to subtract 1
jne setup
dec %rax
setup:
mov %rax, %rbx # rbx will hold our current count
push %rcx
loop:
pop %rcx
sub $2, %rbx # Start the countdown to find highest factor
mov %rcx, %rax # set up to see if starting number is divisible by rdx
mov $0, %rdx
div %rbx
cmp $0, %rdx # Check if remainder is 0, if so then check if its a prime
je check_if_prime
push %rcx
jmp loop
check_if_prime:
push %rcx # Save our prime master number
mov $2, %rcx # Start our checking if prime number at 2
prime_loop:
cmp %rcx, %rbx # if rcx got high enough to only be divisible by itself, its prime
je print
mov %rbx, %rax # divide the current number, by 2..n
mov $0, %rdx
div %rcx
cmp $0, %rdx # if remainder is 0, it was divisible by something other then itself
je loop
inc %rcx # increment and keep going
jmp prime_loop
print:
pop %rdx # So everythings off the stack
mov $format, %rdi
mov %rbx, %rsi
xor %rax, %rax
call printf
ret
format:
.asciz "%200d\n"