-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrimeNum.asm
75 lines (58 loc) · 1.2 KB
/
PrimeNum.asm
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
75
.text
.globl main
main:
# input of the range number
li $v0, 4
la $a0, prompt
syscall
# take n
li $v0, 5
syscall
move $t2, $v0
li $t1, 2
# for loop for printing primes
loop:
# put num in $a0
#checkPrime called with jal
move $a0, $t1
jal checkPrime
#if $v0 is yes print else dont
move $t8, $v0
beq $t8, $zero, continue
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, comma
syscall
continue:
addi $t1, $t1, 1
bgt $t1, $t2, end_loop
j loop
end_loop:
exit:
li $v0, 10
syscall
# function for checking a number prime
# a0 gets number, v0 gets the return yes/no
# without stack
checkPrime:
li $t0, 2
# loop
loopCheck:
rem $t3, $a0, $t0
beq $t3, $zero, not_prime
addi $t0, $t0, 1
beq $t0, $a0, end_loop_yes
j loopCheck
# put 1/0 in $v0 as prime/not prime resp.
not_prime:
li $v0, 0
jr $ra
end_loop_yes:
li $v0, 1
jr $ra
.data
prompt: .asciiz "Enter range number: "
comma: .asciiz " "
endline: .asciiz "\n"