2013-10-26 181 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

相关问题