From 3cf128d6da667d00bb5bb659413ea55a98a02aff Mon Sep 17 00:00:00 2001 From: mjfernez Date: Tue, 12 Oct 2021 20:18:28 -0400 Subject: Added description. x86 solutions for some problems --- 03-Largest-Prime-Factor/asm/lpf.asm | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 03-Largest-Prime-Factor/asm/lpf.asm (limited to '03-Largest-Prime-Factor/asm/lpf.asm') 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 + -- cgit v1.2.3