summaryrefslogtreecommitdiff
path: root/input
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2025-04-28 23:36:11 -0400
committerbd <bdunahu@operationnull.com>2025-04-28 23:36:11 -0400
commit34fe1c946687d8ff89b1596625f28dc40e12277a (patch)
tree434c60c3a1d595a6395fa8367a5dcc3c03495c2c /input
parent6bdd0cbb4ed723ab4595f94089d9cf9c770db3ec (diff)
Add primes generator
Diffstat (limited to 'input')
-rw-r--r--input/primes-generator.asm74
1 files changed, 74 insertions, 0 deletions
diff --git a/input/primes-generator.asm b/input/primes-generator.asm
new file mode 100644
index 0000000..f772fc3
--- /dev/null
+++ b/input/primes-generator.asm
@@ -0,0 +1,74 @@
+;;;;;;;;
+;;; discovers factors from 0-N through use of the trial division algorithm.
+;;;
+;;; I didn't write a square root function, so this is worse than algorithms
+;;; you'll find online. That may be better. :)
+
+.data
+ N 5000
+ primes 0
+.text
+ addi $5 $2 0x0 ; establish frame pointer
+ addi $6 $0 0x0 ; establish return argument
+ jmp MAIN
+
+TRIAL:
+ push $5 ;function preamble
+ addi $5 $2 0x0
+ subi $2 $2 0x0
+
+ load $7 +1($5) ; N (argument)
+ addi $8 $0 0x2 ; the initial divisor
+ addi $6 $0 0x0 ; the return value
+
+ jrl TCOND
+TLOOP:
+ rem $9 $7 $8 ; check if divisible
+ cmp $9 $0
+ beq END
+
+ addi $8 $8 0x1
+TCOND:
+ cmp $7 $8
+ bgt TLOOP
+
+ addi $6 $0 0x1 ; return 1 (signal parameter is prime)
+END:
+ addi $2 $5 0x0 ; function postamble
+ pop $5
+ ret
+
+MAIN:
+ push $5 ; function preamble
+ addi $5 $2 0x0
+ subi $2 $2 0x3
+
+ load $7 N($0) ; N
+ addi $8 $0 0x2 ; number to test
+ addi $9 $0 0x0 ; index to store
+
+GEN:
+ store $9 -1($5) ; caller-saved registers
+ store $8 -2($5)
+ store $7 -3($5)
+
+ push $8 ; push arg
+ jal TRIAL
+ addi $2 $2 0x1
+
+ load $9 -1($5) ; restore registers
+ load $8 -2($5)
+ load $7 -3($5)
+
+ cmp $6 $0 ; check if return is 0
+ beq COMPOSITE
+
+ store $8 primes($9) ; store prime
+ addi $9 $9 0x1
+COMPOSITE:
+ addi $8 $8 0x1
+ cmp $7 $8
+ bgt GEN
+ nop
+ nop
+ nop