2013-10-26 92 views
0
function: 


la $s0, array1  # loads address of array1 into $s0 
lw $t8, ($s0)  # loads word contained in $s0 into $t8 

la $s1, array2  # loads address of array2 into $s1 
lw $t9, ($s1)  # loads word contained in $s1 into $t9 

beq $t8, $t9, count # if first element of arrays is equal --> count 
j end 

count: 

la $t7, counter  # loads address of count into $t7 
lw $t3, ($t7)  # loads word contained in $t7 into $t3 
addi $t3, $t3, 1 # increments count by 1 
sw $t3, counter  # now count var contains 1 


printcount: 

li $v0, 4    # print string syscall code 
la $a0, prompt3  # prints "number of same elements: " 
syscall 

la $t6, counter  # loads address of count into $t6 
lw $t5, ($t6)  # loads word contained in $t6 into $t5 
li $v0, 1    # print integer syscall code 
move $a0, $t5  # move integer to be printed into $a0 
syscall 


end: 

    li $v0, 10   # system code halt 
syscall 

嗨,程序的這部分應該比較兩個數組的第一個元素(這是用戶輸入的,我已經證實數組存儲正確),以及如果這些元素相同,「計數器」將增加1,並打印出來,以便我知道它是否正常工作。MIPS:比較兩個數組中的第一個元素

問題是它總是打印'1',不管這兩個元素是否相等。什麼可能導致這個?

+0

您使用'count'作爲代碼標籤作爲一個數據項 - 它真的無法兼顧。 –

+0

謝謝,我錯誤地複製了代碼。現在應該是正確的代碼。 – DjokovicFan

+1

忘記添加條件,如果兩個不相等。添加。 – DjokovicFan

回答

0

那麼,代碼最終會執行標籤count上的指令,而不管分支是否被採用。試着像一個bne代替beq

bne $t8, $t9, skip_count # if first element of arrays not equal --> skip_count 


count: 

la $t7, counter  # loads address of count into $t7 
lw $t3, ($t7)  # loads word contained in $t7 into $t3 
addi $t3, $t3, 1 # increments count by 1 
sw $t3, counter  # now count var contains 1 

skip_count: 
+0

謝謝,我只是意識到,我發佈了最後的評論後,哈哈。代碼現在按需要工作。 – DjokovicFan

相關問題