1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

pcspim program to reverse a string

Discussion in 'Mac Programming' started by demetris, Sep 22, 2010.

  1. macrumors newbie

    #1
    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
     
  2. Moderator emeritus

    angelwatt

    #2
    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.
     
  3. macrumors 601

    sammich

    #3
    The google-fu is strong in this one.
     
  4. macrumors 68040

    lee1210

    #4
    Here is a fortran version, so now you just have to convert it to PCSPIM.

    Code:
    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:
    Code:
    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
    
     
  5. macrumors newbie

    #5
    pcspim specific

    ok lets be more specific
    what that means:
    addi $s0,$zero,13?
     
  6. macrumors 603

    #6
    General procedure:
    1. Break it down.
    2. 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
     
  7. macrumors 6502a

    #7
    Personally I think giving the same homework assignments year after year given the internet is a big failure on the part of instructors.

    Code:
    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;
        }
    }
    
     
  8. macrumors G5

    gnasher729

    #8
    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).
     

Share This Page