我正在制作一个代码,用于从另一个字符串中搜索并找到这些字符(aeiou)并保存所有其他字符(最多3个)。如果(aeiou)没有3个不同的字符,则必须使用这些字符。
例如,我有这个字符串:" rossi"。我希望将此字符串的每个字符与另一个字符串进行比较,然后找到不同的字符串。
我这样想:
if "r" is different from "a" && "e" && "i" && "o" && "u" then
保存" r"在寄存器中并将第二个字符进行比较。
我写了这个,但我无法退出循环
.data 0x10010000 cognome: .asciiz "rossi" voc: .asciiz "aeiou" .text 0x400000 main: la $s0, cognome la $s1, voc cerca: lbu $t0, 0($s0) sc_voc: lbu $t1, 0($s1) bne $t0, $t1, sc_voc move $s3, $t2 j cerca
我知道这是错的,但我不知道如何增加lbu的偏移,因为这是第一个
你能帮助我吗?
答案 0 :(得分:1)
你被困在那个循环中,因为你只是比较字母' 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:
我希望这有帮助!