Help with mips program that reverses each word.?

  • Thread starter Thread starter mikebrown
  • Start date Start date
  • Tags Tags
    Mips Program
Click For Summary
SUMMARY

The forum discussion centers on a MIPS assembly program that reverses each word in a string rather than the entire string. The provided code successfully reverses the entire string but requires modification to reverse individual words. A suggested approach involves using two pointers: one at the start of the word and another advancing until a space is encountered, allowing for the reversal of characters between these pointers. This method ensures that each word is reversed in place while maintaining the overall string structure.

PREREQUISITES
  • Understanding of MIPS assembly language syntax and structure
  • Familiarity with string manipulation techniques in assembly
  • Knowledge of pointers and memory addressing in MIPS
  • Experience with system calls in MIPS for input and output
NEXT STEPS
  • Implement a word-reversing subroutine in MIPS assembly
  • Explore MIPS string handling functions and their applications
  • Learn about pointer arithmetic and its implications in assembly programming
  • Study MIPS system calls for efficient input and output operations
USEFUL FOR

Assembly language programmers, computer science students, and anyone interested in low-level string manipulation techniques in MIPS.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · 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