Kodi:
# Main program that calls the subroutine isprime and prints the result.
.text # assembly directive that indicates what follows are instructions
.globl main # assembly directive that makes the symbol main global
main: # assembly label
sub $sp,$sp,8 # push stack to save registers needed by the system code that called main
sw $ra,0($sp) # save return address
li $v0,4 # set the code to print_str
la $a0,str1 # load the string to be printed
syscall # print "Please enter a positive integer to be tested: "
li $v0,5 # read from the keyboard the input and store to register $v0
syscall # execute the instruction
add $a1,$v0,$zero # copy the value just entered to the $a1 register
li $v0,4 # set the code to print_str
la $a0,str2 # load the string to be printed
syscall # print "You entered n = "
li $v0,1 # set the code to print_int
add $a0,$a1,$zero # load the int to be printed
syscall # print n
li $v0,4 # set the code to print_str
la $a0,str6 # load the string to be printed
syscall # print "\n"
jal isprime # call subroutine isprime to check if number is prime
sw $v0,4($sp) # result returned in $v0 and stored on the stack
bne $v0,$zero,prime # if( isprime(n) != zero ) then it is a prime
# else it is not a prime
li $v0,4 # set the code to print_str
la $a0,str4 # load the string to be printed
syscall # print "\nIterative isPrime retuned FALSE\n\n"
j exit
prime: li $v0,4 # set the code to print_str
la $a0,str3 # load the string to be printed
syscall # print "\nIterative isPrime retuned TRUE\n\n"
j exit
exit: lw $ra,0($sp) # restore return address used to jump back to system
add $sp,$sp,8 # pop stack to prepare for the return to the system
jr $ra # [jump register] return to the system
.data # Assembly directive indicating what follows is data
str1: .asciiz "Please enter a positive integer to be tested: "
str2: .asciiz "\nYou entered n = "
str3: .asciiz "\nIterative isPrime retuned TRUE\n\n"
str4: .asciiz "\nIterative isPrime retuned FALSE\n\n"
str5: .asciiz "The number entered is less than 2!\n"
str6: .asciiz "\n"
#===============================================================================================
# Function isprime - check if input is a prime number and return True or False
# Inputs:
# int n passed in registers $a1
# Output:
# bool (T/F)(1/0) returned in $v0
# Temporaries: $t0, $t1
.text
isprime:
sub $sp,$sp,12 # push stack to save registers needed by the system code that called main
sw $ra,8($sp) # save return address
sw $a1,4($sp) # save argument n //optional
slti $t0,$a1,2 # test if n < 2
bne $t0,$zero,fail # if n < 2 then exit_failure
li $a0,2 # load 2 in $a0 = range
jal srchfct # call subroutine srchfct(2, n);
# invert the answer: $v0 = $v0 + 1 - ( 2 * $v0 )
mul $t0,$v0,$a0 # if $v0==0 then $t0=0; if $v0==1 then $t0=2
addi $t1,$v0,1 # if $v0==0 then $t1=1; if $v0==1 then $t1=2
sub $v0,$t1,$t0 # if $v0==0 then $v0=1; if $v0==1 then $v0=0
sw $v0,0($sp) # result returned in $v0 and stored on the stack
lw $a1,4($sp) # restore argument n //optional
lw $ra,8($sp) # restore return address used to jump back to system
add $sp,$sp,12 # pop stack to prepare for the return to the system
jr $ra # return to calling procedure
fail: li $v0,4 # set the code to print_str
la $a0,str5 # load the string to be printed
syscall # print "The number entered is less than 2!\n"
li $v0,10 # set the code to exit
syscall # print "\nIterative isPrime retuned FALSE\n\n"
#===============================================================================================
# Function srchfct - Recursive search for factors
# Input:
# Positive integer n >=2
# Positive integer range
# inputs stored in $a0, $a1
# Output:
# bool (T/F)(1/0) returned in $v0
# true if exists an integer in [range, n-1] that is a factor of n (i.e., n is not prime)
# false if there is no such integer.
# Temporaries: $t0
.text
srchfct:
sub $sp,$sp,12 # Push stack to create room for 3 items
sw $ra,8($sp) # save the return address
sw $a1,4($sp) # save the argument n
sw $a0,0($sp) # save the argument range
sge $t0,$a0,$a1 # test if range >= n
bne $t0,$zero,false # if range >= n then return false
rem $t0,$a1,$a0 # $t0 = n % range
beq $t0,$zero,true # if ( a % n ) == 0 then return true
# else
addi $a0,$a0,1 # range = range + 1
jal srchfct # srchfct(range+1, n)
exits: lw $a0,0($sp) # restore the argument range
lw $a1,4($sp) # restore the argument n
lw $ra,8($sp) # restore the return address
add $sp,$sp,12 # pop 3 items from stack
jr $ra # return to calling procedure
true: li $v0,1 # return true
j exits
false: li $v0,0 # return false
j exits
Krijoni Kontakt