MIPS交换功能陷入无限循环

时间:2018-12-17 18:19:37

标签: mips

我正在编写一个MIPS移动元音函数,现在我陷入无限循环,因为返回地址有问题。有人可以帮忙吗?该问题仅适用于功能“ moveVowelsToBeginning”,功能countvowels和isVowels可以正常工作。 C代码:

char myName[8] = "example"; 
int main()
{
    vowels=countvowels(0,6); //count the vowels using recursion
    moveVowelsToBeginning(0,6); //move vowels to the beginning
    return 0;
}

int countvowels(int low, int high)
{
    omit...
}
int isVowel(int low) // whether the indexed character is a vowel
{
    if vowel ("omit code here")
        return 1;
    else
        return 0;
}

int moveVowelsToBeginning(int low, int high)
{
    int i, j; //two indexes
    char temp;
    j=low; //this index used as head of detected vowels
    for(i=low;i<=high;i++)
    {
        if(isVowel(i))
        {
        //swap characters
            temp = myName[j];
            myName[j]= myName[i];
            myName[i] = temp;
            j++;
        }
    }
}

函数moveVowelsToBeginning的MIPS代码:

moveVowelsToBeginning:  
addiu   $sp,$sp,-16             #stack grows by 16 bytes    
sw  $ra,12($sp)                 #save return address    
sw  $a0,0($sp)                  #save the arguments into stack  
sw  $s0,4($sp)                  #$s0 is used to hold the variable i
sw  $s1,8($sp)                  #$s1 is used to hold the variable j

add $s0,$zero,$a0               #i==0
add $s1,$zero,$a0               #j==0
addiu $t3,$zero,6               #high==6
la $t7, myName                  #gets base address of myName

for_loop:
bgt $s0, $t3, for_loop_done     #if i>high, go to for_loop_done to exit the loop
jal isVowel                     #jump to procedure isVowel    
bne $v0, $zero, swap            #compare the return value to 0, if not 0, call swap procedure.
addi $s0, $s0, 1                #i++
move $a0, $s0
j for_loop                      #loop again

swap:
addiu   $sp,$sp,-8              #stack grows by 24 bytes    
sw  $ra,4($sp)                  #save return address    
sw  $s2,0($sp)                  #$s2 is used to hold the variable temp  


add $t4,$t7,$s1         # myName[j] = myName + j
lbu $s2,0($t4)          # temp=myName[j]
add $t5,$t7,$s0         # myName[i] = myName + i
lbu $t6,0($t5)          # myName[i]
lbu $s7,0($t4)          # myName[j]
move $s7,$t6            # myName[j]= myName[i]
move $t6,$s2            # myName[i] = temp
sb $s7,0($t4)
sb $t6,0($t5)
addi $s0,$s0,1          # i++
addi $s1,$s1,1          # j++

sw  $s2,0($sp)          #restore    
sw  $ra,4($sp)                  
addiu   $sp,$sp,8

jr $ra

for_loop_done:
lw  $s1,8($sp)
lw  $s0,4($sp)
lw  $a0,0($sp)
lw  $ra,12($sp)
nop                     # put in breakpoint here to show memory

0 个答案:

没有答案
相关问题