我试图使用程序集找到数组中的最大单词。我写了以下但似乎我的循环总是分支。任何帮助表示赞赏!
解决方案在评论中,谢谢Jester
# find max from array
#
.data
array: .word 3, 4, 7, 6, 5
max: .word 0
.text
#
# -- register assignments --
# $2 = counter
# $3 = sum
# $6 = loop limit (20)
#
# counter = 0
and $2, $0, $0
# sum = 0
and $3, $0, $0
addi $6, $0, 20
loop:
# if array[i] < max save array[i] as max
lw $4, array($2)
ble $3, $4, incrementCount
move $3, $4
incrementCount:
# counter++
# (words are 4 bytes, so advance counter by 4)
addi $2, $2, 4
# if counter < 5, loop (really < 20)
blt $2, $6, loop
done: sw $3, max
答案 0 :(得分:1)
ble
的操作数顺序不正确。您最初设置$3 = 0
,然后设置ble $3, $4, incrementCount
,即if (0 <= element_just_loaded) goto incrementCount
。这种情况总是正确的,因为数组中的所有元素都大于0,并且$3
的值将保持为0,因为您更改它的唯一时间是不采用分支。
解决方案是切换操作数,以便ble $4, $3, incrementCount
,if (element_just_loaded <= current_max) goto incrementCount
。