0

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

cHao
  • 84,970
  • 20
  • 145
  • 172
  • In order to get text formatted as code, indent it 4 spaces and make sure it's preceded and followed by blank lines. For future reference. :) – cHao Oct 17 '12 at 23:28

1 Answers1

0

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.

Jester
  • 56,577
  • 4
  • 81
  • 125