MIPS correction
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: MIPS correction

  1. #1
    Join Date
    Mar 2012
    Posts
    3

    Question MIPS correction

    # Program to reverse a string input by the user. Please help me out where am i going wrong? This code is giving the same output string that has been input by the user

    .data
    .align 2
    array: .space 50
    input: .asciiz "Enter a string: "
    output: .asciiz "\nThe reversed string is: "
    .text
    .globl main

    main:
    addi $s0, $zero, 50
    addi $t0, $zero, 0

    la $a0, input
    li $v0, 4
    syscall

    la $a0, array

    li $v0, 8
    syscall

    initiate:
    add $t0, $a0, $zero # initial address
    add $t1, $zero, $zero # count=0
    add $t2, $zero, $zero # i=0
    la $t0, array # base address of the array
    add $t3, $t0, $t2 # & array[i]

    loop:
    lb $t3, 0($t3) # fetch array[i]
    beqz $t3, EndOfString # loop exits if it is a null character; array[i] !='\0'
    bne $t3, $0, continue # otherwise loop continues
    add $t1, $t1, 1 # count++

    continue:
    add $t2, $t2, 1 # i++
    j loop

    addi $a1, $zero, 50

    jal StringReversal

    EndOfString:
    la $a0, output
    li $v0, 4
    syscall

    la $a0, array
    li $v0, 4
    syscall

    li $v0, 10
    syscall

    StringReversal:
    add $t0, $a0, $zero # initial address
    add $t4, $zero, $zero # j = start = 0
    addi $t5, $a1, -1 # k = end-1

    SwapLoop:
    add $t6, $t0, $t4
    lb $t7, 0($t6) # load byte array[start]
    add $t8, $t0, $t5
    lb $t9, 0($t8) # load byte array[end-1]
    sb $t7, 0($t8) # array[end-1] = array[start]
    sb $t9, 0($t6) # array[start] = array[end-1]
    addi $t4, $t4, 1 # j++
    addi $t5, $t5, -1 # k--
    slt $t9, $t5, $t4
    beqz $t9, SwapLoop
    jr $ra

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: MIPS correction

    [Moved to the Assembly forum from the Algorithms and Data Structures Forum]
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: MIPS correction

    Quote Originally Posted by pratzy View Post
    # Program to reverse a string input by the user. Please help me out where am i going wrong? This code is giving the same output string that has been input by the user

    Code:
    SwapLoop:
        add $t6, $t0, $t4
        lb $t7, 0($t6)           # load byte array[start]
        add $t8, $t0, $t5
        lb $t9, 0($t8)       # load byte array[end-1]
        sb $t7, 0($t8)       # array[end-1] = array[start]
        sb $t9, 0($t6)       # array[start] = array[end-1]
        addi $t4, $t4, 1     # j++
        addi $t5, $t5, -1    # k--
        slt $t9, $t5, $t4
        beqz $t9, SwapLoop
        jr $ra
    Look at the bit I've highlighted in red. You aren't swapping the values array[start] and array[end-1] properly. You should be putting the value you saved in $t9 into $t7, not $t6:
    Code:
    lb $t9, 0($t8) # load byte array[end-1]
    sb $t7, 0($t8) # array[end-1] = array[start]
    sb $t9, 0($t7) # array[start] = array[end-1]

  4. #4
    Join Date
    Mar 2012
    Posts
    3

    Re: MIPS correction

    But this is giving an error. Stating 'Bad Address'

  5. #5
    Join Date
    Jan 2009
    Posts
    596

    Re: MIPS correction

    Quote Originally Posted by pratzy View Post
    But this is giving an error. Stating 'Bad Address'
    Which line is giving this error?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center