aboutsummaryrefslogtreecommitdiffstats
path: root/05-Smallest-Multiple
diff options
context:
space:
mode:
authormjfernez <mjfernez@gmail.com>2021-10-12 20:18:28 -0400
committermjfernez <mjfernez@gmail.com>2021-10-12 20:18:28 -0400
commit3cf128d6da667d00bb5bb659413ea55a98a02aff (patch)
tree12eb90997dfa63d65fe6da714626271272342320 /05-Smallest-Multiple
parent009ce017d99a8deb2089438381c273e8f3ea3b67 (diff)
downloadProject_Euler_Solutions-master.tar.gz
Added description. x86 solutions for some problemsHEADmaster
Diffstat (limited to '05-Smallest-Multiple')
-rw-r--r--05-Smallest-Multiple/asm/Makefile6
-rwxr-xr-x05-Smallest-Multiple/asm/smallmultbin0 -> 17272 bytes
-rw-r--r--05-Smallest-Multiple/asm/smallmult.asm44
-rw-r--r--05-Smallest-Multiple/asm/smallmult.obin0 -> 2144 bytes
-rwxr-xr-x[-rw-r--r--]05-Smallest-Multiple/smallmultbin16648 -> 16648 bytes
5 files changed, 50 insertions, 0 deletions
diff --git a/05-Smallest-Multiple/asm/Makefile b/05-Smallest-Multiple/asm/Makefile
new file mode 100644
index 0000000..3ae43fc
--- /dev/null
+++ b/05-Smallest-Multiple/asm/Makefile
@@ -0,0 +1,6 @@
+smallmult: smallmult.o
+ gcc smallmult.o -o smallmult
+smallmult.o: smallmult.asm
+ nasm -w+all -f elf64 -g -F stabs smallmult.asm
+clean:
+ rm -f smallmult smallmult.o
diff --git a/05-Smallest-Multiple/asm/smallmult b/05-Smallest-Multiple/asm/smallmult
new file mode 100755
index 0000000..af65d5a
--- /dev/null
+++ b/05-Smallest-Multiple/asm/smallmult
Binary files differ
diff --git a/05-Smallest-Multiple/asm/smallmult.asm b/05-Smallest-Multiple/asm/smallmult.asm
new file mode 100644
index 0000000..c154ff3
--- /dev/null
+++ b/05-Smallest-Multiple/asm/smallmult.asm
@@ -0,0 +1,44 @@
+; 2520 is the smallest number that can be divided by each of the numbers
+; from 1 to 10 without any remainder.
+; What is the smallest positive number that is evenly divisible by all
+; of the numbers from 1 to 20?
+
+extern printf
+SECTION .data
+flag: db "%d",10,0 ; "%d\n\0"
+
+SECTION .text
+ global main
+ global is_divisible
+
+main:
+ push rbp ; set up stack
+ mov rbp, rsp
+
+ mov rax, 2520 ; value to start at (given)
+ mov rcx, 20 ; divisor (counting down)
+.loop add rax, rcx
+ push rcx
+.isdiv xor rdx, rdx ; clear rdx for div
+ push rax ; save rax before dividing
+ div rcx ; rdx:rax/rcx = rax remainder rdx
+ pop rax ; reload rax to be divided again
+ cmp rdx, 0 ; the number was divisible, keep going
+ loopz .isdiv ; loop exits if rdx is non zero OR rcx is 0
+ cmp rcx, 0 ; if rcx = 0, it means it looped 20 to 1 w/o
+ jz .print ; any remainders, that's our number!
+ ; if you get here, number couldn't be evenly divided by all numbers
+ pop rcx
+ jmp .loop
+
+
+
+.print: mov rdi, flag ; arg 1 (format)
+ mov rsi, rax ; arg 2 (value)
+ mov rax, 0 ; no xmm registers
+ call printf wrt ..plt
+ pop rbp
+
+ mov rax, 0
+ ret
+
diff --git a/05-Smallest-Multiple/asm/smallmult.o b/05-Smallest-Multiple/asm/smallmult.o
new file mode 100644
index 0000000..d2c9fe0
--- /dev/null
+++ b/05-Smallest-Multiple/asm/smallmult.o
Binary files differ
diff --git a/05-Smallest-Multiple/smallmult b/05-Smallest-Multiple/smallmult
index 5f4e692..5f4e692 100644..100755
--- a/05-Smallest-Multiple/smallmult
+++ b/05-Smallest-Multiple/smallmult
Binary files differ