Help with mips program that reverses each word.?

In summary, the conversation is about a program that reads a string and reverses the characters in each word, rather than the entire string. The program needs a subroutine that will reverse the characters in a word, using two pointers to identify the beginning and end of each word.
  • #1
mikebrown
1
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
  • #2
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.
 

What is a MIPS program?

A MIPS (Microprocessor without Interlocked Pipeline Stages) program is a type of assembly language used for programming microprocessors. It is commonly used for educational purposes and for low-level programming tasks.

What does the MIPS program that reverses each word do?

This program takes a string of words as input and reverses the order of the letters in each word. For example, the word "hello" would become "olleh" after the program is run.

What is the purpose of reversing each word in a string?

There are a few potential reasons for wanting to reverse each word in a string. It could be for encryption purposes, for data processing or manipulation, or simply as an exercise in coding and understanding assembly language.

What are the main steps in creating a MIPS program that reverses each word?

The main steps in creating this program would include: reading in the input string, breaking it into individual words, reversing the order of the letters in each word, and then combining the words back into a single string. This would likely involve using loops, string manipulation functions, and possibly arrays.

What are some potential challenges in creating a MIPS program that reverses each word?

Some potential challenges in creating this program could include handling different types of input (such as strings with punctuation or numbers), managing memory efficiently, and ensuring the program works correctly for all possible cases and edge cases.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
20K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
2
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top