aboutsummaryrefslogtreecommitdiffstats
path: root/03-Largest-Prime-Factor/asm
diff options
context:
space:
mode:
Diffstat (limited to '03-Largest-Prime-Factor/asm')
-rw-r--r--03-Largest-Prime-Factor/asm/Makefile6
-rwxr-xr-x03-Largest-Prime-Factor/asm/lpfbin0 -> 17232 bytes
-rw-r--r--03-Largest-Prime-Factor/asm/lpf.asm45
-rw-r--r--03-Largest-Prime-Factor/asm/lpf.obin0 -> 2176 bytes
4 files changed, 51 insertions, 0 deletions
diff --git a/03-Largest-Prime-Factor/asm/Makefile b/03-Largest-Prime-Factor/asm/Makefile
new file mode 100644
index 0000000..054301c
--- /dev/null
+++ b/03-Largest-Prime-Factor/asm/Makefile
@@ -0,0 +1,6 @@
+lpf: lpf.o
+ gcc lpf.o -o lpf
+lpf.o: lpf.asm
+ nasm -w+all -f elf64 -g -F stabs lpf.asm
+clean:
+ rm -f lpf lpf.o
diff --git a/03-Largest-Prime-Factor/asm/lpf b/03-Largest-Prime-Factor/asm/lpf
new file mode 100755
index 0000000..aee8383
--- /dev/null
+++ b/03-Largest-Prime-Factor/asm/lpf
Binary files differ
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
+
diff --git a/03-Largest-Prime-Factor/asm/lpf.o b/03-Largest-Prime-Factor/asm/lpf.o
new file mode 100644
index 0000000..df0449d
--- /dev/null
+++ b/03-Largest-Prime-Factor/asm/lpf.o
Binary files differ