> Display a reverse string in MIPS?

Display a reverse string in MIPS?

Posted at: 2014-12-18 
I am trying to write a function that will read in 6 characters from the keyboard, and display them. Then print the string in reverse. This is the code in C language.

reverse6( )

{ char cstring[6];

int n;

n = 0;

while (n <6) {

cstring[n] = echo( );

n++;

}

loop: //teacher does this to let us understand easier

n- -;

putchar(cstring[n]);

if (n > 0) goto loop

}

This is my code in MIPS. My putchar and echo function works. However, my reverse function does not work. At first it printed one input character 6 times each without stopping. Therefore, can someone help me? Thank you.

reverse6:

add $sp, $sp, -16

sw $a0, 0($sp)

sw $ra, 4($sp)

sw $t8, 8($sp)

sw $t9, 12($sp)

add $t8, $t8, $0 # n = 0

while:

move $t8, $a0

lb $t8, 0($a0)

slti $t9, $t8, 6 # if n > 6

beq $t9, $0, loop # go to loop

jal echo # else cstring[n] = echo( );

addi $t8, $t8, 1 # n++

sb $t8, 12($t8)

lw $a0, 0($sp)

lw $ra, 4($sp)

lw $t8, 8($sp)

lw $t9, 12($sp)

add $sp, $sp, 16

jr $ra

loop:

move $t8, $a0

lb $t8, 0($a0)

addi $t8, $t8, -1 # n--

jal putchar

slt $2, $t8, $0

beq $2, $0, loop

jr $ra

The echo function displays what the user input, then the putchar function displays the string but in reverse order.