-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_1.s
60 lines (47 loc) · 1.66 KB
/
euler_1.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
# https://projecteuler.net/problem=1
# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
# The sum of these multiples is 23.
#
# Find the sum of all the multiples of 3 or 5 below 1000.
#
# Solution by Joseph Mullins,
# Answer: 233168
#
# Compile with gcc euler_1.s
.global main
.text
main:
mov $1000, %rcx # rcx will countdown to 0
xor %rax, %rax # rax will hold current number
dec %rcx # we want everything below this
loop:
push %rax # we want to store its current value so its not lost below
mov $0, %rdx
mov %rcx, %rax # Move current counter to rcx
mov $3, %rbx # Move 3 into rbx
div %rbx # divide it by 3, rdx will hold the remainder
cmp $0, %rdx # if rcx was divisible by 5, then rdx will be 0
je add_to_total # add this if compare passed
mov $0, %rdx
mov %rcx, %rax
mov $5, %rbx # check if divisible by 5
div %rbx # divide it by 3, rdx will hold the remainder
cmp $0, %rdx
je add_to_total # add this if compare passed
pop %rax
dec %rcx # reduce rcx by 1
jnz loop # if not 0 then repeat
jmp print # print the output
add_to_total:
pop %rax # retrieve what eax was
add %rcx, %rax # add current value of rcx to it
dec %rcx # reduce rcx by 1
jmp loop # rinse and repeat
print:
mov $format, %rdi # set first paramter of printf, formatting
mov %rax, %rsi # set 2nd paramter (the total value)
xor %rax, %rax # printf is varags
call printf
ret
format:
.asciz "%200d\n"