MIPS代码中的并发症递归逆转字符串

时间:2012-03-11 04:14:41

标签: string recursion mips reverse

我已经编写了这个字符串递归反转的MIPS代码。但是,输出与用户输入的输出相同。有人可以帮助我,并指出我哪里出错了吗?请尽快回复。

# Program to reverse a string 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 个答案:

答案 0 :(得分:1)

    # Program to reverse a string input by the user
.data
.align 2
    array: .space 50
    input: .asciiz "Enter a string: "
    output: .space 50
    size: .word 49
.text
j main

length:
    # return length of the input string
    # $a0 - address of string
    # $v0 - length of the string
    move $v0, $zero # set $v0 to 0
length_loop:
    lb $t0, 0($a0)
    beqz $t0, length_end
    addi $v0, $v0, 1
    addi $a0, $a0, 1
    b length_loop
length_end:
    subi $v0, $v0, 1
    jr $ra

reverse:
    # $a0 - address of string to reverse
    # a1 - length of the string
    # a2 - address of string where to store the reverse
    addi $sp, $sp, -4
    sw $ra, 0($sp)
    bltz $a1, reverse_end
    lb $t0, 0($a0)
    subi $a1, $a1, 1
    subi $a0, $a0, 1
    sb $t0, 0($a2)
    addi $a2, $a2, 1
    jal reverse
reverse_end:
    lw $ra, 0($sp)
    addi $sp, $sp, 4
    jr $ra


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

    la $a0, input
    li $v0, 4
    syscall

    # read string from the user into $a0
    la $a0, array
    lw $a1, size
    li $v0, 8
    syscall

    # reverse the string
    jal length # $v0 contains length of string
    la $a0, array
    move $a1, $v0
    add $a0, $a0, $a1   # pointer to the last character
    la $a2, output
    jal reverse

    # print the reverse string
    la $a0, output
    li $v0, 4
    syscall 

输出:

Enter a string: hello

olleh
-- program is finished running (dropped off bottom) --

答案 1 :(得分:0)

continue标签中,看起来j loop过早被调用,因此

addi $a1, $zero, 50

jal StringReversal
永远不会到达

,因此不会执行StringReversal标签中的说明。