.data
prompt: .asciiz "\n Please input an integer value for P: "
result1: .asciiz "The number is prime."
result2: .asciiz "The number is not prime."
.globl main
.text
main:
li $v0, 4 # Print prompt.
la $a0, prompt
syscall
li $v0, 5 # Read P.
syscall
move $t0, $v0 # P in $t0.
addi $t1, $zero, 4
isprime: sub $sp, $sp, $t1 # allocate stack
sw $ra, 0($sp)
bgt $a0, 1 if1 # if (i > 1) branch to if1
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, result1
syscall
j exit
if1: rem $t0, $a0, $a0 # rem = (P % i)
bne $t0, $zero, if2 # if (rem != 0), branch to if2
li $v0, 0
move $a0, $t0
syscall
li $v0, 4
la $a0, result2
syscall
j exit
addi $t2, $zero, 1
if2: sub $a0, $a0, $t2 # i = i - 1
jal isprime
exit: lw $ra, 0($sp) # restore ra
add $sp, $sp, 4 # cleanup stack
jr $ra # return $v0