Printing prime numbers in MIPS

Thread Solved
Reply

Join Date: Sep 2007
Posts: 36
Reputation: DeadJustice is an unknown quantity at this point 
Solved Threads: 0
DeadJustice DeadJustice is offline Offline
Light Poster

Printing prime numbers in MIPS

 
0
  #1
Jul 1st, 2009
I need to get a MIPS program to print out the first 30 prime numbers and to compute the first 200 (strange, but its the assignment). I can figure out what's wrong with this program. All it does is print 2's out in an infinite loop and I cannot figure out why. We're only supposed to use three procedures (main, testprime, and print) but I'm so bad that I decided to use more. Thank you in advance for helping me.

  1. .text
  2. .globl main
  3.  
  4. main:
  5. # intialize values
  6. ori $s0, $0, 20
  7. ori $s1, $0, 30
  8. ori $s2, $0, 2
  9. ori $s3, $0, 2
  10.  
  11. loop:
  12. ori $s3, $0, 2
  13. beq $s0, $0, exit
  14. beq $t0, $s4, print
  15. j testprime
  16.  
  17. testprime:
  18. beq $s2, $s3, prime
  19. div $s2, $s3
  20. mfhi $t1
  21. beq $t1, $0, noprime
  22. addi $s3, $s3, 1
  23. j testprime
  24.  
  25. noprime:
  26. ori $t0, $0, 0
  27. addi $s2, $s2, 1
  28. j loop
  29.  
  30.  
  31. prime:
  32. ori $t0, $0, 1
  33. sub $s0, $s0, $s4
  34. addi $s2, $s2, 1
  35. j loop
  36.  
  37.  
  38. print:
  39. ori $t0, $0, 0
  40. beq $s1, $0, loop
  41. sub $s1, $s1, $s4
  42. li $v0, 1
  43. or $a0, $0, $s2
  44. syscall
  45. li $v0, 4
  46. la $a0, newline
  47. syscall
  48. j loop
  49.  
  50.  
  51. exit:
  52. li $v0, 10
  53. syscall
  54.  
  55. .data
  56. newline: .asciiz "\n"
Last edited by DeadJustice; Jul 1st, 2009 at 10:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Printing prime numbers in MIPS

 
0
  #2
Jul 2nd, 2009
Hmm. Sorry I dont know mips assembly but on line 12
  1. ori $s3, $0, 2
It looks like your resetting something back to 2. Just an educated guess. Good luck!
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: DeadJustice is an unknown quantity at this point 
Solved Threads: 0
DeadJustice DeadJustice is offline Offline
Light Poster

Re: Printing prime numbers in MIPS

 
0
  #3
Jul 2nd, 2009
Actually it was something stupid, I never initialized $s4 to 1 so the loop never exited. Got the answer from elsewhere, but now my problem is that my output is non-prime numbers which is the opposite of what I want.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Printing prime numbers in MIPS

 
0
  #4
Jul 2nd, 2009
Hmm.. Switch prime and noprime maybe
Last edited by Hiroshe; Jul 2nd, 2009 at 12:17 am.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: DeadJustice is an unknown quantity at this point 
Solved Threads: 0
DeadJustice DeadJustice is offline Offline
Light Poster

Re: Printing prime numbers in MIPS

 
0
  #5
Jul 2nd, 2009
If only the world were that sweet.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Printing prime numbers in MIPS

 
0
  #6
Jul 2nd, 2009
So why aren't you using an empty block of stack and filling it with primes so then you only need to divide by earlier resolved primes instead of the entire set of numbers?

My mind is a bit tired, but as a rule you should always heavily comment your code. Right now would be a good time to do so. Have you tried single stepping your code yet?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Printing prime numbers in MIPS

 
0
  #7
Jul 2nd, 2009
By the way. You don't seem to initialze the temporary register $t0.

(See loop

gotta run, will check back when I get back!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Printing prime numbers in MIPS

 
0
  #8
Jul 2nd, 2009
$t0 and $s4 aren't initialized!
Not sure what you're using $s0 for.
You change the register containing the found prime before printing it. Should copy it into $s4 as you use that as part of your print test.
Not really sure what your logic on prime: is actually doing.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 36
Reputation: DeadJustice is an unknown quantity at this point 
Solved Threads: 0
DeadJustice DeadJustice is offline Offline
Light Poster

Re: Printing prime numbers in MIPS

 
0
  #9
Jul 2nd, 2009
Well I already noted $s4 wasn't initialized. You're right about commenting my code I usually do but I was just tired. I didn't notice about $t0 though.

My logic was sort of checking each number (n) against a counter which started out as 2 then incremented. If n mod counter equaled zero then I jumped out and didn't count it as prime, but somehow I messed up.

I figured out that I was incrementing my answer before the display so that's what was wrong. I even managed to backtrack to fulfill the original condition of my assignment.
Last edited by DeadJustice; Jul 2nd, 2009 at 7:25 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC