简体   繁体   中英

What is this MIPS array + loop doing?

New to MIPS and can understand exactly what is going on with the code, but I do not understand the answer/solution provided. Any help would be greatly appreciated.

  1. Please read the following code and write down the content in array A after funct returns.

      .data A: .word 21,3,2,9,100,22,6,15,33,90 .text .globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0, 10 # exit syscall funct: li $t0, 0 li $v1, 1000000 funct_L0: sll $t1, $t0, 2 add $t1, $t1, $a0 lw $t1, 0($t1) sub $t2, $t1, $a1 bgt $t2, $0, funct_L1 sub $t2, $0, $t2 funct_L1: bgt $t2, $v1, funct_L2 ori $v0, $t0, 0 ori $v1, $t2, 0 funct_L2: addi $t0, $t0, 1 blt $t0, $a2, funct_L0 jr $ra 

SOLUTION: Finds the smallest difference

It finds the element from the array which is nearest to the number passed in in $a1 . In other words, it finds the element x for which the difference abs(x - $a1) is smallest. It returns the index in $v0 and the difference in $v1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM