DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Assembly (http://www.daniweb.com/forums/forum125.html)
-   -   Printing prime numbers in MIPS (http://www.daniweb.com/forums/thread201077.html)

DeadJustice Jul 1st, 2009 10:23 pm
Printing prime numbers in MIPS
 
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.

   .text
  .globl main

main:
        # intialize values
        ori $s0, $0, 20
        ori $s1, $0, 30
        ori $s2, $0, 2
        ori $s3, $0, 2

loop:
        ori $s3, $0, 2       
        beq $s0, $0, exit
        beq $t0, $s4, print
        j testprime

testprime:
        beq $s2, $s3, prime
        div $s2, $s3
        mfhi $t1
        beq $t1, $0, noprime
        addi $s3, $s3, 1
        j testprime

noprime:
        ori $t0, $0, 0
        addi $s2, $s2, 1
        j loop


prime:
        ori $t0, $0, 1
        sub $s0, $s0, $s4
        addi $s2, $s2, 1
        j loop       
       

print:
        ori $t0, $0, 0
        beq $s1, $0, loop
        sub $s1, $s1, $s4
        li $v0, 1
        or $a0, $0, $s2
        syscall
        li $v0, 4
        la $a0, newline
        syscall
        j loop
       

exit:
        li $v0, 10
        syscall

.data
newline: .asciiz "\n"

Hiroshe Jul 2nd, 2009 12:07 am
Re: Printing prime numbers in MIPS
 
Hmm. Sorry I dont know mips assembly but on line 12
ori $s3, $0, 2
It looks like your resetting something back to 2. Just an educated guess.;) Good luck!

DeadJustice Jul 2nd, 2009 12:09 am
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.

Hiroshe Jul 2nd, 2009 12:16 am
Re: Printing prime numbers in MIPS
 
Hmm.. Switch prime and noprime maybe :)

DeadJustice Jul 2nd, 2009 12:40 am
Re: Printing prime numbers in MIPS
 
If only the world were that sweet.

wildgoose Jul 2nd, 2009 12:49 am
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?

wildgoose Jul 2nd, 2009 1:01 am
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!

wildgoose Jul 2nd, 2009 2:08 am
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.

DeadJustice Jul 2nd, 2009 7:09 am
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.


All times are GMT -4. The time now is 9:10 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC