Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- findIndex:
- # $a0 - int array - base address of array
- # $a1 - length of array
- # $t0 - int being checked
- # $t1 - max index
- # $t2 - min index
- # $t3 - int array - base address of array
- # $t4 - length of array
- # $t5 - for loop counter / while loop counter
- # $t6 - int array - base address of array backup
- # dont assume the regs are empty
- li $t0, 0
- li $t1, 0
- li $t2, 0
- li $t3, 0
- li $t4, 0
- li $t5, 0
- li $t6, 0
- # save input to function
- move $t3, $a0 # copy $a0 to $t3
- move $t6, $a0 # copy $a0 to $t3
- move $t4, $a1 # copy $a1 to $t4
- # set min and max values to be first thing
- lb $t0, ($t3) # load int
- move $t1, $t0 # $t1/max = array[0]
- move $t2, $t0 # $t2/min = array[0]
- # check for valid inputs
- bgt $t4, 0 loop # if n > 0, continue to main part of code
- li $v0, -1
- li $v1, -1
- j error # return (-1, -1)
- # $t3 - for loop counter
- li $t5, 1 # i = 1
- loop:
- bge $t5, $t4, done # if i > len_of_array, jump done
- lb $t0, ($t3) # load int
- # testing print
- # li $v0, 1
- # move $a0, $t0
- # syscall
- bgt $t0, $t2, skip_update_min # if value > min, jump skip_update_min
- move $t2, $t0 # min = value
- skip_update_min:
- blt $t0, $t1, skip_update_max # if value < max, jump skip_update_max
- move $t1, $t0 # max = value
- skip_update_max:
- # next position in the array
- addi $t3, $t3, 4 # add 4, each int is 4 bytes, going through array
- # counter/index += 1
- addi $t5, $t5, 1 # counter += 1
- j loop
- done:
- # get index of max value
- move $t3, $t6 # create another backup for looping through array
- li $t5, 0 # while loop counter
- get_max_index_loop:
- lb $t0, ($t6) # load int
- beq $t0, $t1, get_max_index_loop_done # if value == max, end loop
- addi $t6, $t6, 4 # add 4, each int is 4 bytes, going through array
- addi $t5, $t5, 1 # counter += 1
- j get_max_index_loop
- get_max_index_loop_done:
- move $v0, $t5 # copy index of min value to function return
- # get index of min value
- li $t5, 0 # while loop counter
- get_min_index_loop:
- lb $t0, ($t3) # load int
- beq $t0, $t2, get_min_index_loop_done # if value == max, end loop
- addi $t3, $t3, 4 # add 4, each int is 4 bytes, going through array
- addi $t5, $t5, 1 # counter += 1
- j get_min_index_loop
- get_min_index_loop_done:
- move $v1, $t5 # copy index of min value to function return
- error:
- # v0 - index of max value
- # v1 - index of min value
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement