Link to home
Start Free TrialLog in
Avatar of Ibrahimsairafi
Ibrahimsairafi

asked on

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
Avatar of Ibrahimsairafi
Ibrahimsairafi

ASKER

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 ??

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
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
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
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


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
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Happy to help :-)

Paul