Shika93
Shika93

Reputation: 659

MIPS variable offset

I'm making a code that searches and finds these characters (aeiou) from another string and saves all other characters (max 3). If there aren't 3 different characters from (aeiou), it must take those one. For example I have this string: "rossi". I want compare every single character of this string with the other string and find those different. I thought like this:
if "r" is different from "a" && "e" && "i" && "o" && "u" then save "r" in a register and take the second character to compare.

I wrote this but I can't exit from loop

       .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

I know this is wrong but I don't know how to increment the offset of lbu because this take the first one

Can you help me?

Upvotes: 1

Views: 1960

Answers (1)

embedded_guy
embedded_guy

Reputation: 1967

You are stuck in that loop because you only compare the letter 'r' to the letter 'a' to see if they are equal. You have to work your way through the character arrays that you have created. One thing that you can do is take advantage of the fact that when you define a string using .asciiz, the assembler automatically appends the null terminator. This allows you to work through the problem with the following pseudo-code:

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

With the above knowledge of how you want to attack the problem, you can implement the assembly as follows:

    .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:

I hope this helps!

Upvotes: 1

Related Questions