Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MIPS Write a program which uses two different methods to reverse a string. The r

ID: 3869877 • Letter: M

Question

MIPS

Write a program which uses two different methods to reverse a string. The results of both methods should compare, showing that the implementation is correct.

The StackReverse method has two calling parameters. $a0 is the address of the string containing the original, unreversed text. $a1 contains the address of a 40-character buffer where you can write the reversed string. There is no output value for this method, i.e., nothing in the $v registers. To do this task, we read through the input string, character by character, pushing each onto the stack. Even though we are reading the input string by bytes, we are storing words onto the stack . We could store bytes, but this would lead us to have to realign the stack pointer to a word boundary when we are done. This can be complicated. It’s easier to store each byte of the input string as a word on the stack. Once the characters are stored onto the stack, they are retrieved one at a time from the top of the stack. You will be reading words from the stack, but storing the low-order byte each time into the reversed-text buffer.

For the InPlaceReverse method, $a0 points to the string of text to be reversed. There is no output buffer and no output value from the method. This method operates by locating the end of the string (last character before the terminating 0) and then successively swapping characters at each end of the string, incrementing the beginning-of -string and decrementing the end-of-string pointers each time. This method exits when the pointers are equal or cross each other.

The boilerplate code then compares the results of the two reversal methods, presuming that if they match, you did the work correctly.

If your program works correctly, you will receive the following output:

Good job, looks like you're done!

-- program is finished running --

Add your code at the very bottom of the boiler plate code below. Do not change any of the code above that point in your submission. Altering the boilerplate code will result in a 0 on the assignment because it potentially changes the problem you’re trying to solve.

You will lose points on this assignment if you 1) submit code you didn’t write yourself, 2) alter the boilerplate code in the file you turn in, 3) name the file something besides <last name>.asm, 4) don’t get the right answer, 5) alter the values in $a0 or $s0, 6) do not return properly from your subroutine, 7) have side effects in your code (i.e., dependencies on other code apart from $a and $v register values), and 8) can’t explain your code if I ask you about it.

Explanation / Answer

Reverse method using stack:

.text

        .globl main

main:   #input the string

        li      $v0,8          # service code

        la      $a0,str        # address of buffer

        li      $a1,128        # buffer length

        syscall

         

        li      $t0,0          # push a null

        subu   $sp,$sp,4      # onto the stack

        sw      $t0,($sp)      # to signal its bottom

        li      $t1,0          # index of first char in str buffer

        # push each character onto the stack

pushl:

        lbu     $t0,str($t1)   # get current char into a full word

        beqz    $t0,stend      # null byte: end of string

       

        subu    $sp,$sp,4      # push the full word

        sw      $t0,($sp)      # holding the char

       

        addu    $t1,1          # inc the index

        j       pushl          # loop

    # pop chars from stack back into the buffer

stend: li      $t1,0          # index of first byte of str buffer

popl:

        lw      $t0,($sp)      # pop a char off the stack

        addu    $sp,$sp,4

        beqz    $t0,done       # null means empty stack

       

        sb      $t0,str($t1)   # store at string[$t1]

        addu    $t1,1          # inc the index

        j       popl           # loop

        # print the reversed string

done:   li      $v0,4          # service code         

        la      $a1,str        # address of string

        syscall

       la      $a0, ouput     #calling opening output

       li      $v0, 4

     syscall

        li      $v0,10         # exit

        syscall   

             

        .data

str:    .space 128            # character buffer

output: .asciiz “ Good job, looks like you are done ”

Reverse Inplace method:

.data

prompt: .asciiz " Given String is = "

str: .asciiz "Chegg"

newline: .asciiz " "

ans: .asciiz " The String reversed is = "

output: .asciiz " Good job, looks like you are done "

.text

.globl main

main:

la      $a0, prompt     #calling opening prompt

li      $v0, 4

syscall

la      $a0, str        #initial string

syscall

la      $a0, newline    #newline

syscall

la      $a0, ans        #initial text for reversed string

syscall

li      $t2, 0

strLen:                 #getting length of string

lb      $t0, str($t2)   #loading value

add     $t2, $t2, 1

bne     $t0, $zero, strLen

li      $v0, 11         #load imediate - print low-level byte

Loop:

sub     $t2, $t2, 1     #this statement is now before the 'load address'

la      $t0, str($t2)   #loading value

lb      $a0, ($t0)

syscall

#This is where the sub statement used to be, which caused the loop to terminate too early

bnez    $t2, Loop

la      $a0, output     #calling opening output

li      $v0, 4

syscall

li      $v0, 10              #program done: terminating

syscall

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote