Using x64 assembly, I solved the Largest Prime Factor problem on Project Euler.
The problem is as follows:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
The answer is 6857.
I think I have a top notch algorithm to do the job (speed wise), which doesn't actually check if a number is prime. The algorithm just reduces the number they gave you instead.
p3 proc
mov rbx, 600851475143 ; The original number
mov rcx, 1 ; The starting position of the counter
mov rdx, 0 ; Just to clear garbage data
mainloop:
mov rax, rbx ; rbx hold the number, and saves it, because the number in rax will change, and a backup needs to be stored
inc rcx ; Increment counter
noinc: ; This is where the loop starts at each iteration that the previous division operation had no remainder
mov rbx, rax ; At this instruction, we know that rax has been reduced, and save it into rbx
cmp rax, rcx ; These two lines check if the counter rcx reached its maximum value, and if so, the program quits and returns the result in rax
je done
mov rdx, 0 ; makes sure there is no junk data in rdx
div rcx ; Perform division to check for a remainder
cmp rdx, 0
jz noinc ; If there is no remainder: Reduce the value and dont increment next iteration
jnz mainloop ; If there is a remainder, jump back to the top of the loop, increment the value, and try the division again.
done: ; Exit
ret
p3 endp
I am a beginner in assembly, and this is one of my first x64 assembly programs. Are there any standard conventions that I am not following, or general things that could have been done better? Any advice? Is there a simpler way to do what I did?
Please be brutal and detailed in your response.