回文检查汇编程序 - MIPS

时间:2017-06-21 11:33:50

标签: assembly mips palindrome

我是Assemlber(MIPS)的新手,我必须编写一个回文检查程序。我没有得到如何做到这一点。我需要做的是,计算字符串的长度如下:

     .data
string: .asciiz "hello"

printedMessage: .asciiz "The length of the string: "

    .text
main:
  la $a0, string             # Load address of string.
        jal strlen              # Call strlen procedure.
        jal print
        addi $a1, $a0, 0        # Move address of string to A1
        addi $v1, $v0, 0        # Move length of string to V1
        addi $v0, $0, 11        # System call code for message.
        la $a0, printedMessage            # Address of message.
        syscall
        jal reverseString
        addi $v0, $0, 10        # System call code for exit.
        syscall


strlen:
    li $t0, 0 # initialize the count to zero
    loop:
        lb $t1, 0($a0) # load the next character into T1
        beqz $t1, exit # check for the null character
        addi $a0, $a0, 1 # increment the string pointer
        addi $t0, $t0, 1 # increment the count
        j loop # return to the top of the loop
    exit:
        jr $ra

print:
    li $v0, 4
    la $a0, printedMessage
    syscall

    li $v0, 1
    move $a0, $t0
    syscall

    jr $ra

但是我通过阅读例子等来做到这一点所以现在我想要扭转字符串,但是我遇到了一些麻烦。

我想做的是

set j = strLength - 1
while(j>=0){
load next character
store character to array
j--
}

我尝试如下:

reverseString:
    li $t2, 0
    la $a0, string
    add $t2,  $t2, $t0 #initialize j with length of string - 1
    sub $t2, $t2, 1
    loop2:
        add $t3, $t2, $a0
        lb $t1, 0($t3) #load the next character into T1
        beq $t2, $0, exit
        subi $t2, $t2, 1 #j--
        j loop2

此代码向后遍历字符串,但是如何创建一个新字符串,我在$t1每次迭代中添加字符

我需要保留原始字符串以及反转字符串,以便我可以循环检查它们是否相等。

0 个答案:

没有答案