Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. findIndex:
  2. # $a0 - int array - base address of array
  3. # $a1 - length of array
  4.  
  5. # $t0 - int being checked
  6. # $t1 - max index
  7. # $t2 - min index
  8. # $t3 - int array - base address of array
  9. # $t4 - length of array
  10. # $t5 - for loop counter / while loop counter
  11. # $t6 - int array - base address of array backup
  12.  
  13. # dont assume the regs are empty
  14. li $t0, 0
  15. li $t1, 0
  16. li $t2, 0
  17. li $t3, 0
  18. li $t4, 0
  19. li $t5, 0
  20. li $t6, 0
  21.  
  22. # save input to function
  23. move $t3, $a0 # copy $a0 to $t3
  24. move $t6, $a0 # copy $a0 to $t3
  25. move $t4, $a1 # copy $a1 to $t4
  26.  
  27. # set min and max values to be first thing
  28. lb $t0, ($t3) # load int
  29. move $t1, $t0 # $t1/max = array[0]
  30. move $t2, $t0 # $t2/min = array[0]
  31.  
  32. # check for valid inputs
  33. bgt $t4, 0 loop # if n > 0, continue to main part of code
  34. li $v0, -1
  35. li $v1, -1
  36. j error # return (-1, -1)
  37.  
  38.  
  39. # $t3 - for loop counter
  40. li $t5, 1 # i = 1
  41. loop:
  42.  
  43. bge $t5, $t4, done # if i > len_of_array, jump done
  44. lb $t0, ($t3) # load int
  45.  
  46. # testing print
  47. # li $v0, 1
  48. # move $a0, $t0
  49. # syscall
  50.  
  51. bgt $t0, $t2, skip_update_min # if value > min, jump skip_update_min
  52. move $t2, $t0 # min = value
  53. skip_update_min:
  54.  
  55. blt $t0, $t1, skip_update_max # if value < max, jump skip_update_max
  56. move $t1, $t0 # max = value
  57. skip_update_max:
  58.  
  59. # next position in the array
  60. addi $t3, $t3, 4 # add 4, each int is 4 bytes, going through array
  61.  
  62. # counter/index += 1
  63. addi $t5, $t5, 1 # counter += 1
  64.  
  65. j loop
  66. done:
  67.  
  68. # get index of max value
  69. move $t3, $t6 # create another backup for looping through array
  70. li $t5, 0 # while loop counter
  71. get_max_index_loop:
  72. lb $t0, ($t6) # load int
  73. beq $t0, $t1, get_max_index_loop_done # if value == max, end loop
  74. addi $t6, $t6, 4 # add 4, each int is 4 bytes, going through array
  75. addi $t5, $t5, 1 # counter += 1
  76. j get_max_index_loop
  77. get_max_index_loop_done:
  78. move $v0, $t5 # copy index of min value to function return
  79.  
  80. # get index of min value
  81. li $t5, 0 # while loop counter
  82. get_min_index_loop:
  83. lb $t0, ($t3) # load int
  84. beq $t0, $t2, get_min_index_loop_done # if value == max, end loop
  85. addi $t3, $t3, 4 # add 4, each int is 4 bytes, going through array
  86. addi $t5, $t5, 1 # counter += 1
  87. j get_min_index_loop
  88. get_min_index_loop_done:
  89. move $v1, $t5 # copy index of min value to function return
  90.  
  91. error:
  92. # v0 - index of max value
  93. # v1 - index of min value
  94. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement