Urgent help in MIPS

hi guys,
I have problems in project: " Generate N prime numbers" where N is enterd by user

the roll of this project is that

#
# This program is used to get N prime number(s) whrer N is enterd by user
# then, stored these prime numbers in an array and print this array elements
#

1. when user type 1 the program looping with no printing
2. when type : e.g. 2 ==> print first of array then the number user typed (2)
                      e.g. 5 ==> print first of array then the number user typed (5) & three numbers (address as i think)

I need a fast replay please
LVL 1
IbrahimsairafiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IbrahimsairafiAuthor Commented:
Hi,
 I just modify the code

but I face another problem :
                                        the printed result remains 3 i.e. only three out

this my code (after modification):

#
# This program is used to get N prime number(s) whrer N is enterd by user
# then, stored these prime numbers in an array and print this array elements
#

#
# This program is broken into 4 sections:
#      a. initilize needed registers (variables)
#      b. user input procedure
#      c. prime procedure
#      d. printing result
#

##
# Initialization
######

#
# Data segments
#
      .data

prime_arr:      .word 0                        #array stores primes
input:      .word 0                        #array stores user intput
nl:           .asciiz "\n"                        # used for printing new line
qs:      .asciiz "How many prime is needed: "      # begining question
ctap:           .asciiz ",\t"                        # tapped comma
answ:      .asciiz "Generated prime number(s):  "      # answering question
no_pr:      .asciiz "Program terminated\nNo prime !!\n"

#
# Text segments
#
      .text
      
main:  
        # First prime is 2.
            li $s0, 2
            la $t0, prime_arr
            sw $s0, 0($t0)

##
# User Input
######

        # Stop searching for primes after this number.
                  li $v0, 4
                  la $a0, qs
                  syscall
                  
                  li $v0, 5
                  syscall
            
            # store user input into user input array then load it into $s1 reg.
                  sw $v0, input
                  lw $t1, input
                  
                  beq $t1, $0, end
                  
##
# Prime Procedure
######
      
                  li $s1, 2
                  li $s2, 1                  #counter
                  li $t7, 1                  # check counter
                  #addi $t1, $t1, -1
                  
prime_proc:            la $t0, prime_arr
again:                            lw $t2, 0($t0)
                  addi $t0, $t0, 4
                  
                  divu $s1, $t2
                  
                  mfhi $t3
                  beq $t3, $0, move_sild
                  
                  blt $t7, $s2, check_next
                  
                  sw $s1, 0($t0)      # load prime to array
                  addi $s2, $s2, 1      # increment counter
                  addi $s1, $s1, 1      # go to next number
                  addi $t1, $t1, -1      # decrement prime needed
                  li $t7, 1            # set check counter to zero
                  
                  bnez $t1, prime_proc
                  
                  
##
# Printing Result
######
            
            lw $t1, input
                  
sprint:            la $t0, prime_arr
print_prime:      lw $s0, 0($t0)
            addi $t0, $t0, 4
                  
            li $v0, 1
            move $a0, $s0
            syscall
                  
            addi $t1, $t1, -1
            bnez $t1, print_tap
                  
            #print new line
            li $v0, 4
            la $a0, nl
            syscall

            # end
            li $v0, 10
            syscall

#                                                      #
####                                                         ####
#################################################################
#################################################################
####                                                         ####
#                                                      #

##
# cont. of prime procedure
######

check_next:      addi $t7, $t7, 1
            j again
                  
move_sild:      addi $s1, $s1, 1
            j prime_proc

##
# cont. of print procedure
######
print_tap:
            #print new line
            li $v0, 4
            la $a0, ctap
            syscall
            j print_prime
##
# cont. of init.
######
end:
            #print new line
            li $v0, 4
            la $a0, no_pr
            syscall
                  
            # end
            li $v0, 10
            syscall

where is my mistake ??

0
PaulCaswellCommented:
Hi Ibrahimsairafi,

>>prime_arr:     .word 0                    #array stores primes
I think you probably dont want your prime array containing only one word. Try something like:
prime_arr:     .space 100                    #array stores primes

That wont necessarily fix your problem but it certainly will be safer that way. Note that I've only left room for 100 bytes or 25 primes, you may find a larger number better.

Paul
0
IbrahimsairafiAuthor Commented:
thanx paul,

I modify my code after a discussion with instructor he told my to make max N is 10,000
--> prime_arr: .word 0:10000
--> ble $t7, $s2, check_next

it gives my nice thing but with prime ignoration

e.g : how many prime: 10
       generated primes:
       2, 0, 3, 11, 7, 17, 13, 23, 19, 29

ignore:  5
the result unsorted

the procedure i implement should print it out sorted

this is the new code:
===================================
#
# Team name: HIDS
# Team members: 1. Hani Al-Dar (215913)      2.Ibrahim Al-Sirafi (216015)
# Section: 4
# Prepared for Dr. Mudawar, M - Project 1
#

#
# This program is used to get N prime number(s) whrer N is enterd by user
# then, stored these prime numbers in an array and print this array elements
#

#
# This program is broken into 4 sections:
#      a. initilize needed registers (variables)
#      b. user input procedure
#      c. prime procedure
#      d. printing result
#

##
# Initialization
######

#
# Data segments
#
      .data

prime_arr:      .word 0:10000                              #array stores primes
input:      .word 0:1                                    #array stores user intput
nl:           .asciiz "\n"                              # used for printing new line
qs:            .asciiz "How many prime is needed: "      # begining question
ctap:           .asciiz ",\t"                              # tapped comma
answ:            .asciiz "Generated prime number(s):\n"      # answering question
no_pr:      .asciiz "Program terminated\nNo prime !!\n"

#
# Text segments
#
      .text
      
main:  
        # First prime is 2.
                  li $s0, 2
                  la $t0, prime_arr
                  sw $s0, ($t0)

##
# User Input
######

      # Stop searching for primes after this number.
                  li $v0, 4
                  la $a0, qs
                  syscall
                  
                  li $v0, 5
                  syscall
            
      # store user input into user input array then load it into $s1 reg.
                  #sw $v0, input
                  move $s3, $v0
                  add $t1, $0, $s3
                  beq $t1, $0, end
##
# Prime Procedure
######
      
                  li $s1, 3
                  li $s2, 1                  #counter
                  li $t7, 1                  # check counter
                  
prime_proc:            la $t0, prime_arr
again:            lw $t2, ($t0)
                  addi $t0, $t0, 4
                  
                  div $s1, $t2
                  
                  mfhi $t3
                  beq $t3, $0, move_sild
                  
                  ble $t7, $s2, check_next
                  
                  sw $s1, ($t0)      # load prime to array
                  add $s2, $s2, 1      # increment counter
                  add $s1, $s1, 1      # go to next number
                  addi $t1, $t1, -1      # decrement prime needed
                  li $t7, 1            # set check counter to zero
                  
                  bnez $t1, prime_proc
                  
                  
##
# Printing Result
######
            
            # print
                  #lw $t1, input
                  add $t1, $0, $s3

            # print answer line
                  li $v0, 4
                  la $a0, answ
                  syscall
                  
sprint:            la $t0, prime_arr
print_prime:      lw $s0, 0($t0)
                  add $t0, $t0, 4
                  
                  li $v0, 1
                  move $a0, $s0
                  syscall
                  
                  addi $t1, $t1, -1
                  bnez $t1, print_tap
                  
                  #print new line
                  li $v0, 4
                  la $a0, nl
                  syscall

            # end
                  li $v0, 10
                  syscall

#                                                                #
####                                                             ####
#################################################################
#################################################################
####                                                             ####
#                                                                #

##
# cont. of prime procedure
######

check_next:      add $t7, $t7, 1
            j again
                  
move_sild:      add $s1, $s1, 1
            j prime_proc

##
# cont. of print procedure
######
print_tap:
            #print new line
            li $v0, 4
            la $a0, ctap
            syscall
            j print_prime
##
# cont. of init.
######
end:
            #print new line
                  li $v0, 4
                  la $a0, no_pr
                  syscall
                  
            # end
                  li $v0, 10
                  syscall
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

PaulCaswellCommented:
I'm trying to find a MIPS emulator that can run under Windows. Do you know of any? I think tracing through your program would be the easiest way to find bugs.

Paul
0
IbrahimsairafiAuthor Commented:
I use SPIM emulator
see this link
http://www.ccse.kfupm.edu.sa/~mudawar/coe308/tools_manuals/pcspim.zip

another thing:
I type: 25
the result:
2,3,11,7,17,13,37,19,29,25,53,97,59,47,43,0,0,89,71,0,0,83,0,0

see that 25 is not prime

I don't know but
--> ble $t7, $s2, check_next
may cause this problem
I tried it lik this
--> blt $t7, $s2, check_next
it gives me uncomplete correct answer
why zero appear


0
IbrahimsairafiAuthor Commented:
after i adjust
--> ble $t7, $s2, check_next
to
--> blt $t7, $s2, check_next

number 25 gone but zeroes

How many prime is needed: 25
Generated prime number(s):
2,        3,        5,        11,        0,        17,        29,        23,        37,        0,        31,        41,        0,        47,        59,        53,        67,        0,        79,        71,        0,        89,        83,        0,        0
0
PaulCaswellCommented:
Here's the first few primes to compare:
      2      3      5      7     11     13     17     19     23     29
     31     37     41     43     47     53     59     61     67     71
     73     79     83     89     97    101    103    107    109    113
Looks like the table is correct but there's corruption going on after you've built it. Or perhaps you are corrupting your pointer into the table. Not sure.

I'm still struggling with the emulator so I cant give specifics. Sorry.

Paul
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IbrahimsairafiAuthor Commented:
hi paul,

I modifed my code so that with your cooment about .word 0 and i got a hint from your last comment
now i get the perfect answer

the problem was reseting the counter checker

thanx
0
PaulCaswellCommented:
Happy to help :-)

Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.