PDA

View Full Version : pcspim program to reverse a string




demetris
Sep 22, 2010, 09:11 AM
i need a program which does 2 things:
1)reads a string from the console and returns the string reversed
2)reads a string that we give,from the memory and returns the string reversed



angelwatt
Sep 22, 2010, 09:13 AM
Sounds like a homework assignment. We're not against helping, but we won't do your homework for you. Give it a shot on your own and ask questions about specific problems you're having with the code.

sammich
Sep 22, 2010, 09:16 AM
The google-fu is strong in this one.

lee1210
Sep 22, 2010, 09:22 AM
Here is a fortran version, so now you just have to convert it to PCSPIM.


program revstr
implicit none
character(32768) :: inputstr
integer :: pos,strlen,pos2
character :: single

write(6,'("Please enter the string to reverse: ",$)')
read(5,*) inputstr
strlen = len_trim(inputstr)
do pos=1,strlen/2
pos2 = strlen+1-pos
single = inputstr(pos:pos)
inputstr(pos:pos)=inputstr(pos2:pos2)
inputstr(pos2:pos2)=single
enddo

write(6,'("The reversed string is: ",a)') inputstr(1:strlen)

end program revstr


-Lee

Edit: If you really want to cheat, the first google hit for "MIPS assembly reverse a string" is the code you need. I hope your professor uses some anti-cheat software to catch things like this so he can fail/expel you for academic integrity violations. If you don't intend to cheat, write what you can of this code, and post a specific problem and we'll try to help.

Edit 2: This is a little more efficient, but doesn't actually reverse the string in memory:

program revstr
implicit none
character(32768) :: inputstr
integer :: pos,strlen

write(6,'("Please enter the string to reverse: ",$)')
read(5,*) inputstr
strlen = len_trim(inputstr)
do pos=strlen,1,-1
write(6,'(a1,$)') inputstr(pos:pos)
enddo

write(6,'()')

end program revstr

demetris
Sep 22, 2010, 10:14 AM
ok lets be more specific
what that means:
addi $s0,$zero,13?

chown33
Sep 22, 2010, 11:28 AM
ok lets be more specific
what that means:
addi $s0,$zero,13?

General procedure:

Break it down.
Look it up.


Breaking It Down

The Wikipedia article on MIPS architecture has this brief table entry for addi:
$t = $s + C (signed)
...
Used to add sign-extended constants (and also to copy one register to another: addi $1, $2, 0), executes a trap on overflow

So using the arithmetic expression and the description, it means:
$s0 = $zero + 13 (signed)

The description would be:
Adds the value of $zero to the constant 13 and stores the result in $s0. The addition is signed, and there is a trap on overflow.

It's left to you to determine any special significance of registers such as $zero, and whether or not the addition could cause a trap on overflow.

Looking It Up

If any part of of the above doesn't make sense, then you should study your textbook and any other reference materials you have on the MIPS architecture.

If you're learning MIPS assembly language in a classroom situation, and are unable to get the information you need from studying your textbook and other references, then you should ask your instructor for guidance on where to get tutorial assistance. Most schools have some kind of teaching assistants who can provide extra coaching. Your instructor or guidance counselor is the best source for this. Or throw a party with free beer, and invite all the smart kids in the class.

If you're not learning MIPS in a classroom situation, then describe what book or tutorial you're learning it from.

Coming here for an explanation of every MIPS instruction is unlikely to be an effective strategy.

References:
http://en.wikipedia.org/wiki/MIPS_architecture

lloyddean
Sep 22, 2010, 11:38 AM
Personally I think giving the same homework assignments year after year given the internet is a big failure on the part of instructors.


void reverse(char string[])
{
char* lhs = &string[0]-1;
char* rhs = &string[strlen(string)];
char temp;
while ( ++lhs < --rhs )
{
temp = *lhs;
*lhs = *rhs;
*rhs = temp;
}
}

gnasher729
Sep 22, 2010, 12:53 PM
Personally I think giving the same homework assignments year after year given the internet is a big failure on the part of instructors.

I suggest asking for code that reverses a C string in UTF-8 format correctly. Much more fun. Extra points if it groups Unicode base characters + modifiers correctly (so a letter A followed by an accent grave is kept in that order).