Help with mips program that reverses each word.?

  • Thread starter Thread starter mikebrown
  • Start date Start date
  • Tags Tags
    Mips Program
Click For Summary
The discussion centers around modifying an assembly language program to reverse each word in a string rather than the entire string. The original program successfully reverses the entire string but needs adjustment to reverse individual words. The suggested solution involves using two pointers: one at the start of a word and another that advances until a space is encountered. Once a space is found, the characters between the two pointers are reversed. This process is repeated for each word in the string until the entire string has been processed. The key focus is on implementing a subroutine that can handle word-level reversal effectively.
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 :)
 
Technology 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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
21K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 5 ·
Replies
5
Views
10K
Replies
1
Views
2K