首页 > 解决方案 > 使用 mips32 程序集的数组中的最大元素

问题描述

我必须编写 MIPS 汇编代码来查找数组的最大元素,其中我们知道:数组的索引在寄存器 $9 中,数组的长度在 $11 中,数组的起始地址是 500,最大元素必须在寄存器 $10 中设置。所以我的想法是将最大元素初始化为array[0],然后通过for循环并比较array[i]>max_elem。这是我的汇编代码:

add $9,$0,$0 #set index to 0
beg $11,$0,EXIT #if array is empty go to label EXIT
muli $13,$9,4 #4*i(relative address)
lw $14,500($13) #in register $14 now is array[0]
add $10,$14,$0 #initialize max_elem to array[0]
addi $9,$9,1 #now we go through loop starting from index 1

Loop: 
muli $13,$9,4 #in register 13 now is 4*i where i>=1
lw $14,500($13) #now in $14 we have array[1]
slt $8,$10,$14 #in register 8 we have 1 if array[1]>max, otherwise 0
beq $8,$0,L #if array[1]<max then go to label L
add $10,$14,$0 #otherwise set max to that element

L:
addi $9,$9,1 #set index i to i+1
beq $9,$11,EXIT #if index is equal to length of array we traversed through array
j Loop #otherwise jump to loop again

Exit:

如果有人可以帮我检查一下,我将不胜感激:)。

标签: assemblymips32

解决方案


推荐阅读