aboutsummaryrefslogtreecommitdiffstats
path: root/02-Even-Fibonacci-numbers/asm
diff options
context:
space:
mode:
authormjfernez <mjfernez@gmail.com>2021-10-12 20:18:28 -0400
committermjfernez <mjfernez@gmail.com>2021-10-12 20:18:28 -0400
commit3cf128d6da667d00bb5bb659413ea55a98a02aff (patch)
tree12eb90997dfa63d65fe6da714626271272342320 /02-Even-Fibonacci-numbers/asm
parent009ce017d99a8deb2089438381c273e8f3ea3b67 (diff)
downloadProject_Euler_Solutions-master.tar.gz
Added description. x86 solutions for some problemsHEADmaster
Diffstat (limited to '02-Even-Fibonacci-numbers/asm')
-rw-r--r--02-Even-Fibonacci-numbers/asm/Makefile6
-rwxr-xr-x02-Even-Fibonacci-numbers/asm/fibbin0 -> 17288 bytes
-rw-r--r--02-Even-Fibonacci-numbers/asm/fib.asm43
-rw-r--r--02-Even-Fibonacci-numbers/asm/fib.obin0 -> 2272 bytes
4 files changed, 49 insertions, 0 deletions
diff --git a/02-Even-Fibonacci-numbers/asm/Makefile b/02-Even-Fibonacci-numbers/asm/Makefile
new file mode 100644
index 0000000..479e773
--- /dev/null
+++ b/02-Even-Fibonacci-numbers/asm/Makefile
@@ -0,0 +1,6 @@
+fib: fib.o
+ gcc fib.o -o fib
+fib.o: fib.asm
+ nasm -w+all -f elf64 -g -F stabs fib.asm
+clean:
+ rm -f fib fib.o
diff --git a/02-Even-Fibonacci-numbers/asm/fib b/02-Even-Fibonacci-numbers/asm/fib
new file mode 100755
index 0000000..662eb0a
--- /dev/null
+++ b/02-Even-Fibonacci-numbers/asm/fib
Binary files differ
diff --git a/02-Even-Fibonacci-numbers/asm/fib.asm b/02-Even-Fibonacci-numbers/asm/fib.asm
new file mode 100644
index 0000000..600a542
--- /dev/null
+++ b/02-Even-Fibonacci-numbers/asm/fib.asm
@@ -0,0 +1,43 @@
+; By considering the terms in the Fibonacci sequence whose values do not exceed four million,
+; find the sum of the even-valued terms.
+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
+ mov rax, 1 ; start at fib_1 = 1
+ push rax ; push first term of fib onto stack
+ mov r11, 0 ; sum = 0
+ mov r12, 0x3D0900 ; max = 4000000, this is < 2^64
+ ; so we only need one register
+
+.loop xor rdx, rdx ; clear rdx (remainder)
+ push rax ; push current term on the stack
+ mov rcx, 2 ; divisible by 2
+ div rcx ; divide rdx:rax/rcx
+ cmp rdx, 0 ; compare only remainder
+ jz .sum ; if r = 0 , add to sum
+ jmp .next ; else skip
+
+.sum add r11, QWORD [rsp] ; add latest term to sum register
+
+.next mov rax, QWORD [rsp] ; reload rax to current term
+ add rax, QWORD [rsp+8] ; add last term to current term
+ cmp rax, r12 ; while rax < r12 (4 mil)
+ jl .loop
+
+ mov rdi, flag ; arg 1 (format)
+ mov rsi, r11 ; arg 2 (value)
+ mov rax, 0 ; no xmm registers
+ call printf wrt ..plt
+ mov rsp, rbp ; reset to the top of the stack
+ pop rbp
+
+ mov rax, 0
+ ret
+
diff --git a/02-Even-Fibonacci-numbers/asm/fib.o b/02-Even-Fibonacci-numbers/asm/fib.o
new file mode 100644
index 0000000..2a4eadf
--- /dev/null
+++ b/02-Even-Fibonacci-numbers/asm/fib.o
Binary files differ