Computing.Net > Forums > Programming > Prime Number in MIPS help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Prime Number in MIPS help

Reply to Message Icon

Name: oneohthree
Date: October 11, 2005 at 10:34:56 Pacific
OS: na
CPU/Ram: na
Comment:

I am supposed to write a function in MIPS that determines if a number is prime. The function should pass the number to be judged as an argument (through $a0) and return a value to indicate whether the number is a prime or not(through $v0). Use this function in a program that determines and prints all the prime numbers between 1 and 1000. This is supposed to be a recursive function. I wrote it but im not sure if i wrote it recursively.


#I/O console
.data
input_int: .asciiz "The prime numbers between 1 and 1000 are:\n"
newline: .asciiz "\n"
space: .asciiz " "


.text
.align 2
.globl main

main:

la $a0, input_int #print "The prime numbers between 1 and 1000 are:"
li $v0,4
syscall

li $a1,2 #the first prime is 2
li $s2,1000 #Stop searching for primes after 1000

li $t5,0 #zero initialized
li $t2,6 #initialized to 6

li $s0,1

jal primeloop #jumps back
primeloop:

#Find primes
beq $s2,0,exit
sub $s2,$s2,1 #Subtract to keep track as in counter
#Set $t1 to 2
li $t1,2

divide:

div $a1,$t1 #Divides by 2 to get next prime
mflo $t3
slt $v0,$t3,$t1 #if quotient less than divisor stop
beq $v0,$s0,fdprime #determine if prime ($v0=1), if prime prints
#If remainder is zero, it is a composite,not prime
mfhi $t4
beq $t4,0,nprime
#Try next divisor
add $t1,$t1,1
j divide

fdprime:
li $v0,1
move $a0,$a1
syscall
li $v0,4
la $a0,space
syscall
addi $t5,$t5,1
beq $t5,$t2,skip

nprime:
#Advances to the next number
add $a1,$a1,1
jr $ra #goes back to find the primes

skip:

li $v0,4
la $a0,newline
syscall
li $t5,0
j nprime


exit:

oneohthree



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: October 11, 2005 at 13:02:31 Pacific
Reply:

I don't know MIPS, but a recursive program is one which calls itself. So in your situation you should ahve a counter that goes from 1 to 1000. the main function, after it determines wether the current number is prime or not, should do a check to see if the number is less that 1000. If so, the funtion should call itself (and increment the counter). If not, the function should end.

Michael J


0

Response Number 2
Name: oneohthree
Date: October 11, 2005 at 13:39:50 Pacific
Reply:

What I did was I made a counter that decrements from 1000 to 1. And in the divisor loop I go through the divisors. If the quotient is less than the divisor it goes it goes to another loop and increments the number and goes back to the divisor loop. I dont know if it is right

oneohthree


0

Response Number 3
Name: Michael J (by mjdamato)
Date: October 11, 2005 at 14:30:07 Pacific
Reply:

Sounds recursive to me.

Michael J


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Cd burner C++ int statement help



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Prime Number in MIPS help

Find prime numbers in Java? www.computing.net/answers/programming/find-prime-numbers-in-java/11287.html

Prime numbers in oop www.computing.net/answers/programming/prime-numbers-in-oop/17256.html

Prime number checking www.computing.net/answers/programming/prime-number-checking/6158.html