Use of array in mips to store user input and search

Viewed 380

I'm new to this mips program and I'm trying to write a program that the user choose the number of items that they want to have in an array (between 1-10) and then enter those values which we will store into our array.(note: user input is in bold and underlined) and one more question how do we search that the number is present in the array list or not?

.data
    prompt0:.asciiz "How many items will the list contain?"
    prompt: .asciiz "\n Enter a number between 1 to 10 : "
    prompt1: .asciiz "\n Enter the first value: "
    prompt2:.asciiz  "\n Enter the next value: "
    .align 4
    Array: .space 32

.text 
li $v0,4
la $a0,prompt0
syscall

while:
beq $t0,4,exit

li $v0,4
la $a0,prompt
syscall

li $v0,5
syscall

li $v0,4
la $a0,prompt1
syscall

li $v0,5
syscall

sw $v0,Array($t0)
addi $t0,$t0,4

j while

exit:
li $v0,4
la $a0,prompt2
syscall

li $v0,5
syscall

sw $v0,Array($t0)
addi $t0,$t0,4
syscall

li $v0,10
syscall


excepted output:
How many items will the list contain?
Enter a number between 1 and 10: 5
Enter the first value: 1
Enter the next value: 2
Enter the next value: 9
Enter the next value: 7
Enter the next value: 5
1 Answers

There are few problems other than the mentioned ones. Hard to even keep track on them. One of them is the placement of the while label. Why it is there? You will keep printing the prompt string this way.

Also searching for an item is not that hard just iterate over the array and use for example sub to determine if the current array element is equal to the searched for number, because in that case their difference will be 0.

Rather then trying to list all the problems I show you how I made it work (Note: It most likely not the optimal way, but will get the job done)

.data
    prompt0:.asciiz "How many items will the list contain?"
    prompt: .asciiz "\n Enter a number between 1 to 10 : "
    prompt1: .asciiz "\n Enter the first value: "
    prompt2:.asciiz  "\n Enter the next value: "
    prompt3:.asciiz  "\n Enter the value to search for: "
    search_success:.asciiz  "The array contains the number"
    search_fail:.asciiz  "The array does not contains the number"

.align 4
    Array: .space 32

.text 

### prompt to ask for the number of items
li $v0,4
la $a0,prompt0
syscall

li $v0,4
la $a0,prompt
syscall

### read in the # of items
li $v0,5
syscall

move $t1, $v0 # save the # of items
move $t4, $v0 # save the # of items to use it in search also

### if the # of items is outside the [1,10] range then terminate program
bge $t1, 11, end 
ble $t1, 0, end

### loading in the Array start address
la $t0, Array

### prompt for 1st value
li $v0,4
la $a0,prompt1
syscall

### read in the first value
li $v0,5
syscall

### save first value and advancing the pointer
sw $v0,($t0)
addi $t0,$t0,4

### decrease the # of items need to be read in
sub $t1,$t1,1

### branch to the end of the read in loop to skip it since array has only 1 element
beq $t1, 0, end_loop

loop:
#### prompt for next item
li $v0,4
la $a0,prompt2
syscall

### read in next item
li $v0,5
syscall

### save the read in value to array
sw $v0,($t0)
addi $t0,$t0,4

### decrease the # of items need to be read in
sub $t1,$t1,1
bne $t1, 0, loop
end_loop:
###########################################
### Search in the array for given value ###
###########################################

### prompt for the number to search for
li $v0,4
la $a0,prompt3
syscall

### read in the number
li $v0,5
syscall

### load Array start address
la $t0, Array

search_loop:
lb $t2, ($t0) # load in the current Array element
sub $t3, $t2, $v0 # *Array - searched_number
beq $t3, 0, found_num # if the result 0 meaning they are equal -> branch
addi $t0, $t0, 4 # if not then advance the Array pointer
subi $t4, $t4, 1 # decrease the number of items remaining 
beq $t4, 0, do_not_found_num # if no more item remain -> the number is not in the array
b search_loop # loop

found_num:
### print succes string
li $v0,4
la $a0,search_success
syscall
b end

do_not_found_num:
### print fail string
li $v0,4
la $a0,search_fail
syscall
b end

end:
li $v0,10
syscall
Related