944,007 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Marked Solved
  • Views: 1841
  • Assembly RSS
Jul 1st, 2009
0

Printing prime numbers in MIPS

Expand Post »
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.

Assembly Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
DeadJustice is offline Offline
44 posts
since Sep 2007
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

Hmm. Sorry I dont know mips assembly but on line 12
Assembly Syntax (Toggle Plain Text)
  1. ori $s3, $0, 2
It looks like your resetting something back to 2. Just an educated guess. Good luck!
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
DeadJustice is offline Offline
44 posts
since Sep 2007
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

Hmm.. Switch prime and noprime maybe
Last edited by Hiroshe; Jul 2nd, 2009 at 12:17 am.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

If only the world were that sweet.
Reputation Points: 10
Solved Threads: 0
Light Poster
DeadJustice is offline Offline
44 posts
since Sep 2007
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

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?
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

By the way. You don't seem to initialze the temporary register $t0.

(See loop

gotta run, will check back when I get back!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

$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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: Printing prime numbers in MIPS

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
DeadJustice is offline Offline
44 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: Arithmetic Overflow - MIPS
Next Thread in Assembly Forum Timeline: protected mode switch





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC