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 /02-Even-Fibonacci-numbers/asm/fib.asm | |
parent | 009ce017d99a8deb2089438381c273e8f3ea3b67 (diff) | |
download | Project_Euler_Solutions-3cf128d6da667d00bb5bb659413ea55a98a02aff.tar.gz |
Diffstat (limited to '02-Even-Fibonacci-numbers/asm/fib.asm')
-rw-r--r-- | 02-Even-Fibonacci-numbers/asm/fib.asm | 43 |
1 files changed, 43 insertions, 0 deletions
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 + |