2015-10-14 76 views
1

我正在嘗試使用程序集在數組中找到最大字數。我寫了以下內容,但似乎我的循環始終是分支。任何幫助表示讚賞!查找數組中的最大字(MIPS程序集)

解決方案是在評論,謝謝小丑

# 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

'sw $ 3,($ 4)'應該是'move $ 3,$ 4',因爲$ 4不是地址。 – Jester

+0

感謝您的糾正,我做了調整。它仍然看起來我的程序沒有分支 –

+1

同樣你的分支是顛倒過來的,如果最大值小於當前值,則跳過。你如何通過調試器中的代碼? – Jester

回答

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