I need to make a program that asks the user for input 8 numbers and output the max and min value. The numbers has to be stored in an array in memory.


Basically is going to be 2 loops, 1 to prompt the user for the 8 numbers and second loop to figure out the max and min values.

1st loop will go 8 times but im having trouble with storing.
li $v0, 5 #load syscall to read_int into $v0
syscall #make the syscall
move $t1, $v0 #move the number to $t1
sw $t1, 0($s0) #store number from $t1 to array $s0[0]

The problem Im running into is storing the next number in to the next cell of the array.

number 1 goes to s[0]
number 2 goes to s[1] = 4($s)
...and so on

Please help, thanks!

You should post your whole code so that we can see where exactly your error is. However, I think it may be that your not adding 4 everytime you run your loop, so consequently your storing all your numbers in 0($) If thats not the case then please post more of your code.

Here is my code so far:

#Prompts user for 8 numbers and outputs
#the maximum and minimum value

#Registers used-
#          $v0 - syscall parameter, prompt syscall to print the msg
#          $a0 - syscall parameter, the string to print
#          $t0 - current value of $t0 in the loop
#          $t1 - ending condition value for the loop


           .text
main:
     li $t0, 0                #store 0 into $t0
     li $t1, 8                #store 8 into $t1

     la $a0, prompt_msg       #load the address of the prompt_msg into $a0
     li $v0, 4                #code 4 to print the string using syscall
     syscall                  #do the syscall(prints the message)

         Loop:                    #loops 8 times prompting the user for number
         la $a0, prompt           #prompts the user for the current input number
         li $v0, 4                #prints the message
         syscall                  #calls the message
    
         move $a0, $t0            #move the value of $t0 to $a0
         li $v0, 1                #code 1 to $v0 means print integer
         syscall                  #calls syscall(prints message)

         la $a0, new_line         #calls the code for new line
         li $v0, 4                #code 4 to print out string
         syscall                  #prints out new line

                                  
                                  #need code to store each input into an array
                                  #and store them in incrementing cells

         add $t0, $t0, 1          #add 1 to the current value of $t0
         bne $t0, $t1, Loop       #if $t0 != 8, continue loop


     li $v0, 10               #code 10 to exit
     syscall                  #do the syscall, exits

           .data              #Data for the program
     prompt_msg: .asciiz "Please input 8 numbers\n"
     prompt: .asciiz "Input number "
     new_line: .asciiz "\n"

#end of program

My trouble for now is storing each user input into an array.
I know how to store in the first element say A[0] but I dont know how to increment the cells so the next input will go to A[1]. Simpler words is I dont know how to add 4 everytime it loops.

#Prompts user for 8 numbers and outputs
#the maximum and minimum value

#Registers used-
#          $v0 - syscall parameter, prompt syscall to print the msg
#          $a0 - syscall parameter, the string to print
#          $t0 - current value of $t0 in the loop
#          $t1 - ending condition value for the loop


           .text
main:
     li $t1, 0                #store 0 into $t0
     li $t2, 8                #store 8 into $t1

     la $a0, prompt_msg       #load the address of the prompt_msg into $a0
     li $v0, 4                #code 4 to print the string using syscall
     syscall                  #do the syscall(prints the message)
	
     li $sp, 0

         Loop:                    #loops 8 times prompting the user for number
         la $a0, prompt           #prompts the user for the current input number
         li $v0, 4                #prints the message
         syscall                  #calls the message

	 li $v0, 5
	 syscall
	
	  
	 move $a0, $v0   	 

         li $v0, 1                #code 1 to $v0 means print integer
         syscall                  #calls syscall(prints message)

         la $a0, new_line         #calls the code for new line
         li $v0, 4                #code 4 to print out string
         syscall                  #prints out new line

                                  
                                  #need code to store each input into an array
                                  #and store them in incrementing cells\
	 addi $sp, $sp, -4  #THIS IS WHAT CHANGES THE POSITION IN THE STACK
	 

         add $t1, $t1, 1          #add 1 to the current value of $t0
         bne $t1, $t2, Loop       #if $t0 != 8, continue loop


     li $v0, 10               #code 10 to exit
     syscall                  #do the syscall, exits

           .data              #Data for the program
     prompt_msg: .asciiz "Please input 8 numbers\n"
     prompt: .asciiz "Input number "
     new_line: .asciiz "\n"

#end of program[LIST=1]
[/LIST]

Could you explain what you did please? Im confused.
I need help on allocating the input to the memory.

At the end of your LOOP I simply added a statement that said addi $sp, $sp, -4, essentially what this does is say i++ if i is the element in the array, A. This is because in acutal memory the stack pointer goes down when a new item is inserted. For example say the $sp is currently at memory address 4000 and i want to add a word in that location and in the next I would do, sw $a0, 0($sp) then I would move the $sp to point to location 3006 because thats the next place to put in somehting. So i would do addi $sp, $sp, -4 then sw $a0, 0($sp)

hope that helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.