Search code examples
arraysassemblymips32

assembly: not getting stored at the right index


I wrote code in mips 32 to get 10 numbers from the user less than 25, have an array with 25 elements and store a 7 in each index the user enters. For example if the user enters 3 4 5..there will be a 7 stored in the 3rd, 4th and 5th positions of the array. This is what I have:

.data

prompt: .asciiz "Please input 10 integers between 0 and 25:\n"
i: .byte 0
k: .byte 0
ARRAY1: .space 25
newline: .asciiz "\n"

.text

_start:
main: la $a0, prompt
      li $v0, 4
      syscall

      la $t0, ARRAY1
      lb $t1, i
      li $t2, 10
      li $t4, 25
      lb $t5, k
      li $t9, 7

get_input_loop: li $v0, 5
                syscall
                add $t0, $t0, $v0
                sb $t9, ($t0)
                sub $t0, $t0, $v0
                addi $t1, $t1, 1
                blt $t1, $t2, get_input_loop

       la $t0, ARRAY1

print: add $t0, $t0, $t5
       lb $t6, ($t0)

       move $a0, $t6
       li $v0, 1
       syscall


       addi $t5, $t5, 1
       blt $t5, $t4, print





      li $v0, 10
      syscall

For some reason the 7s don't get stored in the indices the user provides. Could someone tell me what I'm doing wrong?


Solution

  • You should have used a debugger/simulator to step through your code. Then you would have seen the 7s are stored into the right place, you are just printing the array wrong. You are adding 1 to $t5 and adding $t5 to $t0 in every iteration, so what you are printing are the indices 0, 1, 3, 6, 10, .... Your print loop should look more like:

    print: lb $t6, ($t0)
    
           move $a0, $t6
           li $v0, 1
           syscall
    
           addi $t0, $t0, 1
           addi $t5, $t5, 1
           blt $t5, $t4, print