你陷入了这个循环,因为你只比较字母“r”和字母“a”来查看它们是否相等。您必须按自己的方式处理已创建的字符数组。您可以做的一件事是利用以下事实:当您使用.asciiz 定义字符串时,汇编程序会自动附加空终止符。这使您可以使用以下伪代码解决问题:
place starting address of cognome in $s0
place starting address of voc in $s1
place starting address of storage in $s3
initialize an index for cognome ($t3 = 0)
initialize an index for voc ($t4 = 0)
initialize an index for storage ($t5 = 0)
LOOP1
if (current value stored at cognome index does not equal 0)
LOOP2
if (current value stored at voc index does not equal 0)
if (value stored at cognome index is equal to value stored at voc index)
increment cognome index
set voc index equal to 0
goto LOOP1
else
increment voc index
goto LOOP2
else
store value at address based on storage index
increment storage index
increment cognome index
set voc index equal to 0
goto LOOP1
有了上面的知识,你想如何解决问题,就可以实现如下组装:
.data 0x10010000
cognome: .asciiz "rossi"
voc: .asciiz "aeiou"
storage:
.text 0x400000
la $s0, cognome #load starting address of "rossi"
la $s1, voc #load starting address of "aeiou"
la $s2, storage #load starting address to save desired letters
li $t3, 0 #initialize index for cognome
li $t4, 0 #initialize index for voc
li $t5, 0 #initialize index for storage
loop1:
lbu $t1, cognome($t3) #load value at cognome[$t3] into $t1
beqz $t1, end #if the value of $t1 is NULL, goto end
loop2:
lbu $t2, voc($t4) #load value at voc[$t4] into $t2
beq $t1, $t2, is_vowel #if $t1 == $t2 do not store, goto is_vowel
addiu $t4, $t4, 1 #increment voc index
beqz $t2, save #if $t2 is NULL, all vowels have been checked,
# and the value in $t1 is not a vowel, goto save.
b loop2 #Otherwise, check $t1 against the next letter in voc
# by going to loop2
save:
sb $t1, storage($t5) #store the letter at storage[$t5]
addiu $t5, $t5, 1 #increment the storage index
is_vowel:
li $t4, 0 #reset the voc index
addiu $t3, $t3, 1 #increment the cognome index
b loop1 #check the next letter in cognome, goto loop1
end:
我希望这会有所帮助!