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 /03-Largest-Prime-Factor/asm/lpf.asm | |
parent | 009ce017d99a8deb2089438381c273e8f3ea3b67 (diff) | |
download | Project_Euler_Solutions-3cf128d6da667d00bb5bb659413ea55a98a02aff.tar.gz |
Diffstat (limited to '03-Largest-Prime-Factor/asm/lpf.asm')
-rw-r--r-- | 03-Largest-Prime-Factor/asm/lpf.asm | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/03-Largest-Prime-Factor/asm/lpf.asm b/03-Largest-Prime-Factor/asm/lpf.asm new file mode 100644 index 0000000..3e67ed3 --- /dev/null +++ b/03-Largest-Prime-Factor/asm/lpf.asm @@ -0,0 +1,45 @@ +; What is the largest prime factor of the number 600851475143 ? +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 rcx, 2 ; smallest prime factor: 2 + mov rax, 0x8BE589EAC7 ; target = 600851475143 + +.loop xor rdx, rdx ; clear rdx (remainder) + push rax + div rcx ; divide rdx:rax/rcx + cmp rdx, 0 ; compare only remainder + jnz .next ; if r = 0 , its a factor + ; we already divided the number so + ; we just need to save the value in rax + ; as the new number to factorize + add rsp, 8 ; delete the old + push rax + mov rcx, 1 ; reset the counter (normally would start at 2, + ; but we increment anyway +.next pop rax ; refresh value + inc rcx ; factor ++ + cmp rax, rcx ; while rax > rcx number > factor + ; i.e. not a prime factor + jg .loop + + mov rdi, flag ; arg 1 (format) + mov rsi, rcx ; 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 + |