diff options
author | mjfernez <mjfernez@gmail.com> | 2021-10-12 20:18:28 -0400 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2021-10-12 20:18:28 -0400 |
commit | 3cf128d6da667d00bb5bb659413ea55a98a02aff (patch) | |
tree | 12eb90997dfa63d65fe6da714626271272342320 /01-Multiples-of-3-and-5 | |
parent | 009ce017d99a8deb2089438381c273e8f3ea3b67 (diff) | |
download | Project_Euler_Solutions-3cf128d6da667d00bb5bb659413ea55a98a02aff.tar.gz |
Diffstat (limited to '01-Multiples-of-3-and-5')
-rw-r--r-- | 01-Multiples-of-3-and-5/asm/Makefile | 6 | ||||
-rwxr-xr-x | 01-Multiples-of-3-and-5/asm/euler1 | bin | 0 -> 17336 bytes | |||
-rw-r--r-- | 01-Multiples-of-3-and-5/asm/euler1.asm | 42 | ||||
-rw-r--r-- | 01-Multiples-of-3-and-5/asm/euler1.o | bin | 0 -> 2384 bytes | |||
-rwxr-xr-x[-rw-r--r--] | 01-Multiples-of-3-and-5/euler1 | bin | 8608 -> 16608 bytes | |||
-rw-r--r-- | 01-Multiples-of-3-and-5/euler1.c | 10 |
6 files changed, 53 insertions, 5 deletions
diff --git a/01-Multiples-of-3-and-5/asm/Makefile b/01-Multiples-of-3-and-5/asm/Makefile new file mode 100644 index 0000000..c34d1a2 --- /dev/null +++ b/01-Multiples-of-3-and-5/asm/Makefile @@ -0,0 +1,6 @@ +euler1: euler1.o + gcc euler1.o -o euler1 +euler1.o: euler1.asm + nasm -w+all -f elf64 -g -F stabs euler1.asm +clean: + rm -f euler1 euler1.o diff --git a/01-Multiples-of-3-and-5/asm/euler1 b/01-Multiples-of-3-and-5/asm/euler1 Binary files differnew file mode 100755 index 0000000..278476a --- /dev/null +++ b/01-Multiples-of-3-and-5/asm/euler1 diff --git a/01-Multiples-of-3-and-5/asm/euler1.asm b/01-Multiples-of-3-and-5/asm/euler1.asm new file mode 100644 index 0000000..9541295 --- /dev/null +++ b/01-Multiples-of-3-and-5/asm/euler1.asm @@ -0,0 +1,42 @@ +extern printf +SECTION .data +flag: db "%d",10,0 ; "%d\n\0" + +SECTION .text + global main + +main: + push rbp ; set up stack + mov rbp, rsp + xor rax, rax ; start at multiple (i = 0) + mov r11, 0 ; sum = 0 + mov r12, 1000 ; max = 1000 +.loop xor rdx, rdx ; clear rdx (remainder) + push rax ; save rax + mov rcx, 5 ; divisible by 5 + div rcx ; divide rdx:rax/rcx + cmp rdx, 0 ; compare only remainder + jz .sum ; if r = 0 , add to sum + xor rdx,rdx ; clear rdx + mov rax, QWORD [rsp] ; reload rax + mov rcx, 3 ; divisible by 3 + div rcx ; divide rdx:rax/rcx + cmp rdx, 0 ; compare only remainder + jnz .next ; if r = 0 , add to sum + +.sum add r11, QWORD [rsp] ; add to sum register + +.next pop rax ; return rax value from stack to be inc + inc rax ; i++ + cmp rax, r12 ; while rax < r12 (1000) + jl .loop ; repeat + + mov rdi, flag ; arg 1 (format) + mov rsi, r11 ; arg 2 (value) + mov rax, 0 ; no xmm registers + call printf wrt ..plt + pop rbp + + mov rax, 0 + ret + diff --git a/01-Multiples-of-3-and-5/asm/euler1.o b/01-Multiples-of-3-and-5/asm/euler1.o Binary files differnew file mode 100644 index 0000000..b45c7c1 --- /dev/null +++ b/01-Multiples-of-3-and-5/asm/euler1.o diff --git a/01-Multiples-of-3-and-5/euler1 b/01-Multiples-of-3-and-5/euler1 Binary files differindex 585d380..93718c8 100644..100755 --- a/01-Multiples-of-3-and-5/euler1 +++ b/01-Multiples-of-3-and-5/euler1 diff --git a/01-Multiples-of-3-and-5/euler1.c b/01-Multiples-of-3-and-5/euler1.c index d89956e..ff24a08 100644 --- a/01-Multiples-of-3-and-5/euler1.c +++ b/01-Multiples-of-3-and-5/euler1.c @@ -3,11 +3,11 @@ //Problem 1: multiples of 3 and 5 int main(){ int max = 1000; - int i; double sum; + int i, sum = 0; - for (i=0;i<max; i++) - if(i%3 == 0 || i%5==0) - sum+= (double) i; + for (i; i<max; i++) + if (!(i%3) || !(i%5)) + sum+=i; - printf("%f\n", sum); + printf("%d\n", sum); } |