使用MIPS查找两个阵列的MAX值

时间:2015-10-28 21:03:00

标签: arrays function mips

我应该在两个不同的数组中找到最大值。这是C

中的一个例子
int largest( int* array, int arrayLength) {
    int largest = array[0];
    int index = 1;
    while( index < arrayLength) {
        if( array[index] > largest {
            largest = array[index];
        }
        index++;
    }
    return( largest);
}

我应该通过在MIPS程序集中实现一个函数并在两个不同的数组上测试main函数来做同样的事情。我在下面提供了我的代码,但我是新的,不确定它是否有效,因为我无法在此计算机上访问QTSPIM。如果您有任何建议,修复或任何帮助,我们非常感谢。感谢

.data

arrayOne:   .word   -10, 9, 5, 60, 12, -33, 10
arrayTwo:   .word   20, 44, -9, 8, 72, 25, -36

.text
main:

la $s0, arrayOne    #load the array
move $a0, $s0       #move array to register the function can use
jal Largest     #call the function
la $s0, arrayTwo    #load the other array
move $a0, $s0
jal Largest
li $v0, 10  #exit program
syscall

Largest:
addi $sp, $sp, -4   #save return address
sw $ra, 0($sp)

move $t0, $a0   #load array into temp register
lw $v0, 0($t0)  #set maximum to array[0]
li $t2, 1   #set index = 1
Loop:
slt $t3, $t2, $t1   #???
beq $t3, $zero, ExitLoop
sll $t3, $t2, 2     #find offest of next element
add $t3, $t0, $t3   #store address in $t3
lw $t4, 0($t3)      #load next value into $t4
sgt $t5, $t4, $v0   #compare previous max with new value
beq $t5, $zero, notGreater  #jump if not greater

move $v0, $t4   #if greater, set return value to new max

notGreater:
addi $t2, $t2, 1    #increment index
j Loop

ExitLoop:
lw $ra, 0($sp)      #unload the stack
addi $sp, $sp, 4
jr $ra              #return to main

0 个答案:

没有答案