Help with mips program that reverses each word.?

Discussion in 'Mac Programming' started by mikebrown, Dec 11, 2010.

  1. mikebrown macrumors newbie

    Joined:
    Dec 11, 2010
    #1
    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 lenght 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 :)
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Is this homework?

    What environment (spim?) are you using on your Mac if someone wanted to try running this themselves?

    Have you devised an algorithm/methodology for doing this, but don't know how to code it? If you had an algorithm, could you code it yourself?

    -Lee
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    The OP has not returned since the original post, so I'll try to give general advice without giving this away, as it is likely homework.

    You already have a way to reverse a string given a base address and a length. It should be pretty easy to write something that gets word length (bound by the end of the total string, of course). It should then be trivial to pass the base of the word and the word length to the current reverse routine. Since this routine does the swap "in place" once you've done all the words one at a time, you're done.

    -Lee
     

Share This Page