【问题标题】:MIPS variable offsetMIPS 变量偏移
【发布时间】:2014-07-28 16:39:52
【问题描述】:

我正在编写一个代码,用于从另一个字符串中搜索并找到这些字符 (aeiou) 并保存所有其他字符(最多 3 个)。如果 (aeiou) 中没有 3 个不同的字符,则必须取其中一个。 例如我有这个字符串:“rossi”。我想将此字符串的每个字符与另一个字符串进行比较并找到不同的。 我是这样想的:
if "r" is different from "a" && "e" && "i" && "o" && "u" then 将“r”保存在寄存器中并取第二个字符进行比较。

我写了这个,但我无法退出循环

 .data 0x10010000
认知:.asciiz "rossi"
主唱:.asciiz “aeiou”

        .text 0x400000
主要:la $s0,认知
        la $s1, 主唱

cerca: lbu $t0, 0($s0)

sc_voc: lbu $t1, 0($s1)
        bne $t0, $t1, sc_voc
        移动 $s3, $t2
        塞卡

我知道这是错误的,但我不知道如何增加 lbu 的偏移量,因为这是第一个

你能帮帮我吗?

【问题讨论】:

    标签: mips offset


    【解决方案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:
    

    我希望这会有所帮助!

    【讨论】:

    • 谢谢你!你救了我!!我可以使用寄存器来存储内存中的所有字符吗?还有一件事:我可以反过来保存角色吗?如果我不这样做,我该如何选择存储字符的地址?远离认知和声音
    • @Shika93,据我所知,将值存储在单独的寄存器中并不容易。这是因为您不能以与内存相同的方式索引寄存器。您可以使用基于索引的一系列分支,并在代码的该分支中将您的 char 分配给特定的寄存器(但我认为这会变得很难看)。如果您使用单个寄存器,则可以使用按位运算。至于选择内存中的地址,您应该能够在数据段中指定任何可用地址。我只是认为使用标签会更容易。
    • 明白。另一件事:我怎么说:“如果 t1>4 那么”?像“bgt”这样的指令没有在寄存器中加载“4”。
    • 听起来您需要很好地介绍 MIPS。看看这个网站。它有你正在寻找的所有答案。 chortle.ccsu.edu/AssemblyTutorial/index.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多