Help with mips program that reverses each word.?

  • Thread starter Thread starter mikebrown
  • Start date Start date
  • Tags Tags
    Mips Program
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 6K views
mikebrown
Messages
1
Reaction score
0
Hello, I have a problem that has been driving me crazy. I have a program that reads a string and then reverses the string but what I need is the program to reverse each word like: ("10 is ten" becomes "01 si net"). Can anyone help? Here is what I have:
# PROGRAM:

.data # Data declaration section
a_string: .asciiz "desrever eb ot gnirts a si sihT\n"

.text # Assembly language instructions
main: la $a0,a_string #base address of string
li $v0,4 #read string
syscall

addi $a1,$zero,32 #pass length of string
jal stringreverse #reverse the string

stringreverse:
add $t0,$a0,$zero #starting address
add $t1,$zero,$zero #i = 0
addi $t2,$a1,-1 #j = length-1

loop:
add $t3,$t0,$t1
lb $t4,0($t3) #the lb string
add $t5,$t0,$t2
lb $t6,0($t5) #the lb string[j]
sb $t4,0($t5) #string[j] = string
sb $t6,0($t3) #string = string[j]
addi $t1,$t1,1 #i++
addi $t2,$t2,-1 #j--
#if i>=j then break - $t1 < $t2
slt $t6,$t2,$t1
beqz $t6,loop

exit:
li$v1, 4 #system call to print reversed string
la$a2, 0($a1)
syscall

li $v0, 10
syscall # Exit program

Thanks in advance for any help :)
 
Physics news on Phys.org
You have what is essentially a subroutine to reverse the string. What you need is one that reverses the characters in a word. One way to do this is to have one pointer at the beginning of the word and advance another pointer until you come to a space in the word, then reverse the characters between the first pointer and the second (but not including the space). After reversing the characters, reset both first pointers to the start of the next word, and advance one of them through the string until you hit another space. Continue in this fashion until you have gone through the entire string.