-1

I have this homework to read array from the user then find the average and search for a specific number entered by the user. I cant print avg value and the output of findNumber give me the same result and want to read the return values of AveArray() & FindNum() methods and display the results in the main can you help me plz here is my code :

.data 
arr_size : .asciiz "Please enter number of elements (N) :  " ;
array : .asciiz "Please enter number of the array :  \n" ;
options : .asciiz "                          **** Options**** \n " ;
Compute: .asciiz "  1- To compute the average of the array elements .\n  " ;
Search : .asciiz " 2- Search for a number in the array.\n " ;
choice : .asciiz "Enter your choice:   "  ;
average : .asciiz "The average is  :    " ;
value : .asciiz "\nEnter the value to be searched :  " ;
found : .asciiz "Number is found " ;
notFound : .asciiz "Nmber is not found  " ;
arr: .word 40
#---------------------------------------------------------------------------------
.text
main:
li $t0, 0                      #sets index to 0
li $t1, 0                      #sets input num to 0
li $t2, 0                      # sets count to 0
li $s0, 0                      #sets sum to 0
li $t7, 1                      #sets choice to 1

# display the arraySize message
la $a0,arr_size      
li $v0,4                    
syscall        

#user input number of elements
li $v0,5                    
syscall
move $t3,$v0 

# display the numberOfArray message
la $a0,array           
li $v0,4                   
syscall                      
jal rearArray

# display the Options message
la $a0,options        
li $v0,4                    
syscall   

# display the firstChoice message
la $a0,Compute   
li $v0,4                    
syscall       

# display secondChoice message
la $a0,Search      
li $v0,4                   
syscall                    

# display userChoice message
la $a0,choice    
li $v0,4                   
syscall  
li $v0,5                    
syscall
move $t6,$v0 
beq $t6, $t7, aveArray
jal FindNum                 

#------------------------------------------------------------------------
rearArray:
beq $t2, $t3, end        #if  count > size of array stop
li $v0, 5
syscall
move $t1,$v0              # sets input to num
sb $t1, arr($t0)            #stores integer in array
addi $t0, $t0, 4            #increments index by 4
addi $t2, $t2, 1            #increments count
j rearArray 
end : jr $ra

#----------------------------------------------------
aveArray :
beq $t2, $t3,avg          #exits loop when everything is read
lw $t1, arr($t0)
add $s0, $s0, $t1        #calculates sum
addi $t0, $t0, 4            #increments index by 4
addi $t2, $t2, 1            #increments count
j aveArray 
avg :
div $s1, $s0, $t3        #calculates average
la $a0,average      
li $v0,4                    
syscall 

li $v0,4
move $a0
 # end of program
li $v0,10
syscall
#------------------------------------------------------------
FindNum :
la $a0,value           
li $v0,4                   
syscall
li $v0,5                    
syscall
move $t8,$v0                 #search for a specific number 

beq $t2, $t3,end1         #exits loop when everything is read
lw $t1, arr($t0)
beq $t8,$t1,end2
addi $t0, $t0, 4            #increments index by 4
addi $t2, $t2, 1            #increments count
j FindNum

end2:
la $a0,found           
li $v0,4                   
syscall 
jr $ra

end1 :
la $a0,notFound           
li $v0,4                   
syscall 
 jr $ra

1 Answers1

1
  1. When you were debugging your control flow, did you notice that your main doesn't end properly?  (Maybe it stopped working sooner than its end?)  But, with no code to finish after the call jal FindNum, it would simply run into the other function, which is bad.

  2. You are using sb, store byte, to store "words".  If the user enters numbers larger than 255, you'll find strange (truncated) results (or just weird results — depending on whether you're running big-endian or little-endian).

  3. Your word array is only 1 word long (initialized with the value 40).  as if in C: int array = 40;.

  4. Suggest you declare your array as follows:

           .align 2
    array: .space 40
    

Try not to nest the code for one function within other function.  Keep each function's code together with itself and separate from others to help avoid control flow logic errors.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • MARS simulates a little-endian MIPS, so truncated. I assume SPIM is the same. – Peter Cordes Apr 18 '20 at 20:16
  • @PeterCordes, yeah, they do (if you run them on x86 - in fact, they follow the native processor's endian), but I didn't see the OP say an environment one way or the other, so offered info on both possibilities. – Erik Eidt Apr 18 '20 at 20:20
  • Yeah worth mentioning. I just wanted to add info for the case that this obviously is: `li $v0,4` / `syscall` with a zero-terminated string is pretty definitively using MARS/SPIM toy system calls, not Linux/MIPS or AFAIK any embedded OS. Also, I had no idea SPM/MARS just used the native endianness. I didn't know you could even write Java code that depended on host endianness. [find endianness of system in java](https://stackoverflow.com/q/9431526) seems to indicate there's no pure-Java way to read the bytes of an `int` as separate `char`s in memory order, and I think MARS is a pure `.jar` – Peter Cordes Apr 18 '20 at 20:32
  • I'd believe SPIM uses native endianness since it's written in C++, but that seems implausible for MARS unless it intentionally changes behaviour based on `ByteOrder.nativeOrder()` or something. I've only used MARS, not SPIM. – Peter Cordes Apr 18 '20 at 20:36
  • 1
    @PeterCordes, you're right that MARS uses little endian. "Memory addresses and values, and register values, can be viewed in either decimal or hexadecimal format. All data are stored in little-endian byte order (each word consists of byte 3 followed by byte 2 then 1 then 0). Note that each word can hold 4 characters of a string and those 4 characters will appear in the reverse order from that of the string literal.". https://courses.missouristate.edu/KenVollmar/MARS/Help/MarsHelpDebugging.html – Erik Eidt Apr 18 '20 at 22:21