[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 767

# 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
0
Ibrahimsairafi
• 5
• 4
1 Solution

Author 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

prime_proc:            la \$t0, prime_arr
again:                            lw \$t2, 0(\$t0)

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)

li \$v0, 1
move \$a0, \$s0
syscall

bnez \$t1, print_tap

#print new line
li \$v0, 4
la \$a0, nl
syscall

# end
li \$v0, 10
syscall

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

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

j again

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

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

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

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

li \$v0, 4
la \$a0, answ
syscall

sprint:            la \$t0, prime_arr
print_prime:      lw \$s0, 0(\$t0)

li \$v0, 1
move \$a0, \$s0
syscall

bnez \$t1, print_tap

#print new line
li \$v0, 4
la \$a0, nl
syscall

# end
li \$v0, 10
syscall

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

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

j again

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

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

Author Commented:
I use SPIM emulator
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

Author Commented:
--> 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

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

Author 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

Commented:
Happy to help :-)

Paul
0

## Featured Post

• 5
• 4
Tackle projects and never again get stuck behind a technical roadblock.