blob: 2fac25f95b4ffd5f08e63582a36ea48797837b06 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
|