I am having trouble creating a x64 masm program that computes the result of the sum of the odd values. The answer is in RAX as 109E82h (1089154d). I am having difficulty trying to figure out how masm works, but it is so confusing. This is what I have tried so far, but the value in the RAX register is incorrect. I have tried as much as I can, but I do not know where I am going wrong.
extrn ExitProcess: proc
.data
fib1 QWORD 0
fib2 QWORD 1
sum QWORD 0
.code
_main PROC
; calculate the Fibonacci sequence up to 10^7
mov rax, 10000000
mov rcx, fib1
mov rdx, fib2
FIB_LOOP:
add rcx, rdx
mov fib1, rdx
mov fib2, rcx
cmp rcx, rax
jle FIB_LOOP
; add odd numbers to the sum
mov rcx, fib1
add rcx, rdx ; rdx contains the last even Fibonacci number
SUM_LOOP:
test cl, 1 ; check if the current number is odd
jz SKIP_ADD
add sum, rcx
SKIP_ADD:
add rcx, rdx
mov rdx, rcx
cmp rcx, rax
jle SUM_LOOP
; exit the program
mov rax, sum
call ExitProcess
_main ENDP
END
Well, I don't understand why the first part of your program is calculating the Fibonacci sequence up to 10^7, when the title mentions you need to deal with numbers from the range [0,10^6]. That is 10 times higher than what is needed! And after that, the sum loop goes into even bigger numbers, luckily the RAX limit wasn't changed and so that part exits soon.
This could be an interesting alternative (untested):